How do I run a command only if certain characters exist in a string
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Sean Byrne
el 14 de Abr. de 2017
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
0 comentarios
Respuesta aceptada
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
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
Más respuestas (0)
Ver también
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!