How to import data sequentially from different folders?

15 views (last 30 days)
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
per isakson
per isakson on 16 Jun 2018
Edited: per isakson on 16 Jun 2018
"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 on 16 Jun 2018
Thank you very much. I got to understand how to use '(user) function' in matlab with your help.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 16 Jun 2018
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
Nayeon Kwon
Nayeon Kwon on 16 Jun 2018
Edited: Nayeon Kwon on 16 Jun 2018
With a little correction, I got what I want and learned many concepts including cell/structure data types. Thank you!

Sign in to comment.

More Answers (0)

Categories

Find more on Cell Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!