Get code from a GUI created using GUIDE
Mostrar comentarios más antiguos
I created a GUI using GUIDE a while ago. Now I have realized that a few things I want to do would be easier if had created the GUI form scratch using the editor. Instead of starting over again I would like to use what I already have. Is it posible to get the code that creates all the objects in the GUI that I set up with GUIDE? (I have the m files that GUIDE creates, but I can't find where the buttons, checkbox, etc are set). Thanks
2 comentarios
per isakson
el 27 de Mzo. de 2014
" that a few things I want to do would be easier" Some examples of these things would help
uopdon
el 5 de Feb. de 2015
I fully understand Juan's point. I also prefer the programmatic approach to creating GUI's, but find it slightly difficult to sketch out where the objects go, which is where GUIDE could come in handy. I want to use GUIDE to sketch out the GUI, and then simply get the programmatic code that would generate the same GUI. I would also like to be able to use the property inspector in GUIDE to edit properties - for example I find it a lot easier (and more creative) to set the background colour of a control using GUIDE. However, I then want GUIDE to create the programmatic version of the code with the non default value attributes.
Respuestas (3)
Image Analyst
el 26 de Mzo. de 2014
0 votos
Not sure I follow. You can create a new GUI with GUIDE then view any callback to get the new, empty m-file. Then you can copy and paste from some other, old m-file into the new one, if you want to.
Joseph Cheng
el 26 de Mzo. de 2014
The only way I can think of getting the positions and parameters from the GUI items would be to insert a section within the startup to grab the desired parameters for each and save them to a file.
example:
%insert into yourGUI_OpeningFcn(hObject.....
pushbuttonAttribute = get(handles.pushbutton1);
Then you'll have to save or record these for your use.
6 comentarios
Image Analyst
el 26 de Mzo. de 2014
Why would you do that? You can get them instantly anytime you want them. Why store them all in advance of running the program? If you want, you can have functions to save all the last states of various controls in a mat file and then recall those in the openingfcn function to restore the controls to the state the user last left them in.
Joseph Cheng
el 26 de Mzo. de 2014
Editada: Joseph Cheng
el 26 de Mzo. de 2014
From his question it appears he has a GUI created in GUIDE, however he is attempting to go away from guide and manually write them using ui_____. However he already has the GUI arranged in GUIDE so my suggestion is with the already written file insert and store the uicontrol positions or any other parameters he want. Alternative would be get a pad and pencil and record each position by hand for his non-GUIDE created GUI.
Image Analyst
el 26 de Mzo. de 2014
Oh, if he wants to go the harder route of manually doing it all himself, then he should see Matt Fig's "41 gui examples" File Exchange submission. And of course prepare himself for a ton of work.
Image Analyst
el 26 de Mzo. de 2014
Juan's "Answer" moved here since it's not an answer to his original question but a comment about Joseph's comment 2 above here:
Joseph Cheng is right. Let's say I have created a pushbutton with position [1 1 10 5], units, 'pixels', and enable 'off'. I would like to get the code to create such button from scratch (uicontrol('Style','pushbutton','Units','pixels','Position',[0 0 100 50],'Enable','off')). I can of course write that myself, but provided that I have already the GUI as I like it I was wondering if I could get that automatically for each of the ui objects I have. One option would be to write a short script to 'get' each the properties from each object and then write a new m file. But, is there an automatic way? Thanks!
Image Analyst
el 26 de Mzo. de 2014
Not that I know of. Why do you want to go through such pain and inconvenience anyway?
Joseph Cheng
el 27 de Mzo. de 2014
Editada: Joseph Cheng
el 27 de Mzo. de 2014
There is anotherway... Still painful but not that bad, probably can make this in like 20 min (including debuggin). Since each ui-object is saved within handles.
- save handles using save('handles.mat','-struct','handles')
- exit existing gui and clear all variables.
- load handles.mat
- listofuiobjects = who;
- fid = fopen('textfile.txt')
- write for loop to get(listofuiobjects(i).Position) and style and other param.
- uitext = [''uicontrol('Style',' retreivedStyle ','Units','pixels','Position', ' retreivedPosition ' ,'Enable','off'))'] %this is air coding (saw term in another post here and liked it-similar to air guitar) so don't hold it against me if the string concatenation is wrong.
6. fprintf uitext within the above forloop.
per isakson
el 26 de Mzo. de 2014
Editada: per isakson
el 27 de Mzo. de 2014
0 votos
" Is it possible to get the code that creates all the objects in the GUI that I set up with GUIDE?"
I think it is possible to create a function, fig2mfile, which takes the "figure" as input and outputs an mfile function, which in turn can create the figure.
The figure is a rooted tree of handle graphic objects. Starting with h=findall(fig_h,1,'-depth',1) and get(h(ii)) it is possible to to build that tree as a structure. Next it's possible to traverse the tree and create all the graphic handle objects, which would result in a huge mfile of set(handle,name,value). There is nothing of this kind in the File Exchange - as far as I can see.
The fig-file of GUIDE is a mat-file (behaves like a mat-file if you change the extension to .mat), which contains a huge structure. The structure is easy to navigate.
.
However, for practical purposes it is NOT possible.
- And one day The Mathworks will present next generation of GUI building tools.
Categorías
Más información sobre Entering Commands en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!