How to create a series of variables of data?
조회 수: 2 (최근 30일)
이전 댓글 표시
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
Stephen23
2015년 7월 16일
편집: Stephen23
2015년 7월 17일
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.
dpb
2015년 7월 16일
편집: Stephen23
2015년 7월 17일
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.
채택된 답변
dpb
2015년 7월 16일
What error(s), specifically are you getting (iow, what precisely what does "not working" mean?
It's somewhat simpler imo to use the dir solution instead for the filenames but whether there's where the problem lies or not isn't discernible w/o the error...
fileMask=[FileNamePattern '*.dta'];
d=dir(fileMask);
for i=1:length(d)
Data(i)=importdata(d(i).name, DELIMITER, HEADERLINES);
end
OK, I see I missed the problem initially; still it would be good to know the error but several things show up --
- Don't assign the vector 1:i to Data initially; it's going to become a structure and that reassignment triggers at least a warning and may error if have later version. Also, at that point i isn't defined in the code you've posted
- Don't use {} "curlies" for structure array indices; they're reserved for cell array content addressing
Those should resolve the problem I believe...
댓글 수: 0
추가 답변 (0개)
참고 항목
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!