Vary Variable name in a for loop

Hello Matlab community,
I am a beginner in Matlab and I use it normally for data treatment.
The software I'm using ouputs my acquired data in multiples Traces when I import it in Matlab in a similar way than showed at the bottom:
Trace_1_1_1_1
Trace_1_1_2_1
Trace_1_1_3_1
Trace_1_2_1_1
...
I would like to create a new matrix where I would have 3 columns that would go like this:
Trace_1_1_1_1 Trace_1_1_2_1 Trace_1_1_3_1
Here is my current for loop I would like to used to accomplish this task since I have 58 different Traces.
for i = 1:58;
for j = 1:3;
a_i = zeros(size(Trace_1_i_j_1,1),3);
a_i(:,j) = Trace_1_i_j_1(:,2);
end end
Matlab don't have the ability to use the i and j values and implement them in the Trace variable. Do you have any suggestion to overcome this issue?
I don't know if I made myself clear.
Feel free to reply and ask further questions.
Thanks for all the help!

Réponses (2)

Babak
Babak le 5 Déc 2012
Modifié(e) : Babak le 5 Déc 2012

0 votes

you can create a name with this:
myvarname = cell(3,1);
for j=1:3
myvarname{j} = sprintf('Trace_1_1_%u_1',j)
end
Then you can use these names as follows to store values in workspace
clear assignin;
for j=1:3
assignin('base',myvarname{j},2*j+5); % for example
end
To get the values from workspace do this:
value = zeros(3,1);
for j=1:3
value(j) = evalin('base',myvarname{j})
end

7 commentaires

Philippe
Philippe le 5 Déc 2012
If I understand you correctly, I need to change the %u manually in the Trace_1_1_%u_1 and rename my variables as myvarname? I would like to automate this process though...
Babak
Babak le 5 Déc 2012
Modifié(e) : Babak le 5 Déc 2012
I put myvarname{j} in a for loop where j goes from 1 to 3. Look up sprintf() command in MATLAB's help.
sprintf('Trace_1_1_%u_1',j)
automatically changes the index from 1 to 3.
Philippe
Philippe le 5 Déc 2012
Ok I think I managed to get your idea. I would like to add an extra parameter in you first loop. Since I have 3 * 58 datasets, which are going like this : Trace_1_1_1_1
Trace_1_1_2_1
Trace_1_1_3_1
Trace_1_2_1_1
Trace_1_2_2_1
Trace_1_2_3_1
...
Trace_1_58_3_1
What can I do with that, using the sprintf function?
Philippe
Philippe le 5 Déc 2012
Ok got it!
Philippe
Philippe le 5 Déc 2012
I don't understand the 2nd for loop. Matlab stores 3 times '0'.
Philippe
Philippe le 5 Déc 2012
Each Trace_1_i_j_1 represents thousands of data points and not a single value...
I am sorry, I made a mistake in the format of the assignin() function. I fixed the solution I'd written above (the second for loop).
Please clear assignin variable if you have created it in MATLAB's base workspace by mistake... you just need to do this once.. and use the assignin(.,.,.) the way I'm indicating above, or loop at MATLAB's documentation on assignin
doc assignin

Connectez-vous pour commenter.

Catégories

Question posée :

le 5 Déc 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by