Info

This question is closed. Reopen it to edit or answer.

saving an array with prefix and a variable index to a loop read

1 view (last 30 days)
for j=109:110
% create a filename for loop read
ExcelFileName = sprintf('%d_IF.csv', j);
ExcelSheetName = sprintf('%d_IF',j);
Mod_IF = xlsread(ExcelFileName,ExcelSheetName,'B22:B100021');
%
end
% what I want to do is read in 109_IF.csv, 109_IF sheet from B22 to B100021
% As far as I can tell that is occuring because I can look at Mod_IF and
% it has the correct values.
%
% I would like to save the data into Mod_IF_109 on the first loop
% and then save the data on the second read into Mod_IF_110 and so on
%
% I have not found a way to do that, I have tried eval, S., newname
% and so on, but each time I tried to access the data outside the
% loop an error occurs that says function or variable Mod_IF_109 is undefine
% and I do not see Mod_IF_109 array in the workspace being created.
%
% Looking at the FAQs and the other have, the solutions are close
% but I have not been able to get them to get past the error, in fact
% the above came from a FAQ but the FAQ did not elaborate on how to
% save the data into an array that is identified by the j value.
%
% Using a multi-column array is NOT an option i.e (10000x(number of
% reads in the loop) for various reasons
% Thanks for any advance
%Regards
%Philip
  4 Comments
dpb
dpb on 10 Jul 2014
...but the FAQ did not elaborate on how to save the data into an array that is identified by the j value.
And there is a very good reason for that--as you have learned, that way "there be dragons". Once you go that route you're then at the mercy of eval to do anything and trust the folks who wrote the FAQ, you do NOT want to travel that route unless there really is an overriding(*) reason.
(*) And almost never is the reason truly sufficient to not make one of the other solutions as outlined in the FAQ far superior.
Philip
Philip on 10 Jul 2014
Thanks for the feedback, the following syntex looks like it worked
for j=109:110
ExcelFileName = sprintf('%d_IF.csv', j);
ExcelSheetName = sprintf('%d_IF',j);
Mod_IF = xlsread(ExcelFileName,ExcelSheetName,'B22:B100021');
eval(['Mod_IF_',num2str(j),' = Mod_IF'])
end
The Physics Forum solution lacked the , between num2str(j)
Thanks for everyones comments Regards, Philip

Answers (0)

Community Treasure Hunt

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

Start Hunting!