How to import data sequentially from different folders?

조회 수: 2 (최근 30일)
Nayeon Kwon
Nayeon Kwon 2018년 6월 15일
편집: Nayeon Kwon 2018년 6월 16일
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월 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일
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개)

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by