How to load all the data in one folder

조회 수: 51 (최근 30일)
Sikai Wu
Sikai Wu 2021년 2월 19일
편집: Stephen23 2021년 2월 19일
Path = 'D:\50-350\';
File = dir( fullfile(Path,'*.dat'));
FileNames = {File.name}';
Length_Names = size(FileNames,1);
for i=1:Length_Names
filename=strcat(Path, FileNames(i));
eval(['Data',num2str(i),'=','load(filename{1,1})',';']);
end
I have 3 targets here:
1、Find out all the files with '.dat' in folder '50-350';
2、Read the data;
2、Rename the data from ‘Data1 Data2 Data3 Data4...’ one by one;
By the codes above, I successfuly achieve the goals, but I find that the size of the data change.
For example: I load the ' 31.dat ' by the above codes, and rename it as ' Data31'. The size of Data31 is 3353×10 . If I read '31.dat' directly, the size is 2501×10. I am wonder why it happened.
  댓글 수: 2
Sikai Wu
Sikai Wu 2021년 2월 19일
for i=1:Length_Names
% filename=strcat(Path, FileNames(i));
% eval(['Data',num2str(i),'=','load(filename{1,1})',';']);
filename=[num2str(i),'.dat'];
chr=[Path filename];
Data{i}=load(chr);
end
I change the code as above. It seems goos. I am not sure whether it is correct.
Stephen23
Stephen23 2021년 2월 19일
편집: Stephen23 2021년 2월 19일
For robustness you should preallocate Data before the loop.
Also fullfile is recommended instead of string concatenation.
You will probably find that the files are not imported in numeric order: to get numeric order based on numbered filenames (without leading zeros, as your example shows) you will need to do either of these:
  • sort the names alphanumerically.
  • generate the filenames (rather than using DIR), as shown here:

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

답변 (1개)

Stephen23
Stephen23 2021년 2월 19일
편집: Stephen23 2021년 2월 19일
"I am wonder why it happened."
Because you always load exactly the same file data (note the indexing you used):
filename{1,1}
Your code always loads the first file and ignores all the other files.
In any case your approach is not robust. Here is a simpler and more robust way to import that data:
P = 'D:\50-350\';
S = dir(fullfile(P,'*.dat'));
for k = 1:numel(S)
F = dir(fullfile(P,S(k).name));
S(k).data = readmatrix(F); % much better than EVAL and LOAD.
end
Depending on how the files are named, you might also find this useful:
  댓글 수: 2
Sikai Wu
Sikai Wu 2021년 2월 19일
Thanks for you help. My software is 2017a. 'readmatrix' is expanded in 2019a.
Stephen23
Stephen23 2021년 2월 19일
For 2017a you can try dlmread or cvsread or textscan or whatever suits your file format.

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

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by