How to modify the handles structure (GUI) from an external function
    8 Ansichten (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
    MRR
 am 8 Mär. 2013
  
    
    
    
    
    Beantwortet: zakaria
 am 5 Jun. 2014
            Hi,
I am developing a GUI. I can modify the handles structure (for example handles.variable1 = 2) within callbacks. However I am not able to do the same within an external function. E.g., this does not work:
function [matrix, indexVector] = myFunction(handles)
handles.variable1 = 1;
It is important to note that myFunction does not belong to the GUI callbacks, it is simply a function I have created in another m file. When I call it I can read the handles struture (actually im passing them as a parameter) but when I try to modify them the modification is not recorded.
Best,
-MRR
0 Kommentare
Akzeptierte Antwort
  Sean de Wolski
      
      
 am 8 Mär. 2013
        
      Bearbeitet: Sean de Wolski
      
      
 am 8 Mär. 2013
  
      function [matrix, indexVector] = myFunction(handles)
handles.variable1 = 1;
guidata(handles.figure1,handles)
Where figure1 is the 'Tag' of the figure. You want to make sure that you assign the handles structure to the right place (i.e. the figure).  hObject is dynamic, handles will always have the field for the figure.
10 Kommentare
  Jan
      
      
 am 20 Mär. 2013
				
      Bearbeitet: Jan
      
      
 am 20 Mär. 2013
  
			@MRR: Yes, of course, this is the expected behavior. Inside myFunction you update the handles struct and store it in the figure afterwards. Then after returned to Simulate_Callback, you use the local copy of handles with its value before calling myFunction() and overwrite the changes from inside this function. A suggestion for improvements:
   function errorParameters = Simulate_Callback(hObject, eventdata, handles)
    %some code here ...
    handles = guidata(hObject);  % Care for the newest version explicitly!
    [output1, output2] = myFunction(hObject, handles);
    handles = guidata(hObject);  % Get the version updated in myFunction!
    handles.variable1 = handles.variable1 + 1;
    guidata(hObject, handles);
And inside myFunction(hObject, handles) I do:
      handles.variable1 = 200; %or any other value
      guidata(hObject, handles);
So Azzi's and Sean's answers hit the point already, but in your code there was a problem at another location, namely in the caller, which forgot to obtain the updated struct.
Weitere Antworten (3)
  Azzi Abdelmalek
      
      
 am 8 Mär. 2013
        Use
function [matrix, indexVector] = myFunction(hObject,handles)
handles.variable1 = 1;
guidata(hObject,handles)
4 Kommentare
  zakaria
 am 5 Jun. 2014
         handles = guihandles(gcbo); % if not presents in the other external function
0 Kommentare
Siehe auch
Kategorien
				Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





