How to create a function that do not require all the input paramenters

Dear all,
I'm quite new using matlab and I'm trying to create a function with +- 30 input parameters. Then, when I call the function, in some cases, I don't need to use all input parameters.
According to this I would like to know if its possible to create a function with 30 input parameters but when using it, just use a few according to the need of the user.
I don't want to put [] in the parameters that I'm not using. I was thinking of somethins like : Test = funct('a', a, 'c', c); However, I have one of the input parameter requires a matrix and when I'm introducing the matrix I get an error.
Thank you in advance,
Ricardo

回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 11 月 1 日

1 投票

The correct way to do this is to use an input parser object and define some inputs as optional: https://www.mathworks.com/help/matlab/ref/inputparser.html. The documentation example shows how it can be done.

3 件のコメント

Ricardo Duarte
Ricardo Duarte 2020 年 11 月 1 日
I'm doing this to create the function:
function writeh5filetotal(varargin)
T=0;
%[H5file]=file_new;
file_new='teste.h5';
format_version=[];
author=[];
institution=[];
country_code=[];
contact=[];
start_date=[];
end_date=[];
date_of_creation=[];
purpose=[];
data_uuid=[];
data_type=[];
comments=[];
expsetup=[];
recorder_manufacturer=[];
recorder_serial_number=[];
recorder_model=[];
builtin_hydrophone=[];
hydrophone_manufacturer=[];
hydrophone_sensitivity=[];
hydrophone_serial_number=[];
hydrophone_model=[];
calibration_frequency_count=[];
calibration_datetime=[];
calibration_factor=[];
calibration_procedure=[];
reference_frequencies=[];
ais_database=[];
source_levels=[];
bathymetry_database=[];
temperature_database=[];
salinity_database=[];
sound_speed_profile_model=[];
propagation_model=[];
numeric_data_calibration=[];
hydrophone_count=[];
total_number_of_grid_points=[];
longitude=[];
latitude=[];
depth=[];
frequency_count=[];
frequency_band_definition=[];
frequency=[];
time_duty_on=[];
time_duty_off=[];
time=[0];
averaging_time=[];
spl_values=[];
percentile_count=[];
percentile_list=[];
percentile=[];
for opt=1:nargin
switch varargin{opt}
case 'file_new'
file_new=varargin{opt+1};
%file_new=file_new;
case 'format_version'
format_version=varargin{opt+1};
%format_version=format_version;
case 'author'
author=varargin{opt+1};
case 'institution'
institution=varargin{opt+1};
case 'country_code'
country_code=varargin{opt+1};
...
end
end
....
However if one of those cases is a matrix I will obtain an error:
"SWITCH expression must be a scalar or a character vector"
Ameer Hamza
Ameer Hamza 2020 年 11 月 1 日
This will be a complicated approach. I suggest you follow input parser approach as given in the link in my answer. It may be difficult to understand initially, but once it is working, it will be much more robust.
John D'Errico
John D'Errico 2020 年 11 月 1 日
Ugh. I STRONGLY recommend you follow the advice given by Ameer.

この質問は閉じられています。

製品

リリース

R2017a

タグ

質問済み:

2020 年 11 月 1 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by