How to convert a GUI *.m file generated by GUIDE's export tool back to a *.fig file?

9 views (last 30 days)
As described : https://blogs.mathworks.com/community/2011/04/04/guide-guis-in-all-one-file/, it's possible to export a GUI *.fig file (and it's *.m file) to a single *.m file, which is very useful for version controlling a project (fig files are compiled).
However, the generated single *.m file is not easy to edit again if you need to do more changes to the GUI. Is there a way to convert that file back to the *.fig and *.m files, or import that single *.m into GUIDE, so that it can be editable there?
  3 Comments
Mathieu Boudreau
Mathieu Boudreau on 6 Sep 2019
I don't think that's possible. The .m file only contains the function definitions and implementations related to your GUI, not the information that format/define the GUI objects.
Rik
Rik on 6 Sep 2019
It is indeed impossible to reconstruct the fig file from the code only (unless you have used the export functionality).
This entire thread shows why using GUIDE is a bad idea in most circumstances. GUIDE has been stuck in the same state for years (a decade?) now. For anything more complex than throwing together a GUI with 4 elements I would highly suggest using native Matlab (sometimes called a 'programmatic GUI') or AppDesigner.

Sign in to comment.

Answers (1)

OCDER
OCDER on 25 Jul 2018
Edited: OCDER on 30 Jul 2018
NEW ANSWER:
How about this?
1) Export your guide .fig file as a single m file. EX: myGUI.m
2) >> h1 = myGUI
3) >> saveas(h1, 'myGUI.fig', 'fig')
4) >> guide('myGUI.fig')
OLD ANSWER
You could use App Designer instead of GUI, which packages the code and figure into a single .mlapp file https://www.mathworks.com/help/matlab/creating_guis/create-a-simple-app-or-gui-using-app-designer.html
If stuck in GUIDE, I would just keep both the .fig file (to easily edit the "View") and the .m file (to easily edit the "Controller"). Read this https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
If the ONLY reason to combine the files into one .m file is for version control, there are better ways to do it via github or just changing the version number in the .m file when you make significant changes, and also keep a changes.log file. I don't think it's worth trying to hard-code-edit the GUI View, as that'll require a lot of imagination, manual calculation of ui component positions, etc.
  5 Comments
Adam
Adam on 30 Jul 2018
Unfortunately I doubt anything new will be added to GUIDE in future since AppDesigner is deemed to be the future for Matlab. I don't think you should have these problems with AppDesigner as the whole GUI is created in a class (similar to how I would do a programmatic GUI) so the WYSIWYG part of it that allows you to move components just translates straight into the code.
I have also encountered the nightmare of having two people edit a .fig file from GUIDE when using a repository and it is not fun at all. We were only a small team sat next to each other though so it wasn't too difficult for us to just ask each other not to edit the GUI whilst one of us was doing it. I'm not sure there are any solutions to this for GUIDE though. I use a mixture of GUIDE GUIs and programmatic ones - the latter are more robust, but less quick to design and change. AppDesigner sends me flying into a rage so I don't use it at the moment until it improves (if it will).
OCDER
OCDER on 30 Jul 2018
Yeah, doesn't seem like there's an easy way. Other languages like JAVA will have some sort of XML code for their GUI that can be modified and saved, and thus it's these XML code changes that can be tracked. For matlab GUIDE, there is no feature like this other than a "LayoutFcn". I'm not sure how to manipulate this LayoutFcn separately.
I imagine you'll have to do this.
1. Convert .m + .fig into a single exported .m file (myGUI.m)
2. Use the step in Answer to convert a .m to .fig file (myGUI_test.fig)
3. Make changes to the .fig file via GUIDE
4. Export GUI into another .m file (myGUI_test_export.m)
5. Use a separate code to "extract" the new LayoutFcn and
callbackFcn from myGUI_test_export.m into the original myGUI.m file.
Essentially, you're merging the new changes into the old .m file.
It's a mess...

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!