我想通过单击按钮显示命令 window 中显示的结果

I want to display the results shown in command window by clicking push button

我想通过单击按钮显示命令 window 中显示的结果。

我的意思是,我创建了一个函数,当我 运行 该函数时,结果显示在 matlab 命令中 window。

现在我正在使用 matlab gui 创建一个界面,并希望通过单击按钮在文本框中显示该结果。为此,我在 gui 中调用了这个函数,但在命令 window .

中得到了结果

包含数字和单词的结果(大约 5 行)

如何将命令 window 的结果重定向到 GUI 文本框?

function pushbutton4_Callback(hObject, eventdata, handles) 
global E1; 
global E2; 
results=NPCR_and_UACI(E1,E2);

最简单的方法是直接在 "NPCR_and_UACI" 函数中构建字符串,将其设置为函数的附加输出,然后将其分配给静态文本框。

一个可能的选择是:

  • 使用 diary
  • 在文本文件上激活输出记录
  • 逐行读取 diary 文件
  • 将行存储在 cellarray
  • 将cellarray设置为静态文本框的string

您可以使用 tempname

创建一个唯一的文件名

进程结束时,您可以用delete删除临时日记文件。

如果要移动recylce文件夹中删除的文件,需要设置recycle on.

在下面您可以找到该过程的可能实现;为此,我创建了一个 "dummy" NPCR_and_UACI 函数和 "commented" global varaibles.

function pushbutton4_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% global E1; 
% global E2; 
E1=1;
E2=2
results=NPCR_and_UACI(E1,E2);
% Create a unique filename
tmp_file=tempname
% Activate diary recording
format long
diary(tmp_file)
% Display the output on the Command Window
disp(results)
% Turno disry off
diary off
% Open the diary file
fp=fopen(tmp_file)
% Initialize the string
res_string={};
% Initialize the row counter
cnt=0;
% Read the diary file line by line
while 1
   tline = fgetl(fp);
   if ~ischar(tline)
      break
   end
   % Increment the rwo counter and store the current line
   cnt=cnt+1;
   res_string{cnt}=tline
end
% Close the diary file
fclose(fp)
% Enable moving the file to the recycle folder
recycle('on')
% Delete the diary file (move it in the recycle folder
delete(tmp_file)
% Setr the result string in the statictext box
set(handles.text2,'string',res_string,'horizontalalignment','left','fontname','courier')

希望这对您有所帮助。

Qapla'