# Reading a string value stored in an element of a matrix and compare it to a string

1 view (last 30 days)
Nicle Davidson on 4 Sep 2021
Commented: Walter Roberson on 5 Sep 2021
There are three strings such as :
A='sfdgkl';
B='yuiopt';
C='qwerty';
I permutate them and make a matrix of it.
Then put the elemnts of the matrix in variables aA, bB, and cC in a forloop, in the end, I need to compare the string left in aA, bB, cC with A,B,C and find out that for example if it is A which is stored in aA, or it is B or it is C, and the same about bB, and cC.
I try this:
To compare the value of aA with a string say 'A' as mentioned above
However I just get false or '0' values out of this comparison. I have tride strcmp, and isequel functions for matter.
An example could be
if isequal(aA, A)
disp('hello');
elseif isequal(bB, A)
disp('hello2');
elseif isequal(cC, A)
disp('hello3');
end
but
I only get a logical '0' out of this and the if-statement does not do what I'm looking for.
The only output is:
ans =
logical
0
however if I test:
if isequal('hello', 'hello')
disp('hello');
end
the output is a logical 1 or true:
ans =
logical
1
So I understand that the problem could be reading the value stored in the matrix element. But would there be any idea how to extract that value and be able to compare it with a string?
*If I write:
aA
I will get output the string which is stored in it, this might help to know the challenge better? So there is a string in aA and a string in A, but it looks like when I compare it does not compare the string inisde aA with A.

Walter Roberson on 4 Sep 2021
Using if does not set ans . ans is set only if you have a calculation that is not assigned to a variable. Also, there are a number of MATLAB functions that test whether any outputs are requested and if not then they do not emit any output. For example,
fprintf('hello\n')
hello
ans
abc = fprintf('hello\n')
hello
abc = 6
fprintf() formally speaking returns the number of characters emitted, but if you have not requested any output then it does not return anything.
Walter Roberson on 5 Sep 2021
At the point in your code where you do
if isequal(aA, A)
just before that add these debugging lines:
class(aA)
size(aA)
if ischar(aA)
fprintf('aA is character, might be able to proceed. Codes are:\n')
double(aA)
elseif isstring(aA)
fprintf('aA is string, might be able to proceed. Codes are:\n')
double(char(aA))
else
error('aA is unexpected class %s', class(aA))
end