MATLAB: How to asses whether a single Char is member of an Array of Chars

cell arrayscondtionsifindexingloopsMATLAB

I try to code an experiment. I have a Matrix with 4 Rows, where 1 row contains numbers from 1:20 which should be matched with a char Array {'B'…..'Z'} without vowels. I have several Conditions in FOR Loop for every Row:
trial = 1;
for trial = 1:20
Trial(trial).Setsize = TrialMatrix(trial,1);
Trial(trial).UpdatingSteps = TrialMatrix(trial,2);
Trial(trial).Match = TrialMatrix(trial,3);
Trial(trial).ProbeStim = TrialMatrix(trial,4);
if Trial(trial).Match == 0
Stims = Stims(Stims ~= Trial(trial).ProbeStim);
Trial(trial).EncodingStims = randsample(Stims,Trial(trial).Setsize+Trial(trial).UpdatingSteps);
clear Stims
elseif Trial(trial).Match == 1 && Trial(trial).UpdatingSteps == 0
cmpr0 = 0
while cmpr0 == 0
Trial(trial).EncodingStims = randsample(Stims,Trial(trial).Setsize+Trial(trial).UpdatingSteps)
cmpr0 = ismember(Trial(trial).ProbeStim,Trial(trial).EncodingStims)
end
elseif Trial(trial).Match == 1 && Trial(trial).UpdatingSteps == 1
cmpr1 =0;
cmpr2 =1;
while cmpr1==0 && cmpr2==1
Trial(trial).EncodingStims = randsample(Stims,Trial(trial).Setsize+Trial(trial).UpdatingSteps)
cmpr1 = ismember(Trial(trial).ProbeStim,Trial(trial).EncodingStims)
cmpr2 = strncmp(Trial(trial).ProbeStim,Trial(trial).EncodingStims,1)
end
elseif Trial(trial).Match == 1 && Trial(trial).UpdatingSteps == 2
cmpr2 =0;
cmpr3 =1;
while cmpr2 == 0 && cmpr3 == 1
Trial(trial).EncodingStims = randsample(Stims,Trial(trial).Setsize+Trial(trial).UpdatingSteps)
cmpr2 = strcmp(Trial(trial).ProbeStim,Trial(trial).EncodingStims);
cmpr3 = strncmp(Trial(trial).ProbeStim,Trial(trial).EncodingStims,2)
end
end
end
Especially the while loops are not working properly, the Conditions are violated. I want to compare, whether a letter is
  1. a.) Part of a Set of Letters
  2. b.) On which position of this Set (should not be on the first and the second position in some cases).
I tried this with strncmp and ismember, but I think this functions don't return a single true or false for my "Probe" but a vector, so that the loops quit incorrectly.
The Condtions for "NoMatch" and "Match" with no Updating are working fine, if I don't "convert" the sampled stimuli to chars like
% bla =(1:20);
% Stims = expinfo.letters(bla);
% Trial(trial).ProbeStim = expinfo.letters(Trial(trial).ProbeStim)
My question: are there functions for single value comparisons of chars (didn't found some) and how can I handle this char / numbers problem? Maybe you habe ideas, I'm kinda desperate.
greetings
Jan

Best Answer

"How to asses whether a single Char is member of an Array of Chars?"
"...single value comparisons of chars..."
Of course, just use ==:
>> 'C'=='ABCDE'
ans =
0 0 1 0 0
"with a char Array {'B'.....'Z'}"
Except what you shows us is not a char array, but is actually a cell array of char vectors (each of which appears to be a scalar char), and this is making your code more complex than it needs to be. You can trivially define a real char array like this:
'BCDFGH...Z'
and then the method I showed you will work. Using a cell array is likely a misdirection that complicates your code: if you only need an array of individual characters, then a simple char array/vector does the job perfectly.
You can easily use ismember to generate a character array of all non-vowel letters:
>> V = 'A':'Z'
V = ABCDEFGHIJKLMNOPQRSTUVWXYZ
>> V = V(~ismember(V,'AEIOU'))
V = BCDFGHJKLMNPQRSTVWXYZ
Related Question