Validating Function Inputs
Use function argument validation in MATLAB® to declare specific restrictions on function input arguments. You can constrain the class, size, and other aspects of function input values without writing code in the body of the function to perform these tests.
Published: 5 Sep 2019
Creating robust functions that check input arguments for errors can result in code that looks complicated and is difficult to understand.
Note that most of the code for this simple function is error checking code.
Using function input argument validation, you can constrain the class, size, and other aspects of function inputs without writing a lot of code in the body of the function to perform these tests.
And it provides users with clear error messages.
With function input argument validation, you can include a default value for optional input arguments—in this case, (0,0)—and MATLAB will override it if the user provides a value.
Now let's look at an example function that plots a quadrilateral.
The algorithm requires row vectors containing four elements for the operations to work.
But what happens when we pass in column vectors instead? I’ll do this by transposing one of our inputs.
In the case of 1D arrays, function input argument validation automatically transposes the inputs to the expected orientation for you.
This function also accepts two custom name-value pairs—LineType and LineThickness—which are defined in the code as a struct.
Using this syntax enables auto-complete suggestions for your custom name-value pairs.
With function input argument validation, you can eliminate cumbersome argument-checking code and improve the readability, robustness, and maintainability of your code.
Read the documentation to learn more.
Featured Product
MATLAB
Up Next:
Related Videos:
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)