How ignore a string of characters?

I would like define a set of files by using their names which are string of characters, for example:
LNC-CAT_26_ *170613* _run_bada_01.mat
LNC-CAT_26_ *072612* _run_bada_01.mat
LNC-CAT_26_ *045612* _run_bada_01.mat
...etc.
But, I also would like to ignore a little characters string (in blod) in these filenames because these little strings change every time.
my first lines of command are :
source = 'C:\Users\ALG\Desktop\2012_fMRI_LNCCAT\LNC_CAT\ba-da';
direction = 'C:\Users\ALG\Desktop\behavior_analysis';
sujets = {'03','05','08','11','14','15','17','21','26'};
for numsubj = sujets
run1file = ['LNC-CAT_' cell2str(numsubj) '_' * '_run_bada_01.mat'];
etc....
What function or command should I write instead of the star (last line) in order to ignore the bold string?

댓글 수: 1

Jan
Jan 2014년 4월 28일
편집: Jan 2014년 4월 28일
You do not want to ignore the strings. You want to consider them, also you do not known them before. Because the names look like MAT-file names, I guess, that you want to import the file. What should happen with this file?
What is cell2str?

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

답변 (3개)

Roberto
Roberto 2014년 4월 28일

0 개 추천

if the length of the numbers is always the same, you can try parsing the string via the subscripts:
newStr = [oldstr(1:11) oldstr(18:end) ] ;

댓글 수: 1

Maxime
Maxime 2014년 4월 28일
ok thanks, yes the string I want ignore has always the same number of characters, but how can I implement this in my command line?

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

Roberto
Roberto 2014년 4월 28일

0 개 추천

I'm assuming you have this string like this, but I didn't understand what is it that you want to do, anyway here's some code to give you ideas:
oldStr = 'LNC-CAT_26_170613_run_bada_01.mat';
sujets = {'03','05','08','11','14','15','17','21','26'};
for i = 1: numel(sujets)
run1file1 = ['LNC-CAT_' sujets{i} '_run_bada_01.mat']
run1file2 = [oldStr(1:8) sujets{i} '_' oldStr(12:end)]
end
disp(run1file1);
disp(run1file2);

댓글 수: 1

Hello, Thank you for your response. and sorry for the late. Ok your program runs correctly. But it does not give what I wanted to do.
This is what I want to do : in a directory I want to select files by their number to read their content (matrices)
LNC-CAT_03_ 170613 _run_bada_01.mat
LNC-CAT_05_ 072612 _run_bada_01.mat
LNC-CAT_08_ 045612 _run_bada_01.mat
the number after LNC_CAT_ is the subject number, end this is with this number i want to select each file, but the string of six number inside the name of each file change every time.
after, in each file i want to select 2 matrices among 6, in order to merge it.
do you have a solution?

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

Roberto
Roberto 2014년 5월 6일
편집: Roberto 2014년 5월 6일

0 개 추천

I don't know if i really got it this time, is this what you want to do?
selDossier = uigetdir; % ask for the folder
cd(selDossier); % go to the folder
currentFile = dir ; % read folder's content
selectedFile = {} ;
selectedSubject = {} ;
selectedNumber = {} ;
for i = 1:numel(currentFile) % look for files
if ~currentFile(i).isdir % discard subfolders
if strcmpi(currentFile(i).name(1:8),'LNC-CAT_') %starts 'LNC-CAT_'
selectedFile{end+1} = currentFile(i).name ; %filename
selectedSubject{end+1} = currentFile(i).name(9:10); %Sub number
selectedNumber{end+1} = currentFile(i).name(12:17);
end
end
end
% now lets work with each selected file...
for i = 1: numel(selectedFile)
load(selectedFile{i}) ;
disp(['Selected Subject: ' selectedSubject{i}]) ;
disp([' Selected Number: ' selectedNumber{i}]) ;
% this depends on what you want to do!!!
end

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

질문:

2014년 4월 28일

편집:

2014년 5월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by