I have a series of .txt tiles that all contain 3 columns; depth, temperature and salinity. I want to create a vector for depth, temperature and salinity for all of these files without individually writing out the names of each as there is a lot.
I tried to use the following script to accomplish this but get the error message 'index exceeds Matrix Dimensions'. Any help would be greatly appreciated.
files=dir('*.txt');
for i=1:length(files)
eval(['load ' files(i).name ' -ascii']);
end
Depth(:,1)=files(:,1);
Temp(:,1)=files(:,2);
Sal(:,1)=files(:,3);

댓글 수: 1

Jos (10584)
Jos (10584) 2014년 2월 19일
files is a variable holding the names of the text files, not the contents of it. This is causing the error message.

댓글을 달려면 로그인하십시오.

 채택된 답변

Jos (10584)
Jos (10584) 2014년 2월 19일

0 개 추천

Do not use eval!
files=dir('*.txt');
for i=1:length(files)
tempdata = load(files(i).name, '-ascii')
% now tempdata *only* holds the values from the last file.
% You want to store them for use, outside the for-loop.
% A convenient way is to use structures:
data(i).depth = tempdata(:,1) ;
data(i).temp = tempdata(:,2) ;
data(i).sal = tempdata(:,3)
end
Now, for instance,
allsal = cat(1,data(:).sal)
will give you all the salinity values of all files concatenated into a single vector

댓글 수: 3

Jos (10584)
Jos (10584) 2014년 2월 19일
편집: Jos (10584) 2014년 2월 19일
In addition, you can store the name of the data file in the structure as well, for later reference. Simply add
data(i).filename = files(i).name ;
inside the for-loop
Tom Wetherill
Tom Wetherill 2014년 2월 19일
It works but only for the last file in the directory. If i plot(data(i).temp,data(i).depth) it only plots a figure for the last file. Is there a way to plot a figure for each file at once?
At the end of the loop, i==length(files). If you want to plot the data of a all files, do:
for k=1:length(files)
figure(k)
plot(data(k).temp,data(k).depth); %kth file
end

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Adding custom doc에 대해 자세히 알아보기

질문:

2014년 2월 19일

댓글:

2014년 2월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by