How do I add my own variables to the handles structure from within a custom function of mine?
I have a question to ask the MATLAB gurus here ..
So here is my code (only showing lines of code which are relevant to the problem here):
mainProcess(hObject, handles)
handles.Checkpoint2 =1;
guidata(hObject, handles);
function testGUI1_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
handles.Checkpoint1 = 1;
mainProcess(hObject, handles);
handles.Checkpoint3 = 1; //EDIT: Checkpoint2 is also not visible at this line
guidata(hObject, handles);
handles.Checkpoint4 = 1;
function saveAndContinueButton_Callback(hObject, eventdata, handles)
(breakpoint here) --> faceDatabase(handles.currentImageIteration).lookingTowardsCamera=handles.lookingAtCamera;
So in the above code, I'm making these 'checkpoints' at different parts of the code, and seeing which of them are visible when a save and continue button is clicked separately ... Checkpoint1 is created BEFORE calling my custom function called mainProcess, Checkpoint2 is created within the code of mainProcess, and Che开发者_开发知识库ckpoint3 is created AFTER mainProcess is finished executing and the control is back with the function that called it, which is testGUI1_OpeningFcn ... And Checkpoint4 is created WITHIN testGUI1_OpeningFcn, but AFTER the handles structure is updated in the testGUI1_OpeningFcn code ..
So my question is this, when the button is clicked and I see what is visible at that point, Checkpoint 1 and 3 are visible to the button Callback code, but Checkpoints 2 and 4 are NOT visible ... I understand that Checkpoint4 is not visible because it was created AFTER the handles structure was updated in testGUI1_OpeningFcn's code ... But why is Checkpoint2 not visible, even when at the end of mainProcess's code, I did put a line:
guidata(hObject, handles);
I mean the mainProcess function is getting references to both hObject and handles, so it should have write access to it, right ?
So why isn't Checkpoint2 not visible to the button's Callback code .. ?
Any clues ?
EDIT: I just tried to see if Checkpoint2 is visible even within mainProcess's calling function, right after the control is returned the caller, and even there Checkpoint2 is not visible (see the EDIT in the code above) ..
That's correct, you need to call guidata to update the handles variable you have. However, guidata needs an argument. I think the right command would be:
handles = guidata(hObject);
You may find this link helpful:
http://www.mathworks.com/matlabcentral/answers/10197-guidata-doesn-t-work-the-way-i-expected-it-to
I believe you need to add the following just after calling mainProcess()
handles = guidata();
In general, the 'handles' struct is passed by value to the guidata() function. Therefore, mainProcess() cannot change the handles structure -- just attach the existing structure to the handle. Before making further modifications you need to get it back (using handles=guidata()), update it and set it again with guidata(h, handles).
Let me know if this is not clear enough (or just does not work :)
Edit
You need to change the code like this:
function testGUI1_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
handles.Checkpoint1 = 1;
mainProcess(hObject, handles);
handles = guidata(); // <--- new line
handles.Checkpoint3 = 1; //EDIT: now checkpoint2 will be visible here
guidata(hObject, handles);
handles.Checkpoint4 = 1;
guidata(hObject, handles); // Otherwise Checkpoint4 will not be bound to hObject
精彩评论