
Why does my character vector show up as '1xN char' instead of the the actual characters in MATLAB Workspace and 'uitable' cell?
    4 views (last 30 days)
  
       Show older comments
    
    MathWorks Support Team
    
 on 19 Sep 2019
  
    
    
    
    
    Answered: MathWorks Support Team
    
 on 30 Dec 2020
            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?

Accepted Answer
  MathWorks Support Team
    
 on 19 Sep 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:\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 Comments
More Answers (0)
See Also
Categories
				Find more on Desktop in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!