loop for assigning columns from a textfile
이전 댓글 표시
hello all,
I have 10 txt-files containing data in several columns and want to turn this simple code for a single file into a loop for all 10:
load complete_diagr_00.txt
z0 = complete_diagr_00(:,3);
Z0 = reshape(z0,19,39);
figure
pcolor(Z0);
I know how to do this for the load-part, but not the assignment-part z0=...
for k=0:9
load (['complete_diagr_0' num2str(k) '.txt']);
(???)
end
I thought of making z a matrix and assigning columnswise, but it didn't work, so I would be grateful for any hints.
답변 (1개)
dpb
2013년 9월 7일
Use a return variable...
x=load(['complete_diagr_00.txt']); % load first to get size...
x=[x zeros(length(x),9)]; % allocate room for rest...
for k=1:9 % and put rest in proper columns
x(:,k)=load(['complete_diagr_0' num2str(k) '.txt']);
end
This presumes that all are the same length and oriented as column vector.
If of varying length will have to use cell array instead and if row vector will need to transpose the result of the load to reorient...
댓글 수: 2
Anna
2013년 9월 8일
dpb
2013년 9월 8일
Well, I've sorta' lost what it was you were after but just use a temporary first if using load
t=load(['complete_diagr_00.txt']); % load first to get size...
x=zeros(length(x),10); % allocate room ...
x(:,1)=t(:,3); % store first column
for k=1:9 % and put rest in proper columns
t=load(['complete_diagr_0' num2str(k) '.txt']);
x(:,k)=t(:,3); % store in proper column
end
Or, if only using the single column perhaps simpler to use textread or textscan and use the '%*' form of a format field to skip the first two columns and then the remainder of the record to only read the single column of data in.
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!