Why does my character vector show up as '1xN char' instead of the the actual characters in MATLAB Workspace and 'uitable' cell?

4 visualizaciones (últimos 30 días)
Consider the following MATLAB code:
f = figure;
uit = uitable(f);
text1 = ['ab cd ef g' newline];
text2 = 'ab cd ef gh';
text3 = '01234567891';
d = {text1,52,true;
text2,40,true;
text3,25,false};
uit.Data = d;
The 'newline' concatenated at the end of the char array 'text1' is to simulate a line of text being read in from a file.\n
Why does the variable 'text1' appear as 1X11 char in the MATLAB Workspace and the 'uitable' while 'text2' and 'text3' show up with the characters they contain?

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 19 de Sept. de 2019
This happens because of the presence of the 'newline' character in the character vector variable 'text1'. 
You can use the 'strtrim' function to get rid of the 'newline' characters at the start or end of the character vector.
This will display the contents of the character vector in the MATLAB Workspace and in the 'uitable' cell.
Hence, modifying the given code as follows fixes the issue:
f = figure;
uit = uitable(f);
text1 = strtrim(['ab cd ef g' newline]);
text2 = 'ab cd ef gh';
text3 = '01234567891';
d = {text1,52,true;
text2,40,true;
text3,25,false};
uit.Data = d;
Note that this will only work for 'newline' characters at the start and end of character vectors.
If you have 'newline' characters somewhere in the middle of your character vectors, you will need to use the 'regexprep' to search and replace the 'newline' chars.
This can be done as follows:\n
text1 = regexprep(text1,'[\n\r]+',''); % on windows (to include the carriage return)
text1 = regexprep(text1,'\n',''); % on linux and macOS
Refer to the following MATLAB Answers link for a more detailed discussion on the topic of erasing 'newline' characters from strings in MATLAB:

Más respuestas (0)

Categorías

Más información sobre Desktop en Help Center y File Exchange.

Productos


Versión

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by