Handles in GUIDATA desappear when placed in 'CreateFcn'

2 views (last 30 days)
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 Comments
Adam Danz
Adam Danz on 30 May 2020
Edited: Adam Danz on 30 May 2020
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.
Rafael Barata
Rafael Barata on 30 May 2020
Edited: Rafael Barata on 30 May 2020
I understand what you're saying.
In fact, I just used GUIDE to create the figure and start the m.file. The rest of the GUI I did programmatically.
Every object is created inside the opening function.
This is my first GUI and my first program so maybe this is not the best approach.
As you can see now, every CreateFcn is executed inside of the Opening function so the handles struct is already being filled.
The first image above is during the GUI's creation, inside opening fcn and the second is with the gui running when I edit the edit box. First the variable is there and then it's gone. I can't understand why.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 30 May 2020
Edited: Adam Danz on 30 May 2020
"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 Comments
Rafael Barata
Rafael Barata on 1 Jun 2020
Problem solved! I was getting and setting handles from/to gcf. Then I change it to the figure's handle and it works. I thought it was the same but results it wasn't. Althougth that get(gcf) or get(handles.cyl_fig) prints the same result, handles=guidata(gcf) is not the same that handles=guidata(handles.cyl_fig). This I can't understand but my original doubt is solved.
Thanks for all your time and help. Best regards :)
Adam Danz
Adam Danz on 1 Jun 2020
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!