loop for assigning columns from a textfile
3 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
dpb
on 7 Sep 2013
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 Comments
dpb
on 8 Sep 2013
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.
See Also
Categories
Find more on String Parsing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!