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 个)

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 个评论

Hey, thanks so far, bur how do I reach that only the 3rd column from the txt-file is stored as a column in x? That's the crucial part:
z0 = complete_diagr_00(:,3);
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 的更多信息

提问:

2013-9-7

Community Treasure Hunt

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

Start Hunting!

Translated by