当前位置:网站首页>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
边栏推荐
- Cve-2019-11043 (PHP Remote Code Execution Vulnerability)
- Compilation and connection of shader in games202 webgl (learn from)
- Easy to understand I2C protocol
- UCF (summer team competition II)
- [effective Objective-C] - memory management
- Tetris
- flutter 实现一个有加载动画的按钮(loadingButton)
- Golang -- TCP implements concurrency (server and client)
- Implementing fuzzy query with dataframe
- ARTS Week 25
猜你喜欢

02. 开发博客项目之数据存储

C Advanced - data storage (Part 1)

pix2pix:使用条件对抗网络的图像到图像转换

指针经典笔试题

GAMES202-WebGL中shader的編譯和連接(了解向)

JS array list actual use summary

Unity Vector3. Use and calculation principle of reflect

Configuration file converted from Excel to Lua
![[cloud native] 3.1 kubernetes platform installation kubespher](/img/86/137a65a5b58bc32e596d2a330ca9fc.png)
[cloud native] 3.1 kubernetes platform installation kubespher

【torch】|torch.nn.utils.clip_grad_norm_
随机推荐
Driver development - hellowdm driver
UCF(2022暑期团队赛一)
Three. JS learning - light and shadow (understanding)
Microblogging hot search stock selection strategy
注释、接续、转义等符号
Modbus protocol communication exception
HAC集群修改管理员用户密码
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
Huawei od computer test question 2
Self built DNS server, the client opens the web page slowly, the solution
Leetcode dynamic planning day 16
【华为机试真题详解】检查是否存在满足条件的数字组合
趋势前沿 | 达摩院语音 AI 最新技术大全
jdbc使用call调用存储过程报错
毕业设计游戏商城
2022 half year summary
05. Security of blog project
Nacos TC setup of highly available Seata (02)
Tetris
Cve-2019-11043 (PHP Remote Code Execution Vulnerability)