matlab GUI function&try catch

使用matlab GUI时,有时需要在某个回调函数中使用自己定义的函数。按照一般的情况,需要把函数放在程序末尾且funtion 需要以end结尾。但在GUI的程序中,各个回调函数本身就是函数,函数内部是不能再定义函数的。有鉴于此,在GUI中,可将function 放置程序末尾但不加end。如此便能正确调用。详情见程序。

clc;
clear;
close all;

sum=add(1,1);
disp(sum);

function output=add(a,b)

    output=a+b;      

end


%%%%%%%%-----GUI----------------

function pushbutton1_Callback(hObject, eventdata, handles)
    sum=add(1,1);
    disp(sum);
end

function output=add(a,b)

    output=a+b;

matlab也像Java语言那样,同样有异常处理机制。使用try   catch exception实现。try里的程序没有问题时,正常执行try里的语句,当出现异常时,执行catch exception中的语句。详情见程序。

try 
   
    a=load('123.txt');

catch exception 

    a=load('123.mat');

end

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现一个基本的 Matlab 计算器 GUI 可以按照以下步骤进行: 1. 创建一个新的 Matlab GUI 程序,可以使用 GUIDE 工具来实现。 2. 在 GUI 界面上添加一个文本框用于显示计算结果,以及一个按钮用于触发计算操作。 3. 使用 Matlab 的 eval 函数对计算表达式进行求值,并将结果显示在文本框。 4. 添加一些输入限制和错误处理,例如不能输入非数值字符等。 下面是一份简单的 Matlab 计算器 GUI 代码示例: ```matlab function varargout = calculator_gui(varargin) % CALCULATOR_GUI MATLAB code for calculator_gui.fig % CALCULATOR_GUI, by itself, creates a new CALCULATOR_GUI or raises the existing % singleton*. % % H = CALCULATOR_GUI returns the handle to a new CALCULATOR_GUI or the handle to % the existing singleton*. % % CALCULATOR_GUI('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in CALCULATOR_GUI.M with the given input arguments. % % CALCULATOR_GUI('Property','Value',...) creates a new CALCULATOR_GUI or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before calculator_gui_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to calculator_gui_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 calculator_gui % Last Modified by GUIDE v2.5 09-Jul-2021 17:27:14 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @calculator_gui_OpeningFcn, ... 'gui_OutputFcn', @calculator_gui_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT % --- Executes just before calculator_gui is made visible. function calculator_gui_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 calculator_gui (see VARARGIN) % Choose default command line output for calculator_gui handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes calculator_gui wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = calculator_gui_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 structure varargout{1} = handles.output; % --- Executes on button press in calculate_button. function calculate_button_Callback(hObject, eventdata, handles) % hObject handle to calculate_button (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get the input expression from the edit text box input_expression = get(handles.input_edit, 'String'); % Evaluate the expression try result = eval(input_expression); set(handles.result_text, 'String', num2str(result)); catch set(handles.result_text, 'String', 'Error'); end % --- Executes during object creation, after setting all properties. function input_edit_CreateFcn(hObject, eventdata, handles) % hObject handle to input_edit (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 ``` 在 GUIDE 工具创建一个新的 GUI 程序,添加一个文本框和一个按钮,并为按钮添加一个回调函数 `calculate_button_Callback`。在回调函数使用 `eval` 函数对输入的表达式进行求值,并将结果显示在文本框。同时,添加一些输入限制和错误处理,例如不能输入非数值字符等。 上述代码仅供参考,可以根据需要进行修改和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值