help me not to use eval in a nested loop?
Show older comments
Hi I have a requiremen to import 15 Excel files which I managed, but still cant save the output
files share the same name almost, as follows: Speed(X)_Mvr(Y), herer X is [0.214, 0.252, 0.500 ], and Y is 1 to 5, so the names are like: Speed0.214_Mvr1, Speed214_Mvr2,......Speed0.5_Mvr5 = total is 15 files.
To read the files I made a nested loop that uses "sprintf" function to read every thing using xlsread: code is:
S1= [0.214, 0.252, 0.5]; %these are my speeds
for i=1:3 %this loops my speeds
for j=1:5 %this loops my Mvr values
data = xlsread( sprintf('Speed%d_Mvr%d.csv',S1(i),j));
end
end
I prefer to have the data saved as 15 diffrent Matrices in my Workspace to use later with somesort of identification reagrding Speed value and Mvr value. this can be done with eval, but im not allowed to for this assignment. all ideas welcome
Thanks
Accepted Answer
More Answers (1)
John BG
on 8 Dec 2015
0 votes
Hi, regarding naming each matrix, try
B={};for i=1:1:10 B{i}=['Speed' char(i+48) '_Mvr' char(i+48)]; end
then attach each B(1) B(2) .. to each Excel extracted matrix
3 Comments
Marwan Al
on 8 Dec 2015
@Marwan Al: to achieve that you can't avoid eval because eval is the only way to dynamically assign variable names like that. However dynamically named variables are a really slow, buggy and awful way to program (that beginners think is wonderful), so whoever told you to avoid eval is doing you a favor.
The best advice is: do not use eval. Learn to program using faster and more reliable methods, exactly like in John's answer.
Read this to know why using eval is such a poor way to program:
Categories
Find more on Loops and Conditional Statements 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!