How to create a series of variables of data?
Show older comments
Hi, I need to create a lot of variables like these:
K01 = importdata('20150715-PowerDensity_60x60_Mesh300_28m_vs_K-1.dta', DELIMITER, HEADERLINES);
K02 = importdata('20150715-PowerDensity_60x60_Mesh300_28m_vs_K-2.dta', DELIMITER, HEADERLINES);
K03 = importdata('20150715-PowerDensity_60x60_Mesh300_28m_vs_K-3.dta', DELIMITER, HEADERLINES);
...
Kn = importdata('20150715-PowerDensity_60x60_Mesh300_28m_vs_K-n.dta', DELIMITER, HEADERLINES);
I tried the code below (considering http://www.mathworks.com/matlabcentral/answers/57445-faq-how-can-i-create-variables-a1-a2-a10-in-a-loop), but it's not working:
NumFiles=30; %Number of files used = number of points
FileNamePattern='20150715-PowerDensity_60x60_Mesh300_28m_vs_K-';
%%Import Files and save in a variable named "Data"
DELIMITER = ' ';
HEADERLINES = 9;
Data=1:i;
for i=1:1:numel(Data)
FileName=strcat(FileNamePattern, num2str(i), '.dta');
Data{i}=importdata(FileName, DELIMITER, HEADERLINES);
end
It's strange, because this is ok:
i=1
FileName=strcat(FileNamePattern, num2str(i), '.dta');
Test=importdata(FileName, DELIMITER, HEADERLINES);
These data have three parts:
Test.data: [90000x3 double]
Test.textdata: {9x3 cell}
Test.colheaders:{'theta_x' 'theta_y' 'P.Density'}
2 Comments
The first sentence of that page that you linked states "Please don't do this!". Dynamically named variables are a really bad idea and will make your own life miserable. Do yourself a favor and learn to code using more reliable, faster and much less buggy methods. In particular you could put all of these arrays into one cell array or structure... see dpb's for more information on how to do this.
Well, in actuality he did, Stephen. I had just come back as I had intended to compliment him on that step in the right direction but forgot to do so. His problem (and appears superficially to be the only one to my eye) is the use of the curlies to try to reference a structure array instead of a cell array.
Accepted Answer
More Answers (0)
Categories
Find more on Workspace Variables and MAT Files in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!