Borrar filtros
Borrar filtros

When I try to get a prompt to show a variable in it, it shows a random symbol instead?

4 visualizaciones (últimos 30 días)
For example, in my code [r,c] = size(Temp_Data) assigns r to be 366. However, when I try to prompt the user to enter a number between 1 and r using this code
input(['Please choose a day from 1 to ' c '\n'])
the r shows up as a Ů and if I use c in place of r I get instead. Anyone know why the number isn't showing?

Respuesta aceptada

Stephen23
Stephen23 el 10 de Feb. de 2018
Editada: Stephen23 el 10 de Feb. de 2018
pmt = sprintf(Please choose a day from 1 to %d\n',c);
day = str2double(input(pmt,'s'))
You assumed that by concatenating a numeric value c with some characters MATLAB would magically convert that number into a string representation of that number, whereas in fact MATLAB converts it into a character with that character code, e.g. if c was equal to 32 then you would get a space character. You can learn about character codes here:
If you want to get the string representation of a number then use one of the functions that convert to string, e.g. sprintf, num2str, etc.

Más respuestas (1)

Walter Roberson
Walter Roberson el 10 de Feb. de 2018
When you use [] (formally named horzcat) between a character vector and a numeric value, the result is defined to be the same as concatenating the character vector and char() of the numeric value.
char() of a decimal value is not the character representation of the decimal digits of the number: char() takes uint16() of the value and then says "stop intepreting this value as being a number and start interpreting it as a character". For example char(42) is not '42', it is '*' because the 43rd entry in the character tables is '*' (43rd because the counting starts from 0). char(366) is 'Ů' because that character is the 367'th entry in the character tables.
You should use
input( sprintf('Please choose a day from 1 to %d ', c) )
or, if you are using R2017a or later you can instead use
input( "Please choose a day from 1 to " + c )
Notice the double-quotes around the characters instead of single quotes. Double-quotes are used for string() objects, which are not character vectors. The + operator is defined between strings and numeric values as creating a printable representation of the numeric value and concatenating it with the string. However, this functionality is badly indexed, showing up only in the examples https://www.mathworks.com/help/matlab/ref/plus.html#btx3zvy-1, and there is no documentation about what format will be used for the converted numeric value, with the results being difficult to predict. So if you want any level of control about how the numeric value is to be represented, using sprintf() instead of string operators is much more robust.

Categorías

Más información sobre Data Type Conversion 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