Creating variables in a foor loop using num2str
7 views (last 30 days)
title probably says it all. I want to create variables according to the i in the loop. I just don't even know if I can incorporate the function (I've already tried num2str, eval, evalc, sprintf) into variable definition. Below is a non-working code just to illustrate what I mean to do (hopefully). This example is just for 3 different variables but later on, I need to do this for about 60 more variables. I just cannot quite put my hand on the right formulation of the loop.
Thank you for any advices
%%% years_10 = [xvalues, yvalues]... n*2 matrices
years_num2str(i) = readmatrix(sprintf('%d_years.csv',i);
William Rose on 23 Nov 2022
Edited: William Rose on 23 Nov 2022
[edit: change "v" to "s" in my description, to match the code]
for i=10:10:N, s.(strcat('a',num2str(i)))=i^2; end
creates a structure s with fields a10, a20, ..., a50. Each one equals a scalar. You could initialize each one to be a vector or array, if you prefer.