How do I run a command only if certain characters exist in a string

5 visualizaciones (últimos 30 días)
Hi
I'm trying to run a command that lets me open a file and then will run a process on that imported file depending on characters in the file name.
Say for example I open a file form a folder and then save that filepath as a string. I then want to run a process on it depending on the name of the file path.
The file names are TestMax, TestMin,TestMean
for i = int8(1:nUPSS)
[filename1, dir] = uigetfile('C:\Outputs','Select Trial');
load([dir filename1])
Therefore my trial name becomes either
filename1 = 'TrialMin.mat'
Or
filename1 = 'TrialMax.mat'
Or
filename1 = 'TrialMean.mat'
I then want to be able to run my next line of code depending on the characters in the string.
If filename1 contains Max...
do this
elseif filename1 contains Min...
do this
I hope this makes sense and any help would be much appreciated

Respuesta aceptada

Stephen23
Stephen23 el 14 de Abr. de 2017
Editada: Stephen23 el 14 de Abr. de 2017
Where fnm is the filename:
C = regexp(fnm,'M[a-z]+','once','match');
switch C{1}
case 'Min'
...
case 'Max'
...
case 'Mean'
...
otherwise
.... !!!
end
Note that it is highly recommended to always load into a variable (which happens to be a structure):
S = load(...);
  4 comentarios
Sean Byrne
Sean Byrne el 14 de Abr. de 2017
I See what you're saying so how would I go about getting around that? DO I need to create the matrix first for C before imputting the values 'min', 'mean' etc.
Also with using switch should it look something like this instead?
for i = int8(1:nUPSS)
[filename9, dir] = uigetfile('C:\Users\Sean\Desktop\Sean\PhD\Study 1\Outputs','Select WA Trial');
load([dir filename9])
C = regexp(filename9,'M[a-z]+','once','match');
switch C{1}
case 'Min'
WAMin_Combined(i,:) = WAVariablesDiscreteMin;
case 'Max'
WAMax_Combined(i,:) = WAVariablesDiscreteMax;
case 'Mean'
AMean_Combined(i,:) = WAVariablesDiscreteMean;
otherwise
disp('Could Not Find');
end
end
Final question the original expression
C = regexp(filename9,'M[a-z]+','once','match');
works well for matching with M but what if I have other names for example 'Range'
thanks alot for you help
Stephen23
Stephen23 el 14 de Abr. de 2017
Editada: Stephen23 el 14 de Abr. de 2017
"so how would I go about getting around that?" I don't know, because you did not say what you want to do, and you did not say if the arrays are the same size or different sizes.
"DO I need to create the matrix first for C before imputting the values 'min', 'mean' etc." Yes. Preallocating the size of the array would be a good idea. Or you could use a cell array (which should also be prellocated).
"using switch should it look something like this instead" Yes: using switch is not faster, but it shows the code intent much more clearly.
"what if I have other names for example 'Range'" It depends: how many names are there: 3, 10, or 1000? Do you know them in advance? You could do something like this (works for a few names):
C = regexp(filename9,'(M(ax|in|ean)|Range)','once','match');
PS: You could possibly avoid this whole thing by load-ing into a structure (which is highly recommended). Something like this:
>> trialMax = [1,2,3];
>> trialMin = [0,1,-1];
>> trialRange = [4,5,6];
>> save trialMax trialMax
>> save trialMin trialMin
>> save trialRange trialRange
>> clear
>> S = dir('*.mat');
>> Z = struct();
>> for k = 1:numel(S), tmp = load(S(k).name); fnm = fieldnames(tmp); Z.(fnm{1}) = tmp.(fnm{1}); end
>> Z.trialMax
ans =
1 2 3
>> Z.trialMin
ans =
0 1 -1
>> Z.trialRange
ans =
4 5 6
but it depends on how the data is arranged in the .mat files. To be honest saving each of min, max, etc in its own file make importing the data more complicated that it needs to be. Often what is intuitive and obvious for a human (e.g. a file "TrialMax.mat") is not the best solution for a computer. Good data design makes code simpler and more reliable: sadly data organization is often an afterthought, if at all.
In this case simply saving all of those variables in one .mat file would make the code much simpler:
>> trialMax = [1,2,3];
>> trialMin = [0,1,-1];
>> trialRange = [4,5,6];
>> save trialData trialMin trialMax trialRange
>> clear
>> S = load('trialData.mat');
>> S.trialMin
ans =
0 1 -1
etc

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Workspace Variables and MAT Files en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by