In GUI, is there a function where I can initialize variables using handles that I can then pass on to all CreateFcn items?

조회 수: 3 (최근 30일)
OK, so I am new to GUI and am using the GUIDE feature. I created a complicated UI with lots of popout menus, sliders, etc. I wanted a bunch of variables to be readily accessible to the CreateFcn functions of all the items. I tried OpeningFcn but it looks like all the CreateFcn's are run before the OpeningFcn. I have the option of just declaring all my variables using handles in the popoutmenu that runs first. But is there no other alternative to this? I would ideally like a function to initialize all my required variables that runs first before creating any functions. Or at least, let me know if there is a way we can determine which menu item runs first. It looks like by default, the item you created last will run first. Thank you!

채택된 답변

Walter Roberson
Walter Roberson 2017년 8월 3일
It is correct that the handles structure does not exist until after all of the CreateFcns have run.
Also, the order of running the CreateFcns is not completely defined: it is whichever one happens to occur first in the .fig file. The figure CreateFcn will be the first one for the figure, but if you have multiple items at the same level then you do not know which will get run first.
So you should probably have your figure CreateFcn build the variables and store them, perhaps using setappdata against the figure. (Using setappdata allows you to retrieve only the portions that you need for any one function.)
  댓글 수: 3
Walter Roberson
Walter Roberson 2017년 8월 3일
Unfortunately the documentation just says "Callback executes when MATLAB creates the object, but before it is displayed."
I think it is probably reasonable for a CreateFcn to run after all of the objects contained "inside" it are created, so that the CreateFcn can change settings knowing that its children all exist. So it does not astonish me that the popup CreateFcn runs first.
You could have a common "create variables if missing" routine that you called from all of the CreateFcn.
Pavan
Pavan 2017년 8월 4일
That should work too. But basically, it looks like the popupmenu item I created last in the GUIDE is running first. So I have all my initialization codes in there and it works. Thank you for your help!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Akhilesh Thakur
Akhilesh Thakur 2017년 8월 3일
I guess in createFn the handles are not created. handles empty - handles not created until after all CreateFcns called So i guess you wanted to initialise all the handles before the object is made visible. Doing that in OpeningFn would be easier. And you can do that by just modifying for eg handles.keyFnpress == 'ENTER' in your function. So you can modify all the properties. I hope this helps.

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by