Variables in GUIDE program mysteriously being reset
显示 更早的评论
Hi, I'm relatively new to GUIDE, though I have used Matlab extensively before. In the OpeningFcn of my gui program, I declare a number of variables that need to be kept track of:
handles.output = hObject;
handles.TOFdata=[];
handles.Energies=[];
handles.times=[];
handles.SubNoise = true;
handles.CurrentPlottedEnergy = 0;
handles.masses=[];
handles.MassMultiplier=0;
handles.toffset = 0;
This mainly works fine. For instance, the handles.times vector is referenced many times throughout the program to update plots, and there's no trouble.
However, the last three variables above (masses, MassMultiplier, and toffset) seem to get reset any time I assign them. In a certain callback function I assign them values, and during the scope of that function, they are still in memory. But when a different callback references them, I find that they have been re-initialized to the values above.
Is there a maximum number of variables that can be held in this way? The line to set handles.masses=[]; is not executed except when launching the gui (which I verified by setting a breakpoint there). I cannot seem to locate where or why they are being reset.
I'd appreciate any help you can give. This is on R2011b. I can send the code if someone wants to try it themselves---just email me at r8e8u8b8e8n8g8a8n8n@gmail.com (remove the 8s) and I will email the m file. Thanks
回答(1 个)
Walter Roberson
2012-6-22
After you set the values and before the end of the function that sets them, make sure you have
guidata(hObject, handles)
Also if you have any routine in which you call a function that might change the values (including a pause() or drawnow() that might allow a callback to execute) and you need the (possibly) updated values after that point in the function, be sure to
handles = guidata(hObject);
in order to fetch the updated values.
2 个评论
Reuben Gann
2012-6-22
Walter Roberson
2012-6-22
handles = guidata(hObject) needs to go in any routine that needs to access updated handle data.
You could experiment by moving those lines higher up. I know the order should not make a difference but confirmation is always good.
类别
在 帮助中心 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!