help me not to use eval in a nested loop?
이전 댓글 표시
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
채택된 답변
추가 답변 (1개)
John BG
2015년 12월 8일
0 개 추천
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
Marwan Al
2015년 12월 8일
@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:
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
