Nested function capability with non-nested functions for GUIs

조회 수: 4 (최근 30일)
D. Plotnick
D. Plotnick 2019년 7월 29일
댓글: TADA 2019년 8월 1일
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
Adam
Adam 2019년 7월 30일
편집: Adam 2019년 7월 30일
Well, if the class represents the GUI I would have thought it quite natural to extend the class with new (hard-defined) properties when you extend the GUI. Surely the GUI isn't permanently fluid? It may change a lot while you are developing it, but you aren't going to be constantly adding and removing components are you?
You add your fields to the GUI object in your current example, the only real difference is you have to add one line to a properties block if it is in a class, but on the plus side it is also easy to see exactly what properties a class has, unlike a struct where you have to search through the code to know what you added to it or look at it in a debug situation to see what its fields are.

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by