当前位置:网站首页>Selective parameters in MATLAB functions
Selective parameters in MATLAB functions
2022-07-06 05:27:00 【subtitle_】
1. Write it at the front
A lot of MATLAB Functions support optional input parameters and output parameters . for example , We call plot function , Input parameters can be as few as 2 individual , You can have as many as 7 Parameters . On the other hand , function max It supports one output parameter , Two output parameters are also supported . If there is only one output parameter ,max The maximum value of the function will be returned . If there are two output parameters, it will return the maximum value of the array and the location of the maximum value . How to know a MATLAB Function has several input and output parameters , And the corresponding functions of the function ?
stay MATLAB There are eight special functions in to get information about selective parameters and to report errors in these parameters . Here is an introduction .
2. Meaning of optional parameters
Parameters | meaning |
---|---|
nargin | Returns the number of actual input parameters required to call this function |
nargout | Returns the number of actual output parameters required to call this function |
nargchk | If you want a function call to be called with too many or too few parameters , that nargchk Function will return a standard error message |
error | Display error message , And stop the function to avoid this error . If the parameter error is fatal , This function will be called . |
warning | Display a warning message and continue to execute the function , If the parameter error is not fatal , Execution can continue , Then this will be called . |
inputname | Returns the actual variable name for a specific number of parameters . |
3. Examples of functions written with optional parameters
function [mag, angle] = polar_value(x, y)
% POLAR_VALUE Converts(x, y) to (r, theta)
% Punction POLAR_VALUE converts an input(x,y)
% va1ue into (r, theta), with theta in degrees.
% It illustrates the use of optional arguments.
% Define variables:
% angle --Angle in degrees
% msg --Error message
% mag --Magnitude
% x --Input x value
% y --Input y value(optional)
% Record Of revisions:
% Date Programmer Description of change
% ======== ============== ========================
% 12/16/98 S.J.Chapman Original code
% Check for a legal number of input arquments
% among min_args It refers to the minimum number of parameters ,max_args Is the maximum number of indices ,num_args It refers to the actual number of parameters . If the number of parameters is not within the allowable range , A standard error message will be generated . If the number of parameters is within the allowable range , Then this function will return a null character .
msg = nargchk(1,2,nargin);
% function error It is a standard way to display standard error messages and to abort custom functions that cause error messages .
error(msg);
% If the y argument is missing, set it to 0.
% If there is only one parameter , Then the function assumes y The value is 0
if nargin < 2
y = 0;
end
% Check for (0,0) input argument, and print out
% a warning message.
% x and y All are 0 The situation of , Output warning messages
if x == 0 & y == 0
msg = 'Both x and y are zero: angle is meaningless!';
warning(msg);
end
% Now calculate the magnitude
mag = sqrt(x .^2 + y .^2);
% If the second output argument is present,calculate
% angle in degrees
if nargout == 2
angle = atan2(y,x) * 180/pi;
end
test :
1. No parameters entered
>> [mag angle]=polar_value
??? Error using ==> polar_value
Not enough input arguments.
2. Too many parameters entered
>> [mag angle]=polar_value(1,-1,1)
??? Error using ==> polar_value
Too many input arguments.
3. Enter a parameter
>> [mag angle]=polar_value(1)
mag =
1
angle =
0
4. Enter two parameters
>> [mag angle]=polar_value(1,-1)
mag =
1.4142
angle =
-45
5. Output a parameter
>> mag = polar_value(1,-1)
mag =
1.4142
6. Input x=0,y=0 Parameters
>> [mag angle] = polar_value(0,0)
Warning: Both x and y are zero: angle is meaningless!
> In polar_value at 27
mag =
0
angle =
0
Deepen the understanding of function writing through the above examples , Understand the meaning of each parameter , Notes extracted from :
[1] S.J.Chapman《MATLAB Programming 》 Chinese version
边栏推荐
- 【torch】|torch. nn. utils. clip_ grad_ norm_
- Zoom and pan image in Photoshop 2022
- GAMES202-WebGL中shader的编译和连接(了解向)
- Compilation et connexion de shader dans games202 - webgl (comprendre la direction)
- [leetcode daily question] number of enclaves
- Vulhub vulnerability recurrence 68_ ThinkPHP
- 【OSPF 和 ISIS 在多路访问网络中对掩码的要求】
- 备忘一下jvxetable的各种数据集获取方法
- 自建DNS服务器,客户端打开网页慢,解决办法
- [QNX Hypervisor 2.2用户手册]6.3.3 使用共享内存(shmem)虚拟设备
猜你喜欢
Vulhub vulnerability recurrence 73_ Webmin
初识CDN
Using stopwatch to count code time
无代码六月大事件|2022无代码探索者大会即将召开;AI增强型无代码工具推出...
[mask requirements of OSPF and Isis in multi access network]
Golang -- TCP implements concurrency (server and client)
04. Project blog log
指针经典笔试题
04. 项目博客之日志
图数据库ONgDB Release v-1.0.3
随机推荐
Compilation and connection of shader in games202 webgl (learn from)
[leetcode daily question] number of enclaves
Realize a binary read-write address book
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
UCF (summer team competition II)
MySQL if and ifnull use
pix2pix:使用条件对抗网络的图像到图像转换
EditorUtility. The role and application of setdirty in untiy
Vulhub vulnerability recurrence 71_ Unomi
02. 开发博客项目之数据存储
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
【华为机试真题详解】统计射击比赛成绩
备忘一下jvxetable的各种数据集获取方法
Leetcode dynamic planning day 16
Rce code and Command Execution Vulnerability
Pickle and savez_ Compressed compressed volume comparison
【LeetCode】18、四数之和
Hyperledger Fabric2. Some basic concepts of X (1)
In 2022, we must enter the big factory as soon as possible
Cve-2019-11043 (PHP Remote Code Execution Vulnerability)