Hello,
I just realized a program that allows to load data files from a folder. These data files are all different names but I would group them into three groups in matlab. A group 1 whose file names begin with 'X', a group 2 whose file names begin with 'Y' and a group 3 whose file names begin with 'Z' but I do not know how. Do you have any idea? with strcmp?

 채택된 답변

Alexandre Williot
Alexandre Williot 2015년 4월 22일

0 개 추천

this is what I needed,
contenu = dir('C:\Users\alexandre\Dropbox\UQTR 2013 - 2015\Matlab\MatLab EPK6064\Fichiers_Travail2');
for file = 1:length(contenu)
Fichiers_Travail2 = contenu(file).name;
if strcmp(Fichiers_Travail2,'.')==0 && strcmp(Fichiers_Travail2,'..')==0 && strcmp(Fichiers_Travail2(1,1),'e')==0
load(Fichiers_Travail2)
if strcmp(Fichiers_Travail2(1,1),'S')==1
code_Groupe = 1;
elseif strcmp(Fichiers_Travail2(1,1),'C')==1
code_Groupe = 2;
elseif strcmp(Fichiers_Travail2(1,1),'L')==1
code_Groupe = 3;
end
end
end

추가 답변 (3개)

pfb
pfb 2015년 4월 17일

0 개 추천

It's not very clear to me what you mean by "group the files in matlab".
Anyway, one possibility is perhaps to discriminate them before loading them. I suppose you use dir to make a list of the files to be loaded. Why don't you use something like
dirX = dir('X*');
dirY = dir('Y*');
dirZ = dir('Z*');
or something to that effect? I mean, you might have to refine the string you feed to dir(), possibly adding a path or using some more information about the file names.
Anyway, now dirX, dirY and dirZ are structures containing the names of the files starting with the desired letter.
Image Analyst
Image Analyst 2015년 4월 17일

0 개 추천

It is all there in the FAQ. Code samples and everything. Adapt as needed: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
Image Analyst
Image Analyst 2015년 4월 21일

0 개 추천

Of course, because you're passing the whole structure:
file_name = contenu(file).name;
load(contenu) % Tries to load the entire structure of all filenames.
Instead, try to load only one filename:
file_name = contenu(file).name;
load(file_name) % Tries to load only one single filename.

댓글 수: 4

Of course ! I am a beginner, thank you very much.
Now I have this message for the code below, have you an idea please? It's probably easy but ... not for me.
??? Error using ==> strcmp
Not enough input arguments.
Error in ==> exercice2__a_williot at
20
if strcmp(contenu(file).name,'S')==1
the code
contenu = dir('C:\Users\williot\Dropbox\UQTR 2013 - 2015\Matlab\MatLab EPK6064\Fichiers_Travail2');
for file = 1:length(contenu)
if strcmp(contenu(file).name,'.')==0 && strcmp(contenu(file).name,'..')==0
file_name = contenu(file).name;
load(file_name)
end
end
%--étape2--identifiez chaque groupe de personne-------
if strcmp(contenu(file).name,'S')==1
code_Groupe = 1;
elseif strcmp(contenu(file).name,'C')==1
code_Groupe = 2;
elseif strcmp(contenu(file).name,'L')==1
code_Groupe = 3;
end
file is no longer defined because the for loop over "file" has ended by the time you call your next if block in etape2. I guess you probably want it INSIDE your for loop, not outside.
Yes, thank you!
By the way, you were supposed to accept my answer, not post your code and accept it.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by