Get code from a GUI created using GUIDE

18 views (last 30 days)
Juan
Juan on 26 Mar 2014
Commented: uopdon on 5 Feb 2015
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 Comments
per isakson
per isakson on 27 Mar 2014
" that a few things I want to do would be easier" Some examples of these things would help
uopdon
uopdon on 5 Feb 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.

Sign in to comment.

Answers (3)

Image Analyst
Image Analyst on 26 Mar 2014
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
Joseph Cheng on 26 Mar 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 Comments
Image Analyst
Image Analyst on 26 Mar 2014
Not that I know of. Why do you want to go through such pain and inconvenience anyway?
Joseph Cheng
Joseph Cheng on 27 Mar 2014
Edited: Joseph Cheng on 27 Mar 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.
  1. save handles using save('handles.mat','-struct','handles')
  2. exit existing gui and clear all variables.
  3. load handles.mat
  4. listofuiobjects = who;
  5. fid = fopen('textfile.txt')
  6. 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.

Sign in to comment.


per isakson
per isakson on 26 Mar 2014
Edited: per isakson on 27 Mar 2014
" 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.

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!