MATLAB: How to modify the handles structure (GUI) from an external function

guidehandlesmodify handles

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

Best Answer

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.