【问题标题】:I want to display the results shown in command window by clicking push button我想通过单击按钮显示命令窗口中显示的结果
【发布时间】:2016-05-01 16:37:16
【问题描述】:

我想通过单击按钮在命令窗口中显示结果。

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

现在我正在使用 matlab gui 制作一个界面,并希望通过单击按钮在文本框中显示该结果。为此,我在 gui 中调用此函数,但在命令窗口中获取结果。

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

如何将结果从命令窗口重定向到 GUI 文本框?

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

【问题讨论】:

    标签: matlab user-interface command-line matlab-figure matlab-guide


    【解决方案1】:

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

    可能的替代方案是:

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

    您可以使用 tempname 创建一个唯一的文件名

    在过程结束时,您可以删除带有delete的临时日记文件。

    如果要将已删除的文件移动到recylce文件夹中,应设置recycleon

    您可以在下面找到该过程的可能实现;对此,我创建了一个“虚拟”NPCR_and_UACI 函数并“评论”了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')
    

    希望这会有所帮助。

    卡普拉'

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多