strcmp isn't comparing properly

30 visualizaciones (últimos 30 días)
liu James
liu James el 2 de Jul. de 2017
Respondida: Jan el 5 de Jul. de 2017
I'm having trouble figuring out why the strcmp is providing an incorrect output when comparing PSymbol and symbol. The two cells are identical but when comparing strcmp(PSymbol,symbol) it should return a 1 and change location to 1 but it is not. Please help.
% Find the location in the table where symbols are matched
% Symbol is supposed to be unique in P
if strcmp(PSymbol,symbol)
location = i;
% If sides are opposite, flag as a closing transaction
if strcmp(side,oppSide1)
open = 0;
end
end
Ive attached the code and the .mat file. Please help.

Respuesta aceptada

Jan
Jan el 5 de Jul. de 2017
Of course strcmp works fine. If you do not get the expected result, the inputs differ from what you think they are. This can be examined using the debugger: Set a breakpoint in the line
if strcmp(PSymbol, symbol)
and check the variables:
class(PSymbol)
class(symbol)
Bother are cells as expected. But do they contain strings?
class(PSymbol{1})
class(symbol{1})
ans =
cell
ans =
cell
No, they contain cells. Therefore strcmp does not get strings or cell strings, but cells containing cell strings. This cannot work. An error message would be nice, but unfortunately strcmp replies false. Solution:
PSymbol = P.symbol{i}; % Curly braces
...
if strcmp(PSymbol, symbol{1})
Now both inputs are cell strings and strcmp replies true.

Más respuestas (1)

Image Analyst
Image Analyst el 2 de Jul. de 2017
If they're cell arrays, try ismember() instead of strcmp().
  1 comentario
liu James
liu James el 2 de Jul. de 2017
I tried this, but I get this error
Error using cell/ismember (line 34)
Input A of class cell and input B of class cell must be cell arrays of character vectors, unless
one is a character vector.
Error in Open (line 22)
if ismember(PSymbol,symbol)
In this case I don't get what's wrong cause both seems to be cell arrays with characters in it. and both are 1x1 so I would think they are both character vector. Am I missing something?

Iniciar sesión para comentar.

Categorías

Más información sobre Symbolic Math Toolbox en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by