效果图
图1.主界面
图2.其中一个子界面
具体步骤
1.界面设置
输入guide并选中运行
2.菜单设置(图1上方)
3.背景图片
参考/leo0308/article/details/82694995
4.界面跳转
在自动生成的Untitled_5_Callback末尾加一句set(其余语句均自动生成,下略)
function Untitled_5_Callback(hObject, eventdata, handles)% hObject handle to Untitled_5 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)set(interface4,'Visible','on') ;
添加一个菜单、文本框、图、坐标轴和按钮等(对fig文件操作),都会自动生成对应的函数(对m文件操作)(如按钮为callback),写在这个函数内函数就是点击该按钮执行的语句,右击改按钮打开检查器可查看属性,修改属性后按回车保存。
4.输入提示框
指图2内的步骤数,随机种子等,添加静态文本,打开检查器,修改string属性为步骤数
输入框和坐标轴同理
5.分析按钮
分以下几步:
(1)输入框提取,设置变量名
J=get(handles.edit1,'String');J=str2double(J);
(2)算法代码略。
(3)画图,这里坐标轴改了属性名
plot(handles.axes1,[0:js-1],v(2,1:js));
(4)输出数据到excel
xlswrite('interface4sta.xlsx',v(2,1:js),'Sheet1');
6.GUI转exe文件
参考/caizanhui/article/details/112582223
需要注意的是,生成的exe并不能脱离matlab运行,需要从matlab官网安装一个支持文件(不是整个matlab),文件安装速度很快,但32位系统可能不支持。
图1、图2的全部代码展示,供改错参考,直接复制无法运行,想要传输必须传输扩展名为m和fig的源文件。
interface1
function varargout = interface1(varargin)% INTERFACE1 MATLAB code for interface1.fig%INTERFACE1, by itself, creates a new INTERFACE1 or raises the existing%singleton*.%%H = INTERFACE1 returns the handle to a new INTERFACE1 or the handle to%the existing singleton*.%%INTERFACE1('CALLBACK',hObject,eventData,handles,...) calls the local%function named CALLBACK in INTERFACE1.M with the given input arguments.%%INTERFACE1('Property','Value',...) creates a new INTERFACE1 or raises the%existing singleton*. Starting from the left, property value pairs are%applied to the GUI before interface1_OpeningFcn gets called. An%unrecognized property name or invalid value makes property application%stop. All inputs are passed to interface1_OpeningFcn via varargin.%%*See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one%instance to run (singleton)".%% See also: GUIDE, GUIDATA, GUIHANDLES% Edit the above text to modify the response to help interface1% Last Modified by GUIDE v2.5 06-May- 22:58:17% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @interface1_OpeningFcn, ...'gui_OutputFcn', @interface1_OutputFcn, ...'gui_LayoutFcn', [] , ...'gui_Callback', []);if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});elsegui_mainfcn(gui_State, varargin{:});end% End initialization code - DO NOT EDIT% --- Executes just before interface1 is made visible.function interface1_OpeningFcn(hObject, eventdata, handles, varargin)% This function has no output args, see OutputFcn.% hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% varargin command line arguments to interface1 (see VARARGIN)% Choose default command line output for interface1handles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes interface1 wait for user response (see UIRESUME)% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line.function varargout = interface1_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT);% hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Get default command line output from handles structurevarargout{1} = handles.output;% --------------------------------------------------------------------function Untitled_1_Callback(hObject, eventdata, handles)% hObject handle to Untitled_1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% --------------------------------------------------------------------function Untitled_2_Callback(hObject, eventdata, handles)% hObject handle to Untitled_2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% --------------------------------------------------------------------function Untitled_3_Callback(hObject, eventdata, handles)% hObject handle to Untitled_3 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)set(interface2,'Visible','on') ;% --------------------------------------------------------------------function Untitled_4_Callback(hObject, eventdata, handles)% hObject handle to Untitled_4 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)set(interface3,'Visible','on') ;% --------------------------------------------------------------------function Untitled_5_Callback(hObject, eventdata, handles)% hObject handle to Untitled_5 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)set(interface4,'Visible','on') ;% --- Executes during object creation, after setting all properties.function figure1_CreateFcn(hObject, eventdata, handles)% hObject handle to figure1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns calledha=axes('units','normalized','pos',[0 0 1 1]);uistack(ha);ii=imread('mainpic.png');image(ii);colormap grayset(ha,'handlevisibility','off','visible','off');% --------------------------------------------------------------------function Untitled_6_Callback(hObject, eventdata, handles)% hObject handle to Untitled_6 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% --------------------------------------------------------------------function Untitled_7_Callback(hObject, eventdata, handles)% hObject handle to Untitled_7 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% --------------------------------------------------------------------function Untitled_8_Callback(hObject, eventdata, handles)% hObject handle to Untitled_8 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% --------------------------------------------------------------------function Untitled_9_Callback(hObject, eventdata, handles)% hObject handle to Untitled_9 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% --------------------------------------------------------------------function Untitled_10_Callback(hObject, eventdata, handles)% hObject handle to Untitled_10 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% --------------------------------------------------------------------function Untitled_11_Callback(hObject, eventdata, handles)% hObject handle to Untitled_11 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% --------------------------------------------------------------------function Untitled_12_Callback(hObject, eventdata, handles)% hObject handle to Untitled_12 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% --------------------------------------------------------------------function Untitled_13_Callback(hObject, eventdata, handles)% hObject handle to Untitled_13 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)
interface4
function varargout = interface4(varargin)% INTERFACE4 MATLAB code for interface4.fig%INTERFACE4, by itself, creates a new INTERFACE4 or raises the existing%singleton*.%%H = INTERFACE4 returns the handle to a new INTERFACE4 or the handle to%the existing singleton*.%%INTERFACE4('CALLBACK',hObject,eventData,handles,...) calls the local%function named CALLBACK in INTERFACE4.M with the given input arguments.%%INTERFACE4('Property','Value',...) creates a new INTERFACE4 or raises the%existing singleton*. Starting from the left, property value pairs are%applied to the GUI before interface4_OpeningFcn gets called. An%unrecognized property name or invalid value makes property application%stop. All inputs are passed to interface4_OpeningFcn via varargin.%%*See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one%instance to run (singleton)".%% See also: GUIDE, GUIDATA, GUIHANDLES% Edit the above text to modify the response to help interface4% Last Modified by GUIDE v2.5 10-Oct- 16:33:20% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @interface4_OpeningFcn, ...'gui_OutputFcn', @interface4_OutputFcn, ...'gui_LayoutFcn', [] , ...'gui_Callback', []);if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});elsegui_mainfcn(gui_State, varargin{:});end% End initialization code - DO NOT EDIT% --- Executes just before interface4 is made visible.function interface4_OpeningFcn(hObject, eventdata, handles, varargin)% This function has no output args, see OutputFcn.% hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% varargin command line arguments to interface4 (see VARARGIN)% Choose default command line output for interface4handles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes interface4 wait for user response (see UIRESUME)% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line.function varargout = interface4_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT);% hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Get default command line output from handles structurevarargout{1} = handles.output;function edit1_Callback(hObject, eventdata, handles)% hObject handle to edit1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of edit1 as text% str2double(get(hObject,'String')) returns contents of edit1 as a double% --- Executes during object creation, after setting all properties.function edit1_CreateFcn(hObject, eventdata, handles)% hObject handle to edit1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction edit2_Callback(hObject, eventdata, handles)% hObject handle to edit2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of edit2 as text% str2double(get(hObject,'String')) returns contents of edit2 as a double% --- Executes during object creation, after setting all properties.function edit2_CreateFcn(hObject, eventdata, handles)% hObject handle to edit2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction edit3_Callback(hObject, eventdata, handles)% hObject handle to edit3 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of edit3 as text% str2double(get(hObject,'String')) returns contents of edit3 as a double% --- Executes during object creation, after setting all properties.function edit3_CreateFcn(hObject, eventdata, handles)% hObject handle to edit3 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction edit4_Callback(hObject, eventdata, handles)% hObject handle to edit4 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of edit4 as text% str2double(get(hObject,'String')) returns contents of edit4 as a double% --- Executes during object creation, after setting all properties.function edit4_CreateFcn(hObject, eventdata, handles)% hObject handle to edit4 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction edit5_Callback(hObject, eventdata, handles)% hObject handle to edit5 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of edit5 as text% str2double(get(hObject,'String')) returns contents of edit5 as a double% --- Executes during object creation, after setting all properties.function edit5_CreateFcn(hObject, eventdata, handles)% hObject handle to edit5 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction edit6_Callback(hObject, eventdata, handles)% hObject handle to edit6 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of edit6 as text% str2double(get(hObject,'String')) returns contents of edit6 as a double% --- Executes during object creation, after setting all properties.function edit6_CreateFcn(hObject, eventdata, handles)% hObject handle to edit6 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction edit7_Callback(hObject, eventdata, handles)% hObject handle to edit7 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of edit7 as text% str2double(get(hObject,'String')) returns contents of edit7 as a double% --- Executes during object creation, after setting all properties.function edit7_CreateFcn(hObject, eventdata, handles)% hObject handle to edit7 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction edit8_Callback(hObject, eventdata, handles)% hObject handle to edit8 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of edit8 as text% str2double(get(hObject,'String')) returns contents of edit8 as a double% --- Executes during object creation, after setting all properties.function edit8_CreateFcn(hObject, eventdata, handles)% hObject handle to edit8 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction edit9_Callback(hObject, eventdata, handles)% hObject handle to edit9 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of edit9 as text% str2double(get(hObject,'String')) returns contents of edit9 as a double% --- Executes during object creation, after setting all properties.function edit9_CreateFcn(hObject, eventdata, handles)% hObject handle to edit9 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction edit10_Callback(hObject, eventdata, handles)% hObject handle to edit10 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of edit10 as text% str2double(get(hObject,'String')) returns contents of edit10 as a double% --- Executes during object creation, after setting all properties.function edit10_CreateFcn(hObject, eventdata, handles)% hObject handle to edit10 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');endfunction edit11_Callback(hObject, eventdata, handles)% hObject handle to edit11 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% Hints: get(hObject,'String') returns contents of edit11 as text% str2double(get(hObject,'String')) returns contents of edit11 as a double% --- Executes during object creation, after setting all properties.function edit11_CreateFcn(hObject, eventdata, handles)% hObject handle to edit11 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: edit controls usually have a white background on Windows.% See ISPC and COMPUTER.if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))set(hObject,'BackgroundColor','white');end% --- Executes on button press in pushbutton1.function pushbutton1_Callback(hObject, eventdata, handles)% hObject handle to pushbutton1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)%% setupN=2;% dimension of stateI=eye(N);% identity operatorJ=get(handles.edit1,'String');J=str2double(J);% number of steps J=1e3;gamma=get(handles.edit2,'String');gamma=str2double(gamma);% observational noise variance is gamma^2*I gamma=1;sigma=get(handles.edit3,'String');sigma=str2double(sigma);% dynamics noise variance is sigma^2*I sigma=1;C0=get(handles.edit4,'String');C0=str2double(C0);% prior initial condition variance C0=eye(2);m1=get(handles.edit5,'String');m1=str2double(m1);m2=get(handles.edit6,'String');m2=str2double(m2);m0=[m1;m2];% prior initial condition mean m0=[0;0];sd=get(handles.edit7,'String');sd=str2double(sd);rng(sd);% choose random number seed sd=10;A1=get(handles.edit8,'String');A1=str2double(A1);A2=get(handles.edit9,'String');A2=str2double(A2);A3=get(handles.edit10,'String');A3=str2double(A3);A4=get(handles.edit11,'String');A4=str2double(A4);A=[A1 A2;A3 A4];% dynamics determined by A A=[0 1;-1 0];m=zeros(N,J);v=m;y=zeros(J,1);c=zeros(N,N,J);% pre-allocatev(:,1)=m0+sqrtm(C0)*randn(N,1);% initial truthm(:,1)=10*randn(N,1);% initial mean/estimatec(:,:,1)=100*C0;% initial covarianceH=[1,0];% observation operator%% solution % assimilate!for j=1:J v(:,j+1)=A*v(:,j) + sigma*randn(N,1);% truthy(j)=H*v(:,j+1)+gamma*randn;% observationmhat=A*m(:,j);% estimator predictchat=A*c(:,:,j)*A'+sigma^2*I;% covariance predict d=y(j)-H*mhat;% innovationK=(chat*H')/(H*chat*H'+gamma^2);% Kalman gainm(:,j+1)=mhat+K*d;% estimator updatec(:,:,j+1)=(I-K*H)*chat;% covariance update endjs=21;plot(handles.axes1,[0:js-1],v(2,1:js));plot(handles.axes1,[0:js-1],m(2,1:js)+reshape(sqrt(c(2,2,1:js)),1,js),'r--');plot(handles.axes1,[0:js-1],m(2,1:js)-reshape(sqrt(c(2,2,1:js)),1,js),'r--');xlswrite('interface4sta.xlsx',v(2,1:js),'Sheet1');%plot(handles.axes1,[0:J],reshape(c(1,1,:)+c(2,2,:),J+1,1));%plot([0:J],cumsum(reshape(c(1,1,:)+c(2,2,:),J+1,1))./[1:J+1]','m', ...%'Linewidth',2); grid; hold;xlabel('iteration, j');axis([1 1000 0 50]);%title('Kalman Filter Covariance, Ex. 1.2');%figure;plot([0:J],sum((v-m).^2));hold;%plot([0:J],cumsum(sum((v-m).^2))./[1:J+1],'m','Linewidth',2);grid%hold;xlabel('iteration, j');axis([1 1000 0 50]);%title('Kalman Filter Error, Ex. 1.2')% --- Executes on button press in pushbutton2.function pushbutton2_Callback(hObject, eventdata, handles)% hObject handle to pushbutton2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)% --- Executes during object creation, after setting all properties.function text2_CreateFcn(hObject, eventdata, handles)% hObject handle to text2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% --- Executes during object creation, after setting all properties.function axes1_CreateFcn(hObject, eventdata, handles)% hObject handle to axes1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: place code in OpeningFcn to populate axes1
如果觉得《Matlab制作GUI》对你有帮助,请点赞、收藏,并留下你的观点哦!