How to import data sequentially from different folders?
Ältere Kommentare anzeigen
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 Kommentare
per isakson
am 15 Jun. 2018
Bearbeitet: per isakson
am 15 Jun. 2018
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
am 15 Jun. 2018
per isakson
am 15 Jun. 2018
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
am 16 Jun. 2018
Bearbeitet: per isakson
am 16 Jun. 2018
per isakson
am 16 Jun. 2018
Bearbeitet: per isakson
am 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
am 16 Jun. 2018
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Workspace Variables and MAT Files finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
