How can I change variable names in a for loop?
Show older comments
Hi,
I have exported some simulation results from another software to MATLAB. Now I have 27 variables with names such as vN1a,vN2a....vN27a. I am trying to get max value of each variable using for loop. The problem is I don't know how I can change the numbers in the names of my variables. Some people use eval function, but here the case is a bit different.
Here is my code
clc,clear
format short g
v_max = zeros(27,1);
load('Filename.MAT');
for x=1:1:27
v_max(x,1) = v_max(x,1) + max(vN1a); % I tried max(vNxa), but it didn't work at all.
end
v_max
any suggestions?
5 Comments
per isakson
on 27 Jan 2016
Edited: per isakson
on 27 Jan 2016
Do it without eval! Hint:
A = 1;
B = 2;
C = 3;
save
outputs
Saving to: h:\m\cssm\matlab.mat
Next
S = load()
outputs
Loading from: matlab.mat
S =
A: 1
B: 2
C: 3
Finally
field_name_list = permute( fieldnames( S ), [2,1] );
for name = field_name_list
disp( max( S.(name{:}) ) )
end
outputs
1
2
3
Khalid Khawaja
on 27 Jan 2016
the cyclist
on 27 Jan 2016
Note that there is still some trickiness here, and I am not entirely sure that the algorithm using the structure does exactly what you want.
DISCLAIMER: I don't want you (or anyone!) to think I am advocating the use of eval in general. It is evil, for all the reasons described here.
BUT ... I don't yet see another solution for your specific problem.
Namely, you still need to get the values of vN1a, vN2a, ... into vmax -- in the correct (numerical) order.
There are two potential issues that I see with your case. There are likely workarounds, but it's good for you to be aware.
The first is that you might need to be careful if, in addition to the vN*a variables, you also have other ones in the *.mat file, then at some point you are going to need to filter those out, so that you do not include those in the vmax vector you want to create.
Even if that is not the case, there may be a second issue. Note that the fieldnames in the structure are going to be sorted alphabetically -- not numerically. So, if you use the for loop as in per isakson's nice solution, your vmax elements are not going to be in numerical order.
There are likely ways around these intricacies, so just be aware. Yet another reason to see if there is a way to go upstream and fix the original naming problem!
@the cyclist: I have already resolved both of these "issues" in my comments to your answer. Briefly:
- the fieldnames were specified using sprintf, thus only the desired fields were chosen.
- the fieldnames are generated in a loop, and the loop variable can be chosen to be in any order.
This neatly resolves both of these "issues". It should be noted that you also used sprintf in your Answer, to achieve exactly these aims (specific variables & sequence), but with slow, buggy eval instead of fast and neat fieldnames. There is absolutely no "trickiness" involved, just some fast, simple, reliable code without using eval.
the cyclist
on 27 Jan 2016
Oh, sweet! Sorry for the redundant comment because I missed that.
Accepted Answer
More Answers (1)
Walter Roberson
on 27 Jan 2016
Vs = {vN1a,vN2a....vN27a};
Now you can loop
for K = 1 : length(Vs)
vmax(K) = max(Vs{K});
end
1 Comment
Khalid Khawaja
on 27 Jan 2016
Categories
Find more on Scope Variables and Generate Names in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!