当前位置:网站首页>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
边栏推荐
- UCF (summer team competition II)
- EditorUtility. The role and application of setdirty in untiy
- [untitled]
- SQLite queries the maximum value and returns the whole row of data
- Force buckle 1189 Maximum number of "balloons"
- Note the various data set acquisition methods of jvxetable
- 04. Project blog log
- Pickle and savez_ Compressed compressed volume comparison
- Codeforces Round #804 (Div. 2) Editorial(A-B)
- 无代码六月大事件|2022无代码探索者大会即将召开;AI增强型无代码工具推出...
猜你喜欢

01. Project introduction of blog development project

Three methods of Oracle two table Association update

注释、接续、转义等符号

Codeforces Round #804 (Div. 2) Editorial(A-B)

Can the feelings of Xi'an version of "Coca Cola" and Bingfeng beverage rush for IPO continue?

Vulhub vulnerability recurrence 69_ Tiki Wiki

Notes, continuation, escape and other symbols
![[effective Objective-C] - memory management](/img/1e/611aa998486bbac76ac103c3091794.jpg)
[effective Objective-C] - memory management

In 2022, we must enter the big factory as soon as possible

Summary of deep learning tuning tricks
随机推荐
關於Unity Inspector上的一些常用技巧,一般用於編輯器擴展或者其他
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
Check the useful photo lossless magnification software on Apple computer
[mask requirements of OSPF and Isis in multi access network]
Nacos TC setup of highly available Seata (02)
Imperial cms7.5 imitation "D9 download station" software application download website source code
Three. JS learning - light and shadow (understanding)
[leetcode16] the sum of the nearest three numbers (double pointer)
浅谈镜头滤镜的类型及作用
Vulhub vulnerability recurrence 71_ Unomi
[detailed explanation of Huawei machine test] check whether there is a digital combination that meets the conditions
February 12 relativelayout
改善Jpopup以实现动态控制disable
Summary of deep learning tuning tricks
Excel转换为Lua的配置文件
Class inheritance in yyds dry inventory C
idea一键导包
Hyperledger Fabric2. Some basic concepts of X (1)
图数据库ONgDB Release v-1.0.3
C Advanced - data storage (Part 1)