Understanding and using guihandles programmatically
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Rebecca Prescott
le 7 Mai 2016
Modifié(e) : Rebecca Prescott
le 7 Mai 2016
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
0 commentaires
Réponse acceptée
Jan
le 7 Mai 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);
0 commentaires
Plus de réponses (1)
Voir également
Catégories
En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!