MATLAB Answers

How ignore a string of characters?

34 views (last 30 days)
Maxime
Maxime on 28 Apr 2014
Edited: Roberto on 6 May 2014
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 Comment
Jan
Jan on 28 Apr 2014
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?

Sign in to comment.

Answers (3)

Roberto
Roberto on 28 Apr 2014
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 Comment
Maxime
Maxime on 28 Apr 2014
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?

Sign in to comment.


Roberto
Roberto on 28 Apr 2014
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 Comment
Maxime
Maxime on 1 May 2014
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?

Sign in to comment.


Roberto
Roberto on 6 May 2014
Edited: Roberto on 6 May 2014
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

Community Treasure Hunt

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

Start Hunting!

Translated by