How do I use a loop to isolate column vectors of a matrix into separate variables
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
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
dpb
on 4 Jun 2014
The answer is "Don't do that!!!"
See the FAQ for why it's a_bad_idea (tm) and alternatives...
7 Comments
George
on 4 Jun 2014
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.
dpb
on 4 Jun 2014
...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.
Joseph Cheng
on 4 Jun 2014
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?
dpb
on 4 Jun 2014
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 :)
Joseph Cheng
on 4 Jun 2014
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.
dpb
on 4 Jun 2014
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.
More Answers (0)
Categories
Find more on Annotations in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)