MATLAB Answers

0

How to import data sequentially from different folders?

Asked by Nayeon Kwon on 15 Jun 2018
Latest activity Edited by Nayeon Kwon on 16 Jun 2018
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 Comments

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...
"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
Thank you very much. I got to understand how to use '(user) function' in matlab with your help.

Sign in to comment.

1 Answer

Answer by Walter Roberson
on 16 Jun 2018
 Accepted Answer

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 Comment

With a little correction, I got what I want and learned many concepts including cell/structure data types. Thank you!

Sign in to comment.