2017-03-05 37 views
-1

有沒有辦法將matlab文本輸出轉換爲圖像文件?將字符輸出轉換爲MATLAB中的圖像

舉例來說,如果我有一個這樣的輸出:

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 

我可以轉換到這一點的圖像?像拍這個矩陣區域的屏幕快照 ?

感謝。

注意:結果需要像文本輸出一樣對齊。 我知道如何使用不同的字體對齊。我正在尋找一種 替代方式來對齊數字。我只是要求獲得快照效果的可能性 。

+0

我不明白否定的檢查。是因爲它太簡單了嗎?如果是,至少在檢查否定之前提供答案。 – meizinder

回答

2

您可以使用msgbox函數來顯示figure中的矩陣;矩陣必須被轉換成字符串與functino num2str,那麼你必須:

  • 刪除OK按鈕:
    • 使用findobj函數獲取按鈕的handle
    • 刪除它與delete功能
  • 設置消息框圖HandleVisibilityon(默認設置爲callback
  • 使用功能print打印消息框圖形作爲圖像

這是實現:

% 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 

編輯回答關於的比對的文本

在文本框中表示數字表示爲一個字符串;即使在字符串的生成中您正確對齊數字,字符串的表示也取決於所選的字體。

如果您使用Monospaced font,例如Courier,Courier New,Lucida Console,您可以獲取已使用的對齊方式。

相反,如果您使用比例字體您將失去對齊,因爲不同的字符佔用不同數量的水平空間。關於上面提出的代碼,可以設置一個等寬字體(例如,信使)這樣:

% 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 

這與比例字體圖像:

enter image description here

這與寬字體圖像:

enter image description here

希望這有助於

Qapla」

+0

謝謝。但我正在尋找一個對齊的結果。結果中的數字不一致。 – meizinder

+0

我已經更新了答案:_alignement_的問題是由於字體的類型。您可以通過使用_Monospaced_字體(如_Courier_)來實現它。 –

+0

非常感謝。我正在尋找替代方法來獲得對齊的結果,這是一個圖像捕獲。但仍然感激不盡。 – meizinder

1

可以使用insertText功能,將文本插入圖像。

這是我的代碼示例:

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); 

結果:
enter image description here


與不同的字體對齊文本:

申請我的解決方案用於與沒有固定的對齊文本寬度,每個數字需要不同的座標(共25個字符串和25個字符串)。

以下解決方案使用Arial字體:

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); 

結果:
enter image description here

+0

thanks.but我正在尋找一個對齊的結果。你結果的原因是一致的,因爲你的字體是快遞。如果您更改爲其他字體,它是否仍然對齊?我正在尋找一種使用對齊字體的替代方法。 – meizinder

+0

其實,我有一個沒有等寬字體的工作解決方案,我只是試圖讓它更簡單。這是我以前的帖子:[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