How to import data sequentially from different folders?

Hi, I am a beginner at matlab and I'd like to make a function file to analyze data from different folders.
First, I think I should use for-loop to import data sequentially from different folders. Each .txt file was made such as "DM01/DM01_study.txt" "DM02/DM02_study.txt"..... etc. and here's my code.
It seems filenames are generated intentionally but ERROR occurs in 'importdata' line. However, there was no problem when I wrote this.
study = importdata('DM_Behaviors/DM01/DM01_study.txt')
I would really happy if I could fix this problem. Thank you in advance.
study = dir('DM_Behaviors/DM*/*_study.txt');
for i = 1:3
filename = study(i).name;
temp_data = importdata(filename);
name = strsplit(filename, '.');
eval(sprintf('%s=temp_data', name{1,1}));
end

6 件のコメント

per isakson
per isakson 2018 年 6 月 15 日
編集済み: per isakson 2018 年 6 月 15 日
Most likely the error depends on the content of the text file. We cannot help without detailed information on that file. Why not attach (paperclip button) a sample file.
See
Nayeon Kwon
Nayeon Kwon 2018 年 6 月 15 日
Thanks I've just attached the file.
per isakson
per isakson 2018 年 6 月 15 日
On R2017b
>> cac = importdata( 'DM01_study.txt' )
cac =
struct with fields:
data: [37×4 double]
textdata: {'Trial' 'ObjID' 'FB' 'RT'}
colheaders: {'Trial' 'ObjID' 'FB' 'RT'}
>>
works fine; no problems. What error message do you get? And what release do you use?
Try
>> cac = importdata( fullfile( study.folder, study.name) )
Nayeon Kwon
Nayeon Kwon 2018 年 6 月 16 日
編集済み: per isakson 2018 年 6 月 16 日
Yes, but it doesn't work in the for-loop. I want to read txt files one by one (sequentially). as the picture I posted, the results showed just 'filenames' not the contents of the files...
per isakson
per isakson 2018 年 6 月 16 日
編集済み: per isakson 2018 年 6 月 16 日
"Yes, but it doesn't work in the for-loop." If you want a useful answer, you should really try to be more informative! You don't give us much of a chance to point out the mistakes you are making, e.g you didn't answer my questions: "What error message do you get? And what release do you use?".
I've done the following experiment successfully
  • Made four copies of the sample file, which you uploaded, and put them in different folders (R2017b,Win10)
>> sad = dir('h:\m\cssm\DM*\DM*.txt');
>> {sad.name}
ans =
1×4 cell array
{'DM01_study.txt'} {'DM02_study.txt'} {'DM03_study.txt'} {'DM04_study.txt'}
>> {sad.folder}
ans =
1×4 cell array
{'h:\m\cssm\DM01'} {'h:\m\cssm\DM02'} {'h:\m\cssm\DM03'} {'h:\m\cssm\DM04'}
>>
  • run the function, cssm. (See below)
>> DM = cssm()
DM =
struct with fields:
DM01_study: [1×1 struct]
DM02_study: [1×1 struct]
DM03_study: [1×1 struct]
DM04_study: [1×1 struct]
>> DM.DM04_study
ans =
struct with fields:
data: [37×4 double]
textdata: {'Trial' 'ObjID' 'FB' 'RT'}
colheaders: {'Trial' 'ObjID' 'FB' 'RT'}
>>
where
function DM = cssm()
study = dir(fullfile('h:\m\cssm\DM*\*_study.txt'));
for jj = 1 : length( study )
temp_data = importdata( fullfile( study(jj).folder, study(jj).name ) );
name = strsplit( study(jj).name, '.' );
DM.( name{1} ) = temp_data;
end
end
Nayeon Kwon
Nayeon Kwon 2018 年 6 月 16 日
Thank you very much. I got to understand how to use '(user) function' in matlab with your help.

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

 採用された回答

Walter Roberson
Walter Roberson 2018 年 6 月 16 日

0 投票

study = dir('DM_Behaviors/DM*/*_study.txt');
filenames = fullfile( {study.folder}, {study.name});
nfiles = length(filenames);
all_data_cell = cell(nfiles, 1);
basenames = cell(nfiles, 1);
for K = 1 : nfiles
filename = filenames{K};
[~, basenames{K}, ~] = fileparts(filename);
try
all_data_cell{K} = readtable(filename);
catch ME
fprintf('Warning: problem importing file "%s"\n', filename);
all_data_cell{K} = table();
end
end
all_data = cell2struct( all_data_cell, basenames, 2 );

1 件のコメント

Nayeon Kwon
Nayeon Kwon 2018 年 6 月 16 日
編集済み: Nayeon Kwon 2018 年 6 月 16 日
With a little correction, I got what I want and learned many concepts including cell/structure data types. Thank you!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeWorkspace Variables and MAT Files についてさらに検索

質問済み:

2018 年 6 月 15 日

編集済み:

2018 年 6 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by