help me not to use eval in a nested loop?

4 views (last 30 days)
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

Star Strider
Star Strider on 8 Dec 2015
I assume that ‘can’t save the output’ means that you’re overwriting your ‘data’ variable each time. I would use a cell array:
data{i,j} = xlsread( sprintf('Speed%d_Mvr%d.csv',S1(i),j));
  3 Comments
Star Strider
Star Strider on 8 Dec 2015
My pleasure.
I don’t know precisely what you’re doing, but if all your matrices are the same size, you can use the cell2mat function to convert each cell to a double matrix, and then use the cat function to concatenate them into a 3D array.
Specifically, in the loop, you can use M = cat(cell2mat(X),3); where ‘X’ is the 2D matrix read from your Excel files. You can then address every element of the resulting double matrix ‘M’ as you wish. (Name them whatever you wish.) You would have to keep track of which ‘page’ of the 3D matrix corresponds to a specific file, so I leave that to you to organise.
I apologise for the delay in responding. Today is ‘errand day’, and I was away for two hours. (Life intrudes!)

Sign in to comment.

More Answers (1)

John BG
John BG on 8 Dec 2015
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
Marwan Al on 9 Dec 2015
Edited: Stephen23 on 30 Jan 2016
Ok guys, kind of still stuck with this, @ John I didnt know how to attach names for elemets inside a cell, but to clarify my question, this is my current code using eval:
S= [204 242 4];
for i=1:3
for j=1:5
data = xlsread( sprintf('Dataset_0.%d_u%d.csv',S(i),j));
eval(['Speed' num2str(S1(i)) 'Mvr' num2str(j) ' = data;'])
end
end
the output is 15 entries in the Workspace likle in picture
How can I achive the(( very same outputs )) without using eval?? Thank you all
Stephen23
Stephen23 on 30 Jan 2016
Edited: Stephen23 on 30 Jan 2016
@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:

Sign in to comment.

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!