How to import data from multiple files into MATLAB?

I have multiple text files that I need to import into MATLAB so that I can analyze the data. Here is what I have so far:
%%import data
x = importdata('1.txt'); %creates x.data and x.textdata
z1 = x.data(:,5); %grabs column 5 from x.data
z2 = x.data(:,6); %grabs column 6 from x.data
%%obtains value closest to 0 in column 'z2'
numb=0;
[~, imin] = min(abs(z2 - numb));
z2(imin)
plot(z1,z2)
How can I import multiple files to run the same operation on every text file? I've tried load(), however, it doesn't seem to work.

 採用された回答

Walter Roberson
Walter Roberson 2018 年 6 月 5 日

0 投票

7 件のコメント

Asher Zaidi
Asher Zaidi 2018 年 6 月 5 日
編集済み: Asher Zaidi 2018 年 6 月 5 日
That seems to work. It gives me a struct array.
textfiles = dir('*.txt')
for file = textfiles'
fprintf(1,'Doing something with %s\n',file.name)
end
Result:
2×1 struct array with fields:
name
folder
date
bytes
isdir
datenum
Doing something with 1.txt
Doing something with 2.txt
Then if I use "textfiles.name" it shows me the name of the text files, but how do I access them?
textfiles.name
ans =
'1.txt'
ans =
'2.txt'
Walter Roberson
Walter Roberson 2018 年 6 月 5 日
{textfiles.name} would be a cell array of the names.
Asher Zaidi
Asher Zaidi 2018 年 6 月 5 日
How would I go about accessing the data within those files?
Walter Roberson
Walter Roberson 2018 年 6 月 5 日
textfiles = dir('*.txt');
filenames = {textfiles.name};
numfiles = length(filenames);
cmap = jet(numfiles);
L = gobjects(numfiles, 1);
alldata = cell(numfiles, 1);
for K = 1 : numfiles
thisfile = filenames{K};
thiscolor = cmap(K,:);
[~, thisbase, ~] = fileparts(thisfile);
x = importdata( thisfile );
alldata{K} = x;
z1 = x.data(:,5); %grabs column 5 from x.data
z2 = x.data(:,6); %grabs column 6 from x.data
%%obtains value closest to 0 in column 'z2'
numb=0;
[~, imin] = min(abs(z2 - numb));
L(K) = plot(z1, z2, 'LineColor', thiscolor, 'DisplayName', thisbase);
hold on
plot( z1(imin), z2(imin), '*', 'LineColor', thiscolor);
end
hold off
legend(L);
Asher Zaidi
Asher Zaidi 2018 年 6 月 5 日
Very helpful! Thank you!
hanadi abbas
hanadi abbas 2019 年 5 月 25 日
I'm used the following codes for loading files , but how can find length of files
for i=1:3
dd1(i)=load(['D:/train/tttt' num2str(i) '.mat'])
end
Walter Roberson
Walter Roberson 2019 年 5 月 25 日
Are you asking what the file size is for each .mat file? That can be found by looking at the 'bytes' field returned by dir()
Are you asking how many rows there are in the data you loaded? If so then you would use something like,
for i = 1 : 3
lens(i) = structfun(@(M) size(M,1), dd1(i), 'uniform', 0);
end
The output, lens would be a non-scalar structure each of which had one field for each variable name in the .mat file, with the content of the field being the number of rows that was stored in that variable in that .mat file. (With the given information, I cannot assume that there is only one variable in each file. I can, though, assume that the files each have the same variable name in the same order, as otherwise the assignment to dd1(i) would fail in your code.)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Import and Analysis についてさらに検索

製品

リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by