Reading a string value stored in an element of a matrix and compare it to a string
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Nicle Davidson
am 4 Sep. 2021
Kommentiert: Walter Roberson
am 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.
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 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')
ans
abc = fprintf('hello\n')
fprintf() formally speaking returns the number of characters emitted, but if you have not requested any output then it does not return anything.
3 Kommentare
Walter Roberson
am 4 Sep. 2021
You said,
The only output is:
ans =
logical
0
However, the code you posted never changes ans . None of the operations you do in your posted code change ans . Therefore your problem is that you are displaying ans that was set some different way.
You do not show us what is in aA . You talk about it as having a "string", but that is not the same as showing us class(aA) and displaying aA .
My crystal ball is saying that when you implemented my code from https://www.mathworks.com/matlabcentral/answers/1446489-matlab-get-matrix-values-into-a-variable-in-for-loop#answer_780639 that you did not do
aA = P{q,1};
bB = P{q,2};
cC = P{q,3};
and that you instead did
aA = P(q,1);
bB = P(q,2);
cC = P(q,3);
and so aA and bB and cC are cell rather than character vector. When you isequal() a cell and a character vector, they will never be the same.
Walter Roberson
am 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
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!