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

조회 수: 4 (최근 30일)
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?

채택된 답변

MathWorks Support Team
MathWorks Support Team 2019년 9월 19일
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:

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Desktop에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by