
Why does my character vector show up as '1xN char' instead of the the actual characters in MATLAB Workspace and 'uitable' cell?
조회 수: 4 (최근 30일)
이전 댓글 표시
MathWorks Support Team
2019년 9월 19일
답변: MathWorks Support Team
2020년 12월 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
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:\nRefer to the following MATLAB Answers link for a more detailed discussion on the topic of erasing 'newline' characters from strings in MATLAB:
text1 = regexprep(text1,'[\n\r]+',''); % on windows (to include the carriage return)
text1 = regexprep(text1,'\n',''); % on linux and macOS
댓글 수: 0
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!