MATLAB Answers

Nested function capability with non-nested functions for GUIs

조회 수: 3(최근 30일)
D. Plotnick
D. Plotnick 29 Jul 2019
댓글: TADA 1 Aug 2019
Hello all,
I am working on a GUI which started off rather small and simple, and so I used nested functions for handling the GUI itself. I am using the examples in the GUI Layout Toolbox as a guide. The code has ballooned, and from a simple organizational standpoint I would like to move the various sub-sections (individual panel creations, callbacks, etc.) into seperate *.m files. However, I also want maintain the ease-of-use from the nested functions.
As an example, another little random motion simulator. Here, I am handling the plotting and data calculations in nested functions. The data calculations (namely the motion speed) are dependent on values of the UI (the slider). During the loop, there is a check for data values which then changes the plotting parameters. In other words
  • Nested functions get values from the low-level GUI function (gui,data,state)
  • Nested functions set these values as well.
So, how can I retain this functionality while moving these sub-functions into their own files? Do I have to start returning the value of gui from each sub-function? Do I need to create a guiClass < handle so that I can modify the values directly from high level functions (possibly several layers deep)? Is there a way to migrate this away from nesting, without having to funamentally re-write the code? Is this any kind of way to run a railroad? Globals bad.
-DP
MWE:
function myGUI
gui = initializeGUI();
data = initializeData();
state = initializeState();
runLoop
function gui = initializeGUI()
gui.Window = figure('Name','Brownian Motion',...
'NumberTitle' , 'off',...
'MenuBar' , 'none',...
'ToolBar' , 'none',...
'HandleVisibility' , 'off' ...
);
gui.Panel = uipanel(gui.Window);
gui.Ax = axes('Parent',gui.Panel);
gui.Slider = uicontrol(gui.Panel,...
'Style','slider','Min',0.001,'Max',0.1,'Value',0.01);
% Do more initializing things
end
function data = initializeData()
data.sz = 100;
data.lim = 1.5;
data.x = randh(data.sz);
data.y = randh(data.sz);
end
function state = initializeState()
state.running = true;
end
function runLoop
plotData
while state.running
updateData
checkLims
refreshPlot
pause(0.02);
end
end
function plotData
gui.plot_h = plot(data.x,data.y,'.','Parent',gui.Ax);
axis(gui.Ax,[-data.lim data.lim,-data.lim,data.lim]);
drawnow;
end
function refreshPlot
set(gui.plot_h,'XData',data.x,'YData',data.y);
axis(gui.Ax,[-data.lim data.lim,-data.lim,data.lim]);
drawnow;
end
function updateData
spd = get(gui.Slider,'Value');
data.x = data.x + randh(data.sz)*spd;
data.y = data.y + randh(data.sz)*spd;
end
function checkLims
mx = max(abs([data.x;data.y]));
if mx > data.lim
data.lim = mx;
end
end
function v = randh(n)
v = 2*(rand(n,1) - 0.5);
end
end
  댓글 수: 5

댓글을 달려면 로그인하십시오.

답변(0개)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by