Handles in GUIDATA desappear when placed in 'CreateFcn'
2 ビュー (過去 30 日間)
古いコメントを表示
Hey guys,
I've read other threads but couldn't find a solution.
The thing is, when I write a variable to handles in a 'CreateFcn', I can see it there if I put a breakpoint after
Guidata(hObject,handles);
But when I need that var when using the gui, it's not there.
What am I doing wrong??
This is where I create the function:
handles.data.e_pExt=uicontrol( tab1,...
'Style','Edit',...
'String','[MPa]',...
'FontName','Arial',...
'FontSize',10,...
'Units','Norm',...
'Pos',[0.175,0.71,0.101,0.05],...
'Horiz','Center',...
'Callback', @(hObject,eventdata)...
Copy_of_cylinder('e_pExt_Callback',...
hObject,eventdata,guidata(hObject)),...
'CreateFcn',@(hObject,eventdata)...
Copy_of_cylinder('e_pExt_CreateFcn',...
hObject,eventdata,guidata(hObject)) );
This is where I save the data into handles (specifically handles.pAtm)
function e_pAtm_CreateFcn(hObject, ~, handles) %#ok<DEFNU>
if ispc && isequal( get(hObject,'BackgroundColor'),...
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
handles.pAtm=handles.data.pAtm_ini;
handles.data.e_Atm_chkvalue=true;
guidata(handles.cyl_fig,handles);
And as you can see, it's there when debugging (while oppening function is running)
But then, I need that var when editing the editbox and it's simply vanishes
Do you know why this happens?
Regards
2 件のコメント
Adam Danz
2020 年 5 月 30 日
編集済み: Adam Danz
2020 年 5 月 30 日
If the e_pAtm_CreateFcn is executed when the GUIDE GUI is initialized, the handles structure should be empty, I believe, and the call to handles.cyl_fig should throw an error since cyl_fig should not exist in the handles structure. At least that's what's happening when I test the create functions in GUIDE when a GUI initializes.
Furthermore, all of the Create Functions are run before Opening Function is executed and the Opening Function is the function that stores the handles in the GUI. Some something is fishy about the workflow you're describing unless there are some important details that were left out.
You should be getting an error message but the GUI should still appear to open.
採用された回答
Adam Danz
2020 年 5 月 30 日
編集済み: Adam Danz
2020 年 5 月 30 日
"This is my first GUI and my first program so maybe this is not the best approach."
Correct :)
You've definitely invested some time into this but I strongly urge you to move your GUI from GUIDE to either App Designer or to a GUI created from bottom up using uicontrol(). The time you'll save in the longrun and the skills you'll learn will be a much better investment of your time than continuing wiht GUIDE. Here are my reasons.
1) Matlab plans to stop developing and supporting GUIDE GUIs in future releases.
2) If you're using GUIDE just to create the figure and start the m-file, you're practically already using the same workflow you would use with the uicontrol() method. But since your GUI figure is initialized with GUIDE, it's doing a lot more work than necessary.
3) There's a lot of mysterious things that happen behind the scenes in GUIDE. If you use the uicontrol method, you have much more control of what's happening. App Designer is also more transparent than GUIDE, it offers less control but has more visual appeal and is easier to place objects in the GUI/App.
Here are two other threads that urge new users (all users, actually) to avoid using GUIDE. The contain advice how to transition in one of alternatives.
15 件のコメント
Adam Danz
2020 年 6 月 1 日
Good work! In general, only use gcf, gca, gco, etc when you have no other alternative.
If you have any other questions about making the GUI I'd be happy to help.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Migrate GUIDE Apps についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!