可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I've been trying to put together a gui in MATLAB and have had great difficulty. Here is a little background about my program: I want to create a wizard to step users through an image analysis process. The wizard will cue users for their input and then perform an image processing action once a button labeled "Next" has been pressed, and so on. From reading the MATLAB help I believe there are two approaches to gui creation: 1. write a gui with lines of code. 2. use GUIDE.
I figured GUIDE would have the shallowest learning curve. At first the process was straight-forward. I could drag and drop buttons and text boxes and arrange them easily. My plan was to arrange a series of panels, with the buttons and text and etc., that would sequentially become visible as the "Next" button was pressed. And it was here I became stumped.
Once I save a panel with GUIDE, MATLAB creates a .m file and a .fig file. Reading through the .m file I see it has left room for me to write code for the callbacks, but little else. The .fig file is binary and therefore I dare not bother edit it. I am left wondering how I can control the objects, e.g. How to set the visibility of a panel? Furthermore, I had to create each panel of the wizard separately. I was hoping that I could manually combine the .m files into a wizard.m wrapper file, but I do not think this is the right approach (at least for me, because I cannot see how to do it).
In lieu of using GUIDE I've seen examples where guis are created using the "uicontrol" function. This approach seems tedious to me, because I would have to manually code the position and size and defaults of each button. Perhaps this is the best approach, but it intimidates me.
As a result I am unhappy with MATLAB for its seemingly clunky gui tools and I am unhappy with myself for lacking the chops to overcome this hurdle.
I've seen some fancy guis created in MATLAB and having attempted this myself and falling way short I am baffled by their existence. Either whoever made them were exceptional MATLAB users (I am not), or I am missing something obvious and could use some help seeing. I am now questioning whether a gui is worth the effort. I do not see myself exploring something like QT, because of the additional challenge of embedding MATLAB code into other languages such as python.
Does anyone have advice on this subject? Can anyone please point out where I am going wrong or why I am having such difficulty?
回答1:
The .fig file is binary and therefore I dare not bother edit it.
FIG file is a MAT file containing list of gui elements with properties you set in GUIDE.
How to set the visibility of a panel?
Assign a Tag to the element, you can access it using handles.mytag
like set(handles.mypanel, 'visible', 'off')
Furthermore, I had to create
each panel of the wizard separately. I was hoping that I could
manually combine the .m files into a wizard.m wrapper file, but I do
not think this is the right approach (at least for me, because I
cannot see how to do it).
You can not combine these automatically generated m-files into one, why would you want to anyway? Keep a .fig and .m file for each wizard page!
In lieu of using GUIDE I've seen examples where guis are created using the
"uicontrol" function. This approach seems tedious to me, because I
would have to manually code the position and size and defaults of each
button. Perhaps this is the best approach, but it intimidates me.
Using uicontrol
directly is not bad if you do not position elements manually but use a layout manager. You can see some here and here. If you want you GUI to be nicely resizable then layout manager is the only way to do it.
Does anyone have advice on this subject? Can anyone please point out
where I am going wrong or why I am having such difficulty?
Professional GUI programming is not easy, IMO is even the most difficult part at all.
Besides MATLAB GUI I have used both Java Swing and .NET WPF a lot, IMO MATLAB GUI is much easier, you can do 90% with 10% (learning) effort Sure, you still need time to become accustomed to it.
As a result I am unhappy with MATLAB for its seemingly clunky gui
tools and I am unhappy with myself for lacking the chops to overcome
this hurdle.
Yes, GUIDE is not the best solution but is probably the best technique to quickly create an GUI.
I've seen some fancy guis created in MATLAB and having attempted this
myself and falling way short I am baffled by their existence.
These GUI's are hacks exploiting MATLAB GUI particular details like visual separator being a long '__________' black text on a white uicontrol which is only two pixel high or different colors in tooltip by using html.
And after all you can use Java Swing in a MATLAB GUI (because it is actually derived from Swing)
I am now questioning whether a gui is worth the effort.
Yes, in your case keep it very simple, just the functionality, do not think about fancy stuff!
回答2:
GUIDE is, as of June 2015, worse than you think. Highly inefficient for configuring a GUI, it is buggy as hell. Definitely not professional grade. GUIDE, how do I despise thee? Let me enumerate the ways:
1) Entry errors while creating a GUI are not flagged. With this unannounced error lurking, you can work for hours. When you hit "SAVE", GUIDE will not save your work. Thus, GUIDE violates the prime directive of any software package: thou shalt not lose user's work or data. I can't believe Mathworks actually released something this bad.
2) Gang copying is iffy. Let's say you have a program that calculates the effect of breakfast. You have hour, minutes, amount of carbs as editable boxes. You have labels as static text. You want the same thing for lunch and dinner. Select all. Ctrl-C. Point the cursor somewhere else, and hit Ctrl-V. Nothing happens. No, wait, about 30 seconds later, the paste appears. But if you are already doing something else, well... Try figuring out what the hell happened.
3) Even a gang move is iffy/strange/supernatural/possessed. Say you want to move that row with lunch up and down? Highlight. Use arrow keys to move it. Success! But wait! After about five seconds, the text and editboxes start to move back to the original position! Without intervention! It's possessed!
I've used GUIDE to create a fairly complex GUI. Way more heartache and hassle than it should - or could - be. Given freeware alternatives that run JAVA/Python/Julia, I'd not start another MATLAB project that needed a GUI. Save your dough!
回答3:
Personally, I find GUIDE good enough for quick simple GUIs and to learn how to create GUIs, but now I generally prefer to create the GUIs programmatically. This allows for much better control of the content. For me, the crux is to use the UserData properties of the UI elements to store any useful data, including handles to the UI objects you will want to update.
But if you want something clever, then you will have to resort to something Java- or .NET-based.
Also, it's important to remember what MATLAB is best at, and GUIs are not the strong-suit. If you use a programming language where the GUI creation is better, then you will have to compromise on something else, such as the ease of working with array and matrix data.
EDIT: The GUI Layout Toolbox linked to by @Mikhail looks extremely useful.
回答4:
I've been programming guis in MATLAB.
From what I've seen GUIDE makes programming guis quite convenient. As you said you actually don't have to bother about programming any buttons etc., it's just arranging the buttons and filling the m-files. Within buttons you can handle visibility by setting the property 'Visible' to 'on' or 'off'.
I tried both approaches: Manually writing the gui and writing it using GUIDE.
I had the impression that you really just have to get into it. Once you got the concept of handles (that's the main thing with matlab guis) you've got it.
Try to program a simple gui first, don't start with your project. Get a hold of how handles and the inspector work in MATLAB and how you can set properties of values. Figure out where you can store data within the GUI and how to retrieve it in other windows (pass handles between windows).
I guess that's about it! Programming guis in MATLAB can be hard to begin, but it's pretty straight forward once you figured out the things I mentioned above.
回答5:
The main problem with GUIDE generated GUIs, opposed to programmatically created ones is the fact that the file is not a text. Thus, you cannot use a version control. There is a workaround - you can use the export
function to create only .m
file. Of course, you will not be able to switch it back to fig
and edit it visually.
Personally, I always prefer to write GUIs without GUIDE.
回答6:
I’m coming in late to this conversation, but wanted to add my $0.02. As you mentioned GUI design is not MatLab’s strong suit. However, a lot can be done with it. If you have any specific questions I’d be happy to answer them. Also, realize that it is built on the java swing library so you can actually utilize those features. However, these features are not documented. There is a book title Undocumented Secrets of MatLab that provides a pretty decent discussion of the underlying swing components.
There are quirks to MatLab GUI, but if you can get over those then you’ll enjoy it. Also, as previously mentioned one of the more difficult hurdles is the concept of handles. Once you grasp that concept you will be 90% of the way there.