Recently in the use of Matlab to do a GUI user interface is mostly Baidu come out to explore the learning to summarize some of the problems encountered and solutions
One:
animate text in a text box (retains the previous text and displays the current text content):
The basic syntax for displaying text in a text box is
Set (Handles.listbox2,'string',string);
<1> want to keep the previous text content using the code provided online TextWrap function
(But when =get from str (handles.listbox2, ' String '); Get str put into string{} inside TextWrap will prompt error .... orz error message forgot)
Figure'units','normalized','position',[0.4 0.4 0.4 0.3]); h= Uicontrol ('Style','Text','fontsize', -);string= {'Why is a static text box static? ','because you can't scroll through it like an edit box','If you want to display multiple lines in a static text box','in this way, you can achieve','Call the TextWrap function! '}; [Outstring,newpos]= TextWrap (H,string);Set(H,'String', Outstring,'Position', Newpos);
<2> later wanted to update txt and then display the TXT content in the text box. The problem encountered is that the characters in the text box are read from a TXT document one line at a time (; ′⌒ ')
<3> the following are common methods (Baidu long-time to find)
There is no logical or grammatical error in this way, but I have a problem that has not been solved so far: the ideal case, the program side run side displays the text content in the text box
Real-World can't display text content in real time wait until the program runs to show all the updates are too slow don't know if this situation can be improved
Get (Handles.listbox2,'String'=sprintf ('XXXXXX' ) ); Ss{end+1}=str; Set (Handles.listbox2,'String', SS);
Second: parameter transfer between MATLAB buttons
<1> the first way of passing parameters is more suitable for passing between multiple GUI if a single GUI pass will face a txt text that retains previous data or overwrites previous data (e.g. you may use one data multiple times)
Setappdata (0,'a', a) a=getappdata (0,'a ').
<2> the second way to encounter an unknown error, like when I'm using it, it's weird, and it doesn't implement parameter passing.
Handles.in1 = varargin{1= varargin{2};guidata (hobject, handles); % Save
<3> The Third Way is I think the simplest is also a way I often use but the simple question is that sometimes you get a headache! Because in every place where you want to go to the global variable, you add a gloal x, and If you change the value of x in one place, all the x values change! Sometimes I'm afraid there will be disorder. Another more important problem is that, in the words of C + +, global variables undermine the encapsulation of the program! Therefore, global variables can be used sparingly and sparingly. (It happens that my program does not need to modify their values simply to use)
<4> There are other ways to see http://blog.sina.com.cn/s/blog_6163bdeb0100i2j4.html
Three: Show when the program is running
tic; /..../% Code toc;sprintf ('%s ', Num2str (TOC)); % shows the time that the program ran
Timer class timers (should also write a real-time processing of its data, there is no research)
T=timer ('timerfcn', {@timercallback, handles},'executionmode' 'fixedrate'Period'0.01); Start ( t);
Four: the MATLAB Kmeans function attention Place
when the data to be clustered has only one set of vectors ...
[IDX1,C1,SUMD1,D1] = Kmeans (T,1);
C1 is a 1*1 number Orz and the default distance is square Euclidean distance (⊙﹏⊙) b pits me for a long time ><!!
Five: GUI dialog box design (copy easy to see)
from:http://heatflow.blog.163.com/blog/static/193360279201172191146666/
Dialog Create dialog box
ERRORDLG Create error dialog box
HELPDLG Create Help dialog box
InputDlg Creating an Input dialog box
LISTDLG Create selection List dialog box
MsgBox Creating a message dialog box
Pagedlg The Layout dialog box showing the page
PrintDlg Display Print dialog box
Questdlg Problem dialog box
Uigetfile File Retrieval dialog box
Uiputfile The Search dialog box that is displayed for writing
Uisetcolor interactively set the Colorspec of an object from a dialog box
Uisetfont interactively setting object font characteristics
Warndlg warning Dialog box
* Dialog design: In graphical user interface programming, dialog boxes are important information to display and get input data for user interface objects.
1. Public dialog box:
The common dialog box is a dialog box that leverages Windows resources, including file open, file save, color settings, font settings, print settings, and so on.
1) File Open dialog box: for opening files
Uigetfile
Uigetfile (' Filterspec ')
Uigetfile (' Filterspec ', ' DialogTitle ')
Uigetfile (' Filterspec ', ' DialogTitle ', x, y)
[Fname,pname]=uigetfile (...)
2) File Save dialog box: for saving files
Uiputfile
Uiputfile (' Initfile ')
Uiputfile (' Initfile ', ' DialogTitle ')
Uiputfile (' Initfile ', ' DialogTitle ', x, y)
[Fname,pname]=uiputfile (...)
3) Color Settings dialog box: Interactive settings for drawing object colors
C=uisetcolor (' H_or_c, ' dialogtitle ')
4) Font Settings dialog box: Interactive settings for font properties
Uisetfont
Uisetfont (h)
Uisetfont (S)
Uisetfont (H, ' dialogtitle ')
Uisetfont (S, ' DialogTitle ')
S=uisetfont (...)
5) Print Setup dialog box: Interactive settings for printing pages
Dlg=pagesetupdlg (Fig)
Pagedlg
Pagedlg (Fig)
6) Print Preview dialog box: Used to preview the printed page
PrintPreview
PrintPreview (f)
7) Print dialog box :
PrintDlg
PrintDlg (Fig)
PrintDlg ('-crossplatform ', fig)
PrintDlg (-' setup ', fig)
2, MATLAB Special dialog box
1) Error message dialog box: used to prompt for error messages
ERRORDLG Open the default error message dialog box
Errordlg (' errorstring ') Open the Error message dialog box showing ' errorstring ' information
Errordlg (' errorstring ', ' Dlgname ') Opens the error message dialog box that displays ' errorstring ' information, and the title of the dialog is specified by ' Dlgname '
Erordlg (' errorstring ', ' dlgname ', ' on ') Opens the error message dialog box that displays the ' errorstring ' message, and the title of the dialog box is specified by ' Dlgname '. If the dialog box already exists, the ' on ' parameter displays the dialog box at the front.
H=errodlg (...) return dialog handle
Example: Errordlg (' input error, please re-enter ', ' Error message ')
2) Help dialog box: For help tip information
Helpdlg Open the Default Help dialog box
Helpdlg (' helpstring ') opens the Help dialog box that displays ' errorstring ' information,
Helpdlg (' helpstring ', ' Dlgname ') opens the Help dialog box that displays ' errorstring ' information, and the title of the dialog box is specified by ' Dlgname '
H=helpdlg (...) return dialog handle
Example: Helpdlg (' matrix size must be equal ', ' online Help ')
3) Input dialog box: for entering information
Answer=inputdlg (Prompt) Opens the Input dialog box, prompt is an array of cells that defines the number of input data windows and the display prompt, answer as an array of cells for storing input data.
Answer=inputdlg (Prompt,title) is the same as the previous, title determines the caption of the dialog box.
The Answer=inputdlg (prompt,title,lineno) parameter Lineno can be a scalar, column vector, or MX2-order matrix, which, if scalar, indicates that the number of rows per input window is Lineno, and if it is a column vector, the number of rows per input window is determined by the column vector Each element of the Lineno is determined; in the case of a matrix, each element corresponds to an input window, the first column of each row is the number of rows in the input window, and the second column is the width of the input window.
The Answer=inputdlg (prompt,title,lineno,defans) parameter Defans is an array of cells that stores the default values for each input data, the number of elements must be the same as the number of input Windows defined by prompt, and all elements must be strings.
The Answer=inputdlg (prompt,title,lineno,defans,sesize) parameter resize determines whether the size of the input dialog box can be adjusted and the optional value is on or off.
Example: prompt={' input Name ', ' Input age '};
Title= ' Input Name and age ';
Lines=[2 1] ';
def={' John Smith ', ' 35 '};
Answer=inputdlg (PROMPT,TITLE,LINES,DEF);
4) List selection dialog box: used to select the desired value in multiple options
[Selection,ok]=listdlg (' liststring ', S,...) output parameter selection is a vector that stores the index number of the selected list item,
Input parameters are optional ' liststring ' (unit array), ' SelectionMode ' (' single ' or ' multiple ', ' listsize ' ([wight,height]), ' Name ' (dialog box title), etc.
5) Message prompt dialog box: Used to display prompt information
MsgBox (Message) Opens a message prompt dialog box that displays message information.
MsgBox (Message,title) title determines the title of the dialog box.
MsgBox (Message,title, ' icon ') icon for displaying icons, optional icons include: none (no icon)/error/help/warn/custom (user defined)
MsgBox (Message,title, ' Custom ', Icondata,iconcmap) when the user-defined icon is used, Icondata is the image data that defines the icon, iconcmap the color map for the image.
MsgBox (..., ' Creatmode ') Select the mode Creatmode, with the options: Modal, Non_modal, and replace.
H=msgbox (...) return dialog handle
6) Question Prompt dialog: A variety of options for answering questions
Button=questdlg (' qstring ') Opens the question prompt dialog box with three buttons: Yes,no and Cancel, and ' Questdlg ' to determine the prompt.
Button=questdlg (' qstring ', ' title ') title determines the title of the dialog box.
Button=questdlg (' qstring ' title ', ' Default ') when you press ENTER, the default value of default must be one of Yes,no or cancel.
Button=questdlg (' qstring ', ' title ', ' str1 ', ' str2 ', ' Default ') Open the question prompt dialog box with two buttons, respectively, determined by str1 and str2, ' Qstdlg ' determines the message, default must be one of str1 or str2.
Button=questdlg (' qstring ', ' title ', ' str1 ', ' str2 ', ' str3 ', ' Default ') Open the question prompt dialog box with three buttons, respectively, determined by STR1, str2 and Str3, ' Qstdlg ' determines the message, default must be one of str1, str2, or STR3.
7) Process bar: Graphical display of operations or processes
H=waitbar (x, ' title ') displays the process bar titled title, X is the proportional length of the process bar, its value must be between 0 and 1, and H is the handle to the returned process bar object.
Waitbar (x, ' title ', ' Creatcancelbtn ', ' button_callback ') creates a undo button on the process bar using the CREATCANCELBTN parameter, and pressing the Undo button in the process will call Button_ The callback function.
Waitbar (..., property_name,property_value,...) selects other parameters defined by Prompt_name, and the value of the parameter is specified by Prompt_value.
Cases:
H=waitbar (0, ' pleas wait ... ');
For i=1:10000
Waitbar (I/10000,H)
End
Close (h)
Standard graphical user interface for MATALB
1. Message dialog box
The MsgBox function. It does not accept any input from the user, and after the user clicks the OK button, the dialog box automatically closes and then returns to the program to continue execution.
>> MsgBox (' Welcome to MATLAB ') build an untitled message dialog box
>> MsgBox (' Welcome to Matlab ', ' matalb ', ' warn ') Make a Header message dialog box
2. Error dialog box
The Errordlg function. >> Errordlg% Set up a default parameter error dialog box
>> Errordlg (' This is an error dialog ', ' matlab error ')% build an error dialog named MATLAB errors
3. Warning dialog box
The Warndlg function. >> Warndlg% Set up a warning dialog box with default parameters
>> Warndlg (' This is a warning dialog ', ' MATLAB warning ')
4. Help dialog box
The Helpdlg function.
>> Helpdlg (' Do you need help? ', ' MATLAB help ')
5. Progress Bar Setup dialog box
The Waitbar function. The function is to reflect the completion of the program run.
H=waitbar (0, ' Please wait a moment ... ')
For i=1:10000
Waitbar (i/10000)
End
6. Enter the dialog box
The InputDlg function.
prompt={' input trapezoid's bottom: ', ' input trapezoid bottom ', ' input trapezoid high '};% set hint string
Name= ' Enter Trapeaia Data ';% setting title
numlines=1;% Specifies the number of rows for the input data
defans={' 20 ', ' 50 ', ' 40 '};% set default values
Resize= ' on ';% setting dialog box size Adjustable
Answer=inputdlg (Prompt,name,numlines,defans, ' on ')% Create Input dialog box
7. List dialog box
The Listdlg function.
P=path (Path, ' C:\MATLAB7\work ');% set the path, if it is the current directory, you can omit this step
D=dir (' C:\MATLAB7\work ');% gets the pointer, if it is the current directory, the D=dir
str={d.name};% get the file name within the directory
[Sel,ok]=listdlg (' liststring ', str, ' promptstring ', ' Please select file ', ' SelectionMode ', ' multiple ')% Create List dialog box.
After the file selection succeeds, click the "OK" button, the system will select a good file sequence number to the corresponding vector sel, the value of the parameter OK is 1.
8. Question dialog Box
The Questdlg function. Ask a question and wait for the user to answer. The default answer button is three: Yes, No, Cancel. When the user clicks on any one of the buttons, the system saves the button name in a variable (buttonname). If the user closes the dialog box, the system buttonname to empty.
>> Buttonname=questdlg (' Are you ready? ', ' MATLAB quest ', ' Yes ', ' No ', ' Cancel ', ' yes ')
9. Path selection dialog box
The Uigedir function. Used to find and select paths for users.
>> directoryname=uigetdir (' C:\MATLAB7\work ', ' Browse documents ')
10. Select File dialog box
The Uigetfile function. Used to select the required files for the user.
①>> [Fn,pn,fi]=uigetfile (' *.m ', ' Select a m-file ')% open a Select M file dialog box and select a file
②[fn,pn,fi]=uigetfile (...
{' *.m;*.fig;*.mat; ', ' All MATLAB File (*.m,*.fig,*.mat,*.mdl) ';
' *.m ', ' M-file (*.M) ';
' *.fig ', ' figure (*.fig) ';
' *.mat ', ' Mat-flie (*.mat) ';
' *.mdl ', ' Models (*.mdl) ';
' *. * ', ' all Flies (*. *) '},...
' Pick a file ')
% Open a dialog box to select multiple files and select a file
11. File Save dialog box
The Uiputfile function. Used to save the file.
①>> [Fn,pn,fi]=uiputfile (' *.m ', ' Save selected a M-file ')
②[fn,pn,fi]=uiputfile (...
{' *.m;*.fig;*.mat; ', ' All MATLAB File (*.m,*.fig,*.mat,*.mdl) ';
' *.m ', ' M-file (*.M) ';
' *.fig ', ' figure (*.fig) ';
' *.mat ', ' Mat-flie (*.mat) ';
' *.mdl ', ' Models (*.mdl) ';
' *. * ', ' all Flies (*. *) '},...
' Save a file ')
% create multiple dialog boxes for saving files and select a file
12. Page Setup dialog box
The Pagesetupdlg function. Used to set the various properties and parameters of the page. The calling format is:
Dlg=pagesetupdlg (Fig)
This command creates a Page Setup dialog box where the user can set various properties and parameters for the page. This command only supports page setup for a single graphics window, and the parameter fig must also be a single graphic handle, not a graph handle vector or simulink diagram.
>> fig=figure;
>> Dlg=pagesetupdlg (Fig)
.Print Setup dialog box
The PrintDlg function. Various printing parameters can be set by the user.
X=[-PI:0.02:PI];
Y=cos (x);
Fig=figure;
Plot (x, y);
PrintDlg (fig);% Open Windows Print dialog box
PrintDlg ('-crossplatform ', fig);% Open matlab Print dialog box
14. Color Settings dialog box
The Uisetcolor function. The foreground or background color for the user to set the graphic.
①>> C=uisetcolor% Set up a color selection dialog box
②% creates a color dialog box, sets the initial color of the dialog box, and then selects a color to use for a drawing object.
X=[0:0.02:2*PI];
Y=sin (x);
H=figure;
Plot (x, y);
C=uisetcolor (H, ' select Color ')
15. Font Settings dialog box
The Uisetfont function. A font property that is used to set or modify the text font, axis, or display text of a control object.
①>> S=uisetfont% set up a default font settings dialog box and set a font.
② to set the text in a control
T1=uicontrol (' style ', ' pushbutton ', ' string ', ' OK ',...
' Position ', [200 320 60 20]); % Create a button
T2=uicontrol (' style ', ' pushbutton ', ' string ', ' Cancel ',...
' Position ', [200 220 60 20]); % Create a button
S=uisetfont (T1)% Open Font Settings dialog box
Set (t2,s)% copies the settings of the T1 to
Some summary of Matlab GUI