Understanding and using guihandles programmatically

8 visualizzazioni (ultimi 30 giorni)
Hi
I am programmatically creating a GUI interface with a number of plots that connects to an outside function in the callback. The outside function creates new values for the plots and I want to be able to update the plots from that function (the part of the function that updates the plots is run in a timer). I am confused by guidata and guihandles and how to approach this. In another program I tried just passing in the axes handle and that didn't work anyway. This time I have multiple axes to update so I'd prefer to pass them all in in one object if I can.
I created a figure and the myHandles reference, and I can see when I add tags to my uicontrols that they are stored in myHandles. I presume I pass myHandles to my outside function, but I'm not sure how to update them from that function so the graph is updated.
f = figure;
myhandles = guihandles(f);
Thanks

Risposta accettata

Jan
Jan il 7 Mag 2016
function CreateFigure
handles.FigH = figure('Tag', 'myFigure';
handles.axes1 = subplot(2,2,1);
handles.axes2 = subplot(2,2,2);
handles.axes3 = subplot(2,2,3);
handles.axes4 = subplot(2,2,4);
handles.Button = uicontrol('Style', 'PushButton', ...
'Callback' {@myCallback, handles.FigH});
... % And the other uicontrols
guidata(FigH, handles); % Store handles struct in the figure
Now it depends how you call the "external function". If they are called from buttons or menus inside the figure, provide the figure handle as shown for the button's callback:
function myCallback(ButtonH, EventData, FigH)
externalFunction(FigH)
Then you can get the handles in the external function:
function externalFunction(FigH)
handles = guidata(FigH);
plot(1:10, rand(1, 10), 'Parent', handles.axes1);
But then the calculations and the GUI are mixed. Prefer to leave the GUI parts and the calculations separated:
function myCallback(ButtonH, EventData, FigH)
Data = externalFunction;
handles = guidata(FigH);
plot(Data.x, Data.y, 'Parent', handles.axes1);
And the clean external function:
function Data = externalFunction()
Data.x = 1:10;
Data.y = rand(1, 10);

Più risposte (1)

Rebecca Prescott
Rebecca Prescott il 7 Mag 2016
Modificato: Rebecca Prescott il 7 Mag 2016
Thanks Jan
I'll try that out once I've got some other problems fixed.
(edited). You're absolutely right about keeping the gui and calculations separate. I shouldn't program when I'm tired. I started out with that intention!

Categorie

Scopri di più su Creating, Deleting, and Querying Graphics Objects in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by