matlab函数画图创建gui,如何在MATLAB中的函数内创建GUI?
build a GUI有多種方法,例如使用App Designer,GUIDE或以編程方式創建它(我將在下面說明此選項).了解GUI組件和
options available for sharing data between components的
different ways to define callback functions也很重要.
我偏愛的方法是使用nested functions作為回調.這是一個簡單的GUI作為示例:
function make_useless_button()
% Initialize variables and graphics:
iCounter = 0;
hFigure = figure;
hButton = uicontrol('Style', 'pushbutton', 'Parent', hFigure, ...
'String', 'Blah', 'Callback', @increment);
% Nested callback function:
function increment(~, ~)
iCounter = iCounter+1;
disp(iCounter);
end
end
運行此代碼時,每次按下按鈕時顯示的計數器應遞增1,因為嵌套函數增量可以訪問make_useless_button的工作區,因此可以修改iCounter.請注意,按鈕回調設置為function handle以遞增,并且此函數默認情況下必須接受兩個參數:觸發回調的UI組件的圖形句柄,以及關聯事件數據的結構.我們ignore them with the ~在這種情況下,因為我們沒有使用它們.
將上述方法擴展到您的特定問題,您可以添加循環并更改回調,以便將flag變量設置為false:
function make_stop_button()
% Initialize variables and graphics:
keepLooping = true;
hFigure = figure;
hButton = uicontrol('Style', 'pushbutton', 'Parent', hFigure, ...
'String', 'Stop', 'Callback', @stop_fcn);
% Keep looping until the button is pressed:
while keepLooping,
drawnow;
end
% Delete the figure:
delete(hFigure);
% Nested callback function:
function stop_fcn(~, ~)
keepLooping = false;
end
end
這里需要drawnow給按鈕回調一個機會來中斷循環內的程序流并修改keepLo??oping的值.
總結
以上是生活随笔為你收集整理的matlab函数画图创建gui,如何在MATLAB中的函数内创建GUI?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 矩阵方阵matlab,求助!!错误使用
- 下一篇: php 发布时间设计,php – 发布和