【matlab】GUI中nargin函数 用法

nargin - Number of function input arguments
    This MATLAB function returns the number of input arguments passed in the call to
    the currently executing function.
    nargin
    nargin(fx)
nargin是用来判断输入变量个数的函数,这样就可以针对不同的情况执行不同的功能。通常可以用他来设定一些默认值,如下面的函数。

例子:函数test的功能为输出a加b之和。
 
 
  • 只输入一个变量,则认为另一个变量为0
  • 没有变量输入,则默认两者均为0
function y=test1(a,b) if nargin==0 a=0;b=0; elseif nargin==1 b=0; end y=a+b;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面整理了一份MATLAB GUI图像处理重用函数的示例代码: ```matlab function varargout = ImageProcessingGUI(varargin) % IMAGEPROCESSINGGUI MATLAB code for ImageProcessingGUI.fig % IMAGEPROCESSINGGUI, by itself, creates a new IMAGEPROCESSINGGUI or raises the existing % singleton*. % % H = IMAGEPROCESSINGGUI returns the handle to a new IMAGEPROCESSINGGUI or the handle to % the existing singleton*. % % IMAGEPROCESSINGGUI('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in IMAGEPROCESSINGGUI.M with the given input arguments. % % IMAGEPROCESSINGGUI('Property','Value',...) creates a new IMAGEPROCESSINGGUI or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before ImageProcessingGUI_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to ImageProcessingGUI_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 ImageProcessingGUI % Last Modified by GUIDE v2.5 29-May-2021 20:22:11 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @ImageProcessingGUI_OpeningFcn, ... 'gui_OutputFcn', @ImageProcessingGUI_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 ImageProcessingGUI is made visible. function ImageProcessingGUI_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 ImageProcessingGUI (see VARARGIN) % Choose default command line output for ImageProcessingGUI handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes ImageProcessingGUI wait for user response (see UIRESUME) % uiwait(handles.figure1); % Initialize variables handles.originalImage = []; handles.processedImage = []; handles.ROI = []; % Update handles structure guidata(hObject, handles); % --- Outputs from this function are returned to the command line. function varargout = ImageProcessingGUI_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 loadImageButton. function loadImageButton_Callback(hObject, eventdata, handles) % hObject handle to loadImageButton (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Open file dialog to choose image file [filename, pathname] = uigetfile({'*.jpg;*.png;*.bmp;*.tif'}, 'Select Image File'); if isequal(filename,0) || isequal(pathname,0) return; end % Load image handles.originalImage = imread(fullfile(pathname, filename)); % Display image axes(handles.originalImageAxes); imshow(handles.originalImage); % Update handles structure guidata(hObject, handles); % --- Executes on button press in resetButton. function resetButton_Callback(hObject, eventdata, handles) % hObject handle to resetButton (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Reset original image and processed image handles.originalImage = []; handles.processedImage = []; % Clear axes cla(handles.originalImageAxes, 'reset'); cla(handles.processedImageAxes, 'reset'); % Update handles structure guidata(hObject, handles); % --- Executes on button press in grayscaleButton. function grayscaleButton_Callback(hObject, eventdata, handles) % hObject handle to grayscaleButton (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Check if image is loaded if isempty(handles.originalImage) return; end % Convert to grayscale handles.processedImage = rgb2gray(handles.originalImage); % Display processed image axes(handles.processedImageAxes); imshow(handles.processedImage); % Update handles structure guidata(hObject, handles); % --- Executes on button press in thresholdButton. function thresholdButton_Callback(hObject, eventdata, handles) % hObject handle to thresholdButton (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Check if image is loaded if isempty(handles.originalImage) return; end % Convert to grayscale if necessary if size(handles.originalImage, 3) == 3 handles.processedImage = rgb2gray(handles.originalImage); else handles.processedImage = handles.originalImage; end % Calculate threshold value level = graythresh(handles.processedImage); % Threshold image handles.processedImage = imbinarize(handles.processedImage, level); % Display processed image axes(handles.processedImageAxes); imshow(handles.processedImage); % Update handles structure guidata(hObject, handles); % --- Executes on button press in cropButton. function cropButton_Callback(hObject, eventdata, handles) % hObject handle to cropButton (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Check if image is loaded if isempty(handles.originalImage) return; end % Get ROI from user h = imrect(handles.originalImageAxes); handles.ROI = wait(h); % Crop image handles.processedImage = imcrop(handles.originalImage, handles.ROI); % Display processed image axes(handles.processedImageAxes); imshow(handles.processedImage); % Update handles structure guidata(hObject, handles); % --- Executes on button press in saveButton. function saveButton_Callback(hObject, eventdata, handles) % hObject handle to saveButton (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Check if image is loaded if isempty(handles.originalImage) return; end % Open file dialog to choose save location [filename, pathname] = uiputfile({'*.jpg;*.png;*.bmp;*.tif'}, 'Save Image As'); if isequal(filename,0) || isequal(pathname,0) return; end % Save processed image imwrite(handles.processedImage, fullfile(pathname, filename)); % Update handles structure guidata(hObject, handles); ``` 这是一个简单的图像处理GUI,包含以下功能: - 加载图像 - 灰度化 - 阈值分割 - 裁剪 - 保存图像 可以根据需要进行添加或修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值