当前位置:网站首页>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
边栏推荐
- SQLite add index
- SQLite queries the maximum value and returns the whole row of data
- Unity Vector3. Use and calculation principle of reflect
- 04. Project blog log
- [leetcode] 18. Sum of four numbers
- EditorUtility. The role and application of setdirty in untiy
- Three methods of Oracle two table Association update
- Codeless June event 2022 codeless Explorer conference will be held soon; AI enhanced codeless tool launched
- Ad20 is set with through-hole direct connection copper sheet, and the bonding pad is cross connected
- 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
猜你喜欢

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

idea一键导包

Cuda11.1 online installation

【云原生】3.1 Kubernetes平台安装KubeSpher

Rce code and Command Execution Vulnerability

Zoom and pan image in Photoshop 2022

初识CDN

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

GAMES202-WebGL中shader的编译和连接(了解向)
随机推荐
SQLite add index
Figure database ongdb release v-1.0.3
Promise summary
[QNX hypervisor 2.2 user manual]6.3.3 using shared memory (shmem) virtual devices
Tetris
04. 项目博客之日志
First acquaintance with CDN
01. 开发博客项目之项目介绍
初识CDN
【华为机试真题详解】统计射击比赛成绩
Deep learning -yolov5 introduction to actual combat click data set training
改善Jpopup以实现动态控制disable
Improve jpopup to realize dynamic control disable
自建DNS服务器,客户端打开网页慢,解决办法
[QNX Hypervisor 2.2用户手册]6.3.3 使用共享内存(shmem)虚拟设备
Pix2pix: image to image conversion using conditional countermeasure networks
In 2022, we must enter the big factory as soon as possible
Jvxetable用slot植入j-popup
Promotion hung up! The leader said it wasn't my poor skills
February 12 relativelayout