4

I'd like to plot a character matrix in Matlab, for example this matrix

M = ['test1','test2' ; 'test3','test4'];

Is there a simple way to implement it in Matlab?

Thank you

3
  • How would you like to plot the matrix? Write the character arrays in an axis at some (which?) locations, or something else? Commented Dec 20, 2015 at 18:00
  • Something very simple, just an image corresponding to the matrix M, with each letter in a case and no axes. Commented Dec 20, 2015 at 18:01
  • 2
    Check text. You can define a grid of coordinates and use a loop to add the letters to the figure Commented Dec 20, 2015 at 18:49

1 Answer 1

3

The text command allows you to plot multiple strings at once.

With your example:

M = {'test1','test2';'test3','test4'};

%// adjust x-multiplicator if text becomes very long
[xx,yy] = ndgrid((0:size(M,1)-1)*2+1,1:size(M,2));

figure,
th = text(xx(:),yy(:),M(:));
%// set additional properties, such as centering text horizontally and vertically
set(th,'horizontalAlignment','center','verticalAlignment','middle');

xlim([0 max(xx(:))])
ylim([0,max(yy(:))])
Sign up to request clarification or add additional context in comments.

2 Comments

It's very useful and simple ,thank you ! However, I wonder how to center this text on a grid ? (like in a table where the vertical and horizontal lines are visible)
the x,y coordinates for text normally anchor the lower right corner. you can use the property/values options to center the text on the specified point. (...,'horizontalalignment', 'center',...) text properties can be found at: gnu.org/software/octave/doc/interpreter/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.