How do I use a loop to isolate column vectors of a matrix into separate variables

What I'm trying to do is strip the columns from an excel file into separate variables for analysis. I know how to do it if I know the number of columns, then I can do it manually. What i would like is to do it using a for loop. This is what I have so far:
data = xlsread(fullfile(path,file));
N = size(data,2);
M = size(data,1);
for i = 1:N
varname = strcat('data',num2str(i));
eval(sprintf('%s = %f',varname,data(:,i)));
end
any help would be greatly appreciated.
Thanks, George

 Accepted Answer

7 Comments

But I need to do that! I have an excel files full of data and they have to be broken into separate variables for analysis and simulation comparison and isolation. I'll read the faq though
I am sorry but no you don't. You could put them in a cell array. Accessing through an index is much simpler than dynamically creating names.
Please understand that it is a bad idea. Once you get it, it will save you a lot of headaches down the road.
...But ... I have ... files full of data and they have to be broken into separate variables for analysis...
That's the typical newcomer thought in Matlab but it's non-productive approach as the FAQ outlines. It's the way you've programmed in other languages, but Matlab is not one of those other languages. "When in Rome..."
Anyway, the other way to have your cake and eat it too is with the new-fangled data table datatype. It's only available in latest release or two, but if have a recent-enough version check it out.
Well what you have will work, but again without knowing why you have to (inherited some scripts?) it is hard to explain what you can change to not do this. But clearly see that where ever you need to analyze or use in simulation dataX (where X is that column) you can just use data(:,X).
or is the strcat('data'...) just a placeholder for specific variable names?
For what he's looking for the strcat part is ok; it's the building of the command for eval he's got an issue with (which clearly demonstrates the trickiness part of the recommendation to not proceed this way).
What he needs is to build the rest of the command as a string as if it were what would be on the command line; but I'm not going to write it here as it might convince him to do the dastardly deed instead of taking the other advice given :)
ah i see for the eval part. I quickly did it with a test with data=magic(10). and didn't use the sprintf. but i agree it isn't the best method. Only way i can think this applicable is if the columns are specific telemetry that may not be in the same order each time the file is created and the order is pulled from another file (ie header file). But again if you have the order you can use that to look at which section of the block of data. there is very limited cases where one needs to generate these unknown variables.
Generating the arrays isn't that big of a deal; it's the resultant mess of how is one then going to write robust maintainable code making use of them that is where the real rub lies.
Once he's got all those variables 'poofed' into the workspace, now he's got to dynamically continue to generate the names and eval every command; he can no longer just write generic code directly for almost anything.

Sign in to comment.

More Answers (0)

Asked:

on 4 Jun 2014

Commented:

dpb
on 4 Jun 2014

Community Treasure Hunt

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

Start Hunting!