String Character mixup on data types

Hey all, so I used sprintf to create a char. When I look at the workspace at it's class it shows as character however the value is 1x48 char. When I copy the output of that same item and set test= 'text' I see a class of character however the value is 'text' or 'text... (for longer strings).
Why are these not the same? What extremely simple thing am I forgetting in my tired state?
Thanks!
Can provide more input if needed!

7 comentarios

Karl - please show the code that you are using to do create the 1x48 array of characters, and the code that you are using to initialized test. If you are setting the latter as
test = 'text'
then it makes sense that the variable test takes on the string/character value of 'test' because you are assigning the latter (a string and not a variable) to the former. Why would you expect this to be the same as the 1x48 char array? Is text a variable? If so then you don't need to wrap it in single quotes in order to assign it to text.
Karl
Karl el 8 de Mzo. de 2015
Editada: Karl el 8 de Mzo. de 2015
So the first item was:
infoline = sprintf('\n%s\b %s%dnm %dgra %ds(%dx) %dFrame',header.date,header.ExperimentTimeLocal,header.SpecCenterWlNm,header.SpecGrooves,header.exp_sec,header.lavgexp,header.NumFrames);
The only thing I can think of is this is a mix of string and numbers. The second example is I copied the output of this and set it equal to a variable:
infoline2 = '01Aug2013 110137 674nm 1200gra 60s(5x) 1Frame';
This is apparently saved as a string.
The problem arises when I try to use this with the command title. If I use title(infoline) the title ends up being 01Aug2013. If I use title(infoline2) the title ends up being the entire string properly (01Aug2013 110137 674nm 1200gra 60s(5x) 1Frame).
So a bit perplexed.
Karl - infoline is a string as well (due to the sprintf). You may want to remove the \n and \b (?) from your sprintf command to get something like
infoline = sprintf('%s %s%dnm %dgra %ds(%dx) %dFrame',header.date,header.ExperimentTimeLocal,header.SpecCenterWlNm,header.SpecGrooves,header.exp_sec,header.lavgexp,header.NumFrames);
and try this with your call to title.
So been playing around with this problem a bit more. The problem seems to be related to using "%s" in sprintf. I tried not using \n \b which made no difference.
I however tried the full line omitting the first two values
(... = %dnm %dgra %ds(%dx) %dFrame',header.SpecCenterWlNm, ...
and everything works fine. I noticed that "%s" adds an extra space " " behind whatever the value is and I cannot remove it using stringtrim (strtrim). May play with things to see if I can get around the issue. Sadly it's those date/time strings that I really want.
Geoff Hayes
Geoff Hayes el 8 de Mzo. de 2015
Or maybe the problem lies with the header.date and header.ExperimentTimeLocal. Have you verified that both are of data type char (strings)?
Karl
Karl el 9 de Mzo. de 2015
Unsure how to check if a character type is a string. The closest I've found is ischar was true for both. Whos shows them as a 1x7 and 1x10 char.
Is there another command I'm not thinking of to use to check?
Karl - ischar is sufficient as is using class. What happens if you try the following to narrow down the problem
infoline1 = sprintf('%s',header.date);
infoline2 = sprintf('%s %s ',header.date,header.ExperimentTimeLocal);
infoline3 = infoline = sprintf('%s %s%dnm ',header.date,header.ExperimentTimeLocal,header.SpecCenterWlNm);
Do all produce reasonable strings? I can't help but wonder if there is a problem with one of your inputs (not a string, not an integer, etc.).

Iniciar sesión para comentar.

Respuestas (1)

Jan
Jan el 8 de Mzo. de 2015

0 votos

The code is correct in both cases. It is only the display in the command window which appears under certain conditions in the abbreviated form. The contents of the variable is a char string in both cases, so everything is fine.
If you post the code, which reproduces the problem, it would be easy to demonstrate what's going on.

1 comentario

Guillaume
Guillaume el 8 de Mzo. de 2015
And this is not particular to strings. For short vectors of anything, matlab displays the content of the vectors in the workspace browser. For longer vectors, it just displays their size.

Iniciar sesión para comentar.

Categorías

Más información sobre Environment and Settings en Centro de ayuda y File Exchange.

Preguntada:

el 8 de Mzo. de 2015

Comentada:

el 10 de Mzo. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by