-1

Is there a way to convert matlab text output into an image file?

For example, if I have an output like this:

17    24     1     8    15
23     5     7    14    16
 4     6    13    20    22
10    12    19    21     3
11    18    25     2     9

Can I convert this into an image? Like taking a screen snapshot of this matrix area?

Thanks.

note: the result need to be aligned like the text output. i know how to align using different font. i was looking for an alternative way of aligning numbers. i was only asking for the possibility of taking a snap shot effect.

meizinder
  • 11
  • 1
  • i don't understand the negative check. is it because it is too simple?. if it is, at least provide an answer before checking negative. – meizinder Mar 05 '17 at 20:18

2 Answers2

2

You can use the msgbox function to display the matrix in a figure; the matrix has to be converted to string with the functino num2str, then you have to:

  • remove the OK button:
    • use the findobj function to get the handle of the pushbutton
    • delete it with the delete function
  • set the message box figure HandleVisibility to on (by default it is set to callback
  • use the function print to print the message box figure as an image

This is the implementation:

% Create the message box
x=msgbox(num2str(a),'Dump of matrix a')
% Set the message box figure HandleVisibility to on
x.HandleVisibility='on'
% Find the handle of the OK pushbutton
hp=findobj(x,'style','pushbutton')
% Delete the OK pushbutton
delete(hp)
% Print the figure
print -djpeg99 image_of_a_matrix

Edit to answer the commenrt about the alignment of the text

In the textbox represents the numbers are represented as a string; even if in the generation of the string you properly align the number, the representation of the string depends on the font selected.

If you use Monospaced font such as, for example, Courier, Courier New, Lucida Console you can obtain the dsesired alignment.

At the contrary, if you use proportional fonts you will lose the alignment since different characters occupies different amount of horizontal space.

With respect to the above proposed code, you can set the a Monospaced font (e. g. Courier) this way:

% Create the message box
x=msgbox(num2str(a),'Dump of matrix a')
% Set the message box figure HandleVisibility to on
x.HandleVisibility='on'
% Find the handle of the OK pushbutton
hp=findobj(x,'style','pushbutton')
% Delete the OK pushbutton
delete(hp)
% Get the handle of the text item
txt_h=x.Children.Children
% Change the text font
txt_h.FontName='Courier'
txt_h.FontWeight='bold'
% Print the figure
print -djpeg99 image_of_a_matrix_1

This is the image with Proportional font:

enter image description here

This is the image with Monospaced font:

enter image description here

Hope this helps,

Qapla'

il_raffa
  • 5,090
  • 129
  • 31
  • 36
  • thanks. but i was looking for an aligned result. numbers in your result are not aligned. – meizinder Mar 05 '17 at 18:59
  • I've updated the answer: the problem of the _alignement_ is due to the type of font. You can achieve it by using_Monospaced_ fonts such as _Courier_. – il_raffa Mar 05 '17 at 20:25
  • thank you very much. i am looking for alternative approach to get the aligned result, which is an image capture. but still, appreciate it. – meizinder Mar 05 '17 at 23:58
  • OK, I'm sorry not to have been of help, anyway, [you could try this](http://www.mathworks.com/matlabcentral/fileexchange/24323-screencapture-get-a-screen-capture-of-a-figure-frame-or-component) – il_raffa Mar 06 '17 at 11:12
1

You can use insertText function, to insert text into an image.

Here is my code sample:

A = [17    24     1     8    15
     23     5     7    14    16
      4     6    13    20    22
     10    12    19    21     3
     11    18    25     2     9];

[n_rows, n_cols] = size(A);

%Set background color to [240, 240, 240] (light gray).
I = zeros(n_rows*24+12, n_cols*10*5+8, 3, 'uint8') + 240; %Background color

text_str = cell(n_rows, 1); 
box_color = zeros(n_rows, 3) + 240; %Set box color to same as background color
text_color = repmat([125, 39, 39], n_rows, 1); %Set text color to brown.

%Fill rows of A into cell array text_str
for i = 1:n_rows
    text_str{i} = sprintf('%5d', A(i, :));
end

%Set position of each of the rows.
pos_x = zeros(1, n_rows) + 4;
pos_y = (0:n_rows-1)*24 + 6;
position = [pos_x', pos_y'];

%Insert text into image I.
I = insertText(I, position, text_str, 'FontSize', 14, 'BoxColor', box_color, 'TextColor', text_color,...
               'Font', 'Courier New Bold', 'BoxOpacity', 1);

figure;imshow(I);

Result:
enter image description here


Aligned text with different font:

Applying my solution for aligning text with no fixed width, requires different coordinate for each number (total of 25 coordinated and 25 strings).

The following solution uses Arial font:

A = [17    24     1     8    15
     23     5     7    14    16
      4     6    13    20    22
     10    12    19    21     3
     11    18    25     2     9];

[n_rows, n_cols] = size(A);
n_elms = numel(A);

%Set background color to [240, 240, 240] (light gray).
I = zeros(n_rows*24+12, n_cols*10*5+8, 3, 'uint8') + 240; %Background color

text_str = cell(n_elms, 1);
box_color = zeros(n_elms, 3) + 240; %Set box color to same as background color
text_color = repmat([125, 39, 39], n_elms, 1); %Set text color to brown.

%Fill rows and columns of A into cell array text_str
counter = 1;
for y = 1:n_rows
    for x = 1:n_cols
        text_str{counter} = sprintf('%5d', A(y, x));
        counter = counter + 1;
    end
end

%Set position of each of the rows and columns.
[pos_x, pos_y] = ndgrid(0:n_cols-1, 0:n_rows-1);
pos_x = pos_x(:)*10*5 + 4;
pos_y = pos_y(:)*24 + 6;
position = [pos_x, pos_y];

%Insert text into image I.
I = insertText(I, position, text_str, 'FontSize', 14, 'BoxColor', box_color, 'TextColor', text_color,...
               'Font', 'Arial Bold', 'AnchorPoint','RightTop', 'BoxOpacity', 1);

figure;imshow(I);

Result:
enter image description here

Rotem
  • 30,366
  • 4
  • 32
  • 65
  • thanks.but i was looking for an aligned result. the reason your result is aligned, is because your font is courier. if you change to another font, will it still be aligned? i was looking for an alternative way to using aligned font. – meizinder Mar 05 '17 at 19:03
  • Actually, I had a working solution without monospaced font, I just tried to make it more simple. Here is my previous post: [http://stackoverflow.com/questions/39516274/how-to-plot-with-specific-colors-the-values-of-a-matrix-in-matlab](http://stackoverflow.com/questions/39516274/how-to-plot-with-specific-colors-the-values-of-a-matrix-in-matlab) – Rotem Mar 05 '17 at 21:42