An example of a MATLAB graphical user interface (1)

Source: Internet
Author: User
Tags call back

to create a complete Matlab/gui step:
Run the Guide command in the Command window of MATLAB to open the Guide interface as follows:
Then, select the empty template (Blang GUI) and click OK to open the Guide's design interface as follows:
Click menu Editor on the toolbar to open the menu editor, as follows:
To create a new menu item in menu bar with the name "file", see the other settings:
Under the File menu, add menu items: Open, save, exit. See:
If you need to add a split line above the menu item "Exit", select "Separator above this item "on the line.  Save my interface for Pjimage.fig. Once saved, the PJIMAGE.M file is automatically opened, and all our programs are written in this m file. In programming, each of our mouse actions corresponds to a callback function. Then our menu items are the same. on the interface, click the right mouse button and select "Property Inspector" to open the Properties window. When we click on a non -identical control, its corresponding property will be shown here, we can modify it. The main attributes are the tag property and the string property.  Set the Tag property of the current figure window to: Figure_pjimage, the title of the window (Name property) is the image location instance. as follows:
Then, click the Save button on the toolbar. Then click the Run button on the toolbar (run figure). Note that the toolbar icon will be prompted, like the prompt to run the button is run figure. We will see the following interface:
that means that the directory of the. Fig file We saved is not the current directory, but it doesn't matter, we just click "Change Directory" to alter the current directory. Of course, if you want to add the current directory to the MATLAB path , then click "Add to Path" will be OK. I recommend clicking "Change Directory" here, because there is nothing too important to add it to the MATLAB path, usually the toolbox needs to be added or our function or program is finished, while the command in MATLAB
when the window cannot find our function, we can add the directory where the function or program resides to the MATLAB path. in Short, point that button, depends on the personal hobby. Regardless of the button that clicked the two button, the program will run correctly. the way our program runs, is this:
we can see the menu items and shortcut keys under the file, but we didn't write the program, so there's no point. Response. And if you don't want to set a shortcut, you can set it in the menu editor, just choose it as Ctrl+none , as follows:
in this case, the saved item will not have the shortcut key. We can view the menu item by using the button "view" above .The response function, also known as the callback function. can also be seen in PJIMAGE.M, such as the saved Tag property isM_file_save, then the name of the corresponding Callback function is m_file_save_callback. In turn. Let's write the function to open the menu item, open a picture, of course, open the dialog box. In interface programming,The function that opens the dialog box is uigetfile. Detailed instructions on it are viewed with the help Uigetfile command. Belowis the response function that opens the menu:
function M_file_open_callback (hobject, EventData, handles) [filename, pathname] = uigetfile (... {' *.bmp;*.jpg;*.png;*.jpeg ', ' Image Files (*.bmp, *.jpg, *.png,*.jpeg) ';... ' *. * ', ' all Files (*. *} ', ... ' Pick an Image ') ;
Save the. m file and run the program. Clicking "Open" under "file" opens the following Open dialog box:
    After selecting a file, The filename in the program is the file name of the file you selected, and pathname is the path to the directory where the   settings\ Administrator\my documents\.   So how do we read and display a picture after we get the path? The read-in picture can be used imread    Draw two axes, a display before processing, a display after processing. The Tag property of the axis before processing is changed to   
Then, on the basis of the original M_file_open_callback program, add the following program:
Axes (handles.axes_src);% set the axis of the current operation with the axes command is Axes_srcfpath=[pathname filename];% combines the file name and directory name into a full path Imshow (Imread ( Fpath));% reads the picture with Imread and displays it on the axes_src with the Imshow
Run the program and open a picture with the Open menu item. The effect is as follows:
So how do you save a picture? Use the Imwrite command. But the first parameter of the Imwrite command is the picture data that you read, which is the return value of Imread. In that case, we will
Make a small change to the program in M_file_open_callback. The last sentence (Imshow (Imread (Fpath))) is changed to two sentences, as follows:
Img_src=imread (Fpath); Imshow (IMG_SRC);
Not only that, the callback function of our save menu, how to get the IMG_SRC variable under the callback function of the open menu? This is where img_src is used as a shared data.
Many interface programming friends, like to use the global statement. I personally do not like this use, because there is a better way. That's two functions with Setappdata and getappdata. We can give the world
Add the application data to any control on the polygon that has the Tag property. Of course I prefer to add these shared application data uniformly to the figure window, because it's easy to remember,
If a control is one, it doesn't feel easy to remember.  you will find in the. m file that in addition to the callback function for each menu item, there are two functions:PJIMAGE_OPENINGFCN and PJIMAGE_OUTPUTFCN. and Pjimage_ OPENINGFCN is equivalent to the initialization function of the interface, while PJIMAGE_OUTPUTFCN is the output function of the interface.
That is , when you do not run the fig and call the. m file when you return the value. So, we're going to add the following program to the PJIMAGE_OPENINGFCN to share the IMG_SRC matrix. The code is as follows:
Setappdata (handles.figure_pjimage, ' img_src ', 0);
then, at the end of the M_file_open_callback function, write the following program:
Setappdata (handles.figure_pjimage, ' img_src ', img_src);
So, we can extract img_src like this in the M_file_save_callback function, as follows:
Img_src=getappdata (handles.figure_pjimage, ' img_src ');
When you save it, you will naturally use the Save dialog box. To use the Save dialog box, you need to use the Uiputfile function, specific please use Help uiputfile view.
then, save the Program (M_file_save_callback) under the menu item, and you can write:
[filename, pathname] = Uiputfile ({' *.bmp ', ' BMP files '; ') *.jpg; ', ' jpgfiles '}, ' Pick an Image '); if IsEqual (filename,0) | | IsEqual (pathname,0) return;% if the "Cancel" elsefpath=fullfile (pathname, filename) is ordered, and the% gets the full path of another method Endimg_src=getappdata ( Handles.figure_pjimage, ' img_src ');% get open picture data imwrite (img_src,fpath);% Save picture
Below is the program that exits the menu item. To exit the interface, just use the close function, but usually you'll be prompted
the. For example, if you are working on a picture, and you do not save the processed picture, you should turn it off.
prompts to ask if you want to save. However, here, we do not do this work, and so on after the need to
write it. Therefore, the procedure for exiting a menu item here is a sentence, as follows:
Close (handles.figure_pjimage);
In fact, with the Delete function is also possible, that is: Delete (handles.figure_pjimage); look at your mood.
However, when you run the program, you will find that when you open the image, if you click the "Cancel" button, then the Command window in MATLAB will pop up the error, it is because we did not deal with the cancellation of the situation.
Now let's deal with this problem, as long as the M_file_open_callback the following program to change to the following program:
[filename, pathname] = uigetfile (... {' *.bmp;*.jpg;*.png;*.jpeg ', ' Image Files (*.bmp, *.jpg, *.png,*.jpeg) ';... ' *. * ', ' all Files (*. *} ', ... ' Pick an Image ') ; if IsEqual (filename,0) | | IsEqual (pathname,0), return;endaxes (HANDLES.AXES_SRC); Fpath=[pathname filename];img_src=imread (Fpath); Imshow (img _SRC); Setappdata (Handles.figure_pjimage, ' img_src ', img_src);

Appendix---PJIMAGE.M final source code:
function varargout = pjimage (varargin)% pjimage MATLAB code for pjimage.fig% Pjimage, by itself, creates a new Pjimag E or raises the existing% singleton*.%% H = Pjimage Returns the handle to a new pjimage or the handle to% t He existing singleton*.%% pjimage (' CALLBACK ', Hobject,eventdata,handles,...) calls the local% function named call Back in Pjimage. M with the given input arguments.%% pjimage (' property ', ' Value ',...) creates a new pjimage or raises the% Existin  G singleton*.  Starting from the left, property value pairs are% applied to the GUI before PJIMAGE_OPENINGFCN gets called.  an% Unrecognized property name or invalid value makes property application% stop.  All inputs is passed to PJIMAGE_OPENINGFCN via varargin.%% *see GUI Options on the guide's Tools menu. Choose "GUI allows only one% instance to run (singleton)". percent See Also:guide, guidata, guihandles% Edit the above Tex T to modify the response to help pjimage% last Modified by guide v2.5 21-may-2016 13:35:10% Begin initialization code-do not Editgui_singleton = 1;gui_state = struct (                   ' Gui_name ', Mfilename, ...                   ' Gui_singleton ', Gui_singleton, ...                   ' GUI_OPENINGFCN ', @pjimage_OpeningFcn, ...                   ' GUI_OUTPUTFCN ', @pjimage_OutputFcn, ...                   ' GUI_LAYOUTFCN ', [], ... ' Gui_callback ', []); if Nargin && Ischar (varargin{1}) Gui_state.gui_callback = Str2func (Varargin{1}); EndIf NAR Gout [Varargout{1:nargout}] = GUI_MAINFCN (Gui_state, varargin{:}); else GUI_MAINFCN (Gui_state, varargin{:}); end% end Initialization code-do not edit%---executes just before pjimage is made Visible.function PJIMAGE_OPENINGFCN (HObject, E Ventdata, handles, varargin)% This function have no output args, see outputfcn.% hobject handle to figure% eventdata re Served-to be defined in a future version of matlab% handles structure with handles and user data (see GUIDATA)% vArargin command line arguments to Pjimage (see Varargin)% Choose default command line output for pjimagehandles.output =  hobject;% Update handles Structureguidata (hobject, handles);% uiwait makes pjimage wait for user response (see Uiresume)% Uiwait (Handles.figure_pjimage); Setappdata (Handles.figure_pjimage, ' img_src ', 0);%---Outputs from the This function is Returned to the command line.function varargout = PJIMAGE_OUTPUTFCN (Hobject, eventdata, handles)% Varargout cell array F or returning output args (see varargout),% hobject handle to figure% eventdata reserved-to is defined in a future VE Rsion of matlab% handles structure with handles and user data [see guidata]% Get default command line output from Handl Es structurevarargout{1} = handles.output;%--------------------------------------------------------------------  function M_file_callback (hobject, eventdata, handles)% Hobject handle to M_file (see GCBO)% eventdata reserved-to be Defined in a future version of matlab%Handles structure with handles and user data (see GUIDATA)%----------------------------------------------------------- ---------function M_file_open_callback (hobject, eventdata, handles)% Hobject handle to M_file_open (see GCBO)% Eventdat A reserved-to be defined in a future version of matlab% handles structure with handles and user data (see GUIDATA) [F    Ilename, pathname] = uigetfile (...    {' *.bmp;*.jpg;*.png;*.jpeg ', ' Image Files (*.bmp, *.jpg, *.png,*.jpeg) ';    ' *. * ', ' all Files (*. *) '}, ... ' Pick an image '); if IsEqual (filename,0) | | IsEqual (pathname,0), return;endaxes (HANDLES.AXES_SRC),% with axes command to set the axis of the current operation is Axes_srcfpath=[pathname filename];% Combine the file name and directory name into a full path%imshow (Imread (fpath)),% with Imread read the picture, and Imshow on AXES_SRC (Img_src=imread); Fpath (imshow ); Setappdata (Handles.figure_pjimage, ' img_src ', img_src);%------------------------------------------------------ --------------function M_file_save_callback (hobject, eventdata, handles)% Hobject handleTo M_file_save (see GCBO)% eventdata reserved-to BES defined in a future version of matlab% handles structure with HA Ndles and user data (see GUIDATA)% Img_src=getappdata (handles.figure_pjimage, ' img_src '); [filename, pathname] = Uiputfile ({' *.bmp ', ' BMP files '; ') *.jpg; ', ' jpg files '}, ' Pick an Image '); if IsEqual (filename,0) | | IsEqual (pathname,0) return;% if "Cancel" else fpath=fullfile (pathname, filename) is ordered, and the% obtains a full path of another method Endimg_src=getappdata ( Handles.figure_pjimage, ' img_src ');% get open picture data imwrite (img_src,fpath);% Save picture%------------------------------------- -------------------------------function M_file_exit_callback (hobject, eventdata, handles)% Hobject handle to M_file_ Exit (see GCBO)% eventdata reserved-to is defined in a future version of matlab% handles structure with handles and User data (see GUIDATA) Close (handles.figure_pjimage);%delete (handles.figure_pjimage);


An example of a MATLAB graphical user interface (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.