当前位置:网站首页>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
边栏推荐
- Vulhub vulnerability recurrence 67_ Supervisor
- Leetcode dynamic planning day 16
- Some common skills on unity inspector are generally used for editor extension or others
- 用StopWatch 统计代码耗时
- Algorithm -- climbing stairs (kotlin)
- 04. 项目博客之日志
- 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
- Drive development - the first helloddk
- Simple understanding of interpreters and compilers
- [QNX Hypervisor 2.2用户手册]6.3.3 使用共享内存(shmem)虚拟设备
猜你喜欢
Review of double pointer problems
趋势前沿 | 达摩院语音 AI 最新技术大全
Three methods of Oracle two table Association update
02. 开发博客项目之数据存储
[leetcode16] the sum of the nearest three numbers (double pointer)
Simple understanding of interpreters and compilers
JS quick start (II)
Self built DNS server, the client opens the web page slowly, the solution
Check the useful photo lossless magnification software on Apple computer
Ora-01779: the column corresponding to the non key value saving table cannot be modified
随机推荐
Vulhub vulnerability recurrence 72_ uWSGI
[leetcode] 18. Sum of four numbers
Vulhub vulnerability recurrence 69_ Tiki Wiki
C# AES对字符串进行加密
Summary of redis basic knowledge points
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
MySQL time processing
Zoom and pan image in Photoshop 2022
[QNX hypervisor 2.2 user manual]6.3.3 using shared memory (shmem) virtual devices
Improve jpopup to realize dynamic control disable
Detailed summary of SQL injection
[leetcode16] the sum of the nearest three numbers (double pointer)
Qt TCP 分包粘包的解决方法
2022 half year summary
[mask requirements of OSPF and Isis in multi access network]
算法-- 爬楼梯(Kotlin)
JS quick start (II)
Self built DNS server, the client opens the web page slowly, the solution
Leetcode dynamic planning day 16
C AES encrypts strings