Sharing programs with your colleagues is a very valuable collaboration. In this post, I introduce you to good manners in Matlab programming and their inevitable counterpart : making interfaces to relief your colleagues from the burden of reading and learning all your code.
But we are all very social beings. We want and we need to interact with other programming animals. So very quickly you will come to a point where you want to share your Matlab program with others.
As always, there are some good manners to have when you get out of your programming tower. Here I would like to quote wikipedia :
The goal of politeness is to make all of the parties relaxed and comfortable with one another.
I personally consider a good manner to first have a clean and easy to understand code. The second rule of good programming manner, in my opinion, is to make an interface to your code. Just as we just said, you need to make all parties relaxed and comfortable. And the command line is not going to make your colleagues relaxed and definitely not comfortable.
Besides, some of your colleagues might not know anything about programming. It would be very elegant and appropriate to make these persons life relaxed also.
Luckily, in Matlab, this is not a big deal. I am going to help you to make your entry in the world of interfaces.
There are two ways to make interfaces :
- By hand, directly in your M-file.
- Or using a FIG-file created by the GUIDE or Graphical User Interface Development Environment).
Today, I want to talk about option 2 which is by far easier. The first thing you need to do is to start the GUIDE itself. This is easy, just type in the command line :
You should get a window like this :
From this, select ‘Blank GUI’ and press ‘OK’. You should get this :
This is an empty interface. In the left column are all the objects you can place in the window. The first of them is a simple Push button. There are many other objects like Sliders, Radio button, Checkbox, Edit text, Static text, … . To get started we don’t need to go over all the available objects as these should be quite intuitive.
But I want you to focus on how one object works. Just select the Push button and drag it in the interface. You should get something like this.
Double-click on the button itself. This window should appear :
This is called the Property Inspector. These are all the properties of the object we just selected. Here, as expected, the property ‘Style’ is set to ‘pushbutton’. Of all these properties, the most important, for now, are ‘Tag’ and ‘String’.
- ‘String’ sets the text on the button. So if you click on the second column, you can change ‘Push button’ to ‘OK’.
- ‘Tag’ is the unique identity of the object. In one interface, there can be many push buttons but they all need to have different ‘Tag’. So click on the property and change the Tag from ‘pushbutton1’ to ‘LaunchButton’.
Now we have one interface with one button. You need to know where to place the code so that this code is launched when the user presses the button. To do so, you have to tell to GUIDE where to store the M-file. So click on , GUIDE will ask you where to store the FIG-file. Do as you wish, I named it FirstGUI. An interface is a FIG-file attached with a M-file. Now the corresponding M-file should automatically open in the editor.
This M-file is rather complicated but if you want to understand in details how it works, I have a comprehensive explanation. For now, the only think you need to know is that each button on the interface has a CALLBACK function in this M-file. So there is a LaunchButton_Callback that is attached to the button we just placed. Go to this function and add this one call to msgbox :
% --- Executes on button press in LaunchButton. function LaunchButton_Callback(hObject, eventdata, handles) % hObject handle to LaunchButton (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) msgbox('Hello world');
You are done. Now you can start the interface using the button . If you click on the button, a nice interface should now appear. You can position any code within the callback. To go in more details and use more complicated objects, look into how to use handles.