I am running an Ff statement and i Keep getting the error Matrix dimensions must agree... Help?

I am trying to run a statement that will run a specific command depending on the filename of the file I bring in.
The problem is the file names are different lengths eg. Min, Mean, Max.
So when I import the file name as a variable C == 'min' the first if statement works but then when it loops around to run the second else if C == 'mean' I get the error Matrix dimensions must agree. I assume this is becacuse I now have a 4 character string instead of a 3 character one as the elseif C == 'max' works.
Below is a copy of the code I'm working with. Please bare in mind I'm somewhat new to mablab :)
Thanks.
nX= length(X);
for i = int8(1:nX)
[filename9, dir] = uigetfile('C:\Outputs','Select WA Trial');
load([dir filename9])
C = regexp(filename9,'M[a-z]+','once','match');
if C == 'Max'
WAMax_Combined(i,:) = WAVariablesDiscreteMax;
elseif C == 'Min'
WAMin_Combined(i,:) = WAVariablesDiscreteMin;
elseif C == 'Mean'
WAMean_Combined(i,:) = WAVariablesDiscreteMean;
end
end

Answers (1)

Method one: use strcmpi:
if strcmpi(C,'Max')
...
elseif strcmpi(C,'Min')
...
.. etc
...
end
Method two: use switch:
switch C
case 'Max'
...
case 'Min'
...
case 'Mean'
...
otherwise
...
end
Personally I think switch is the better solution, because it clearly indicates the purpose of the code.

2 Comments

@Sean Byrne: I'm glad to help. Please remember to accept the answer that best resolves your original question: that is an easy way for you to show your appreciation, and that the question has been resolved.

Sign in to comment.

Asked:

on 19 Apr 2017

Commented:

on 19 Apr 2017

Community Treasure Hunt

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

Start Hunting!