Matlab Too Many Input Arguments and How to set default values on that









up vote
1
down vote

favorite
1












There are 8 input arguments in my function unstable_L(T1,T2,z1,z2,z3,z0h,z0m,U).



I would like to create this function with 5 required inouts and 3 optional inputs in which the optional value will be my default values that I have set if the user do not fill in. How can I make it? Is there any mistake in my coding?



nargin = 8;
if nargin > 8
disp (" the function unstable_L_new has only maximum of 8 input paramaters")

else
% Fill in unset optional values.
switch nargin
case 5
if isempty(z0h)
z0h = 0.005;

elseif isempty(z0m)
z0m = 0.005;

elseif isempty(U)
U = 2.0;

end

case 6
if isempty(z0m)
z0m = 0.005;

elseif isempty(U)
U = 2.0;

end

case 7
if isempty(U)
U = 2.0;
end
end
end









share|improve this question























  • It depends on which arguments are optional, are they always the same? Or does the function just require 5/8 to work?
    – rinkert
    Nov 11 at 9:41










  • the last three arguments are optional, but the function must have all 8 inputs to function. I would like to write the function in which in case the user does not have enough data for the last three arguments, then matlab will recognise the last three arguments and fill in the default values that I set. is it posiible to do that?
    – Yeoh
    Nov 11 at 10:04










  • See my answer below, that should do exactly that.
    – rinkert
    Nov 11 at 10:14














up vote
1
down vote

favorite
1












There are 8 input arguments in my function unstable_L(T1,T2,z1,z2,z3,z0h,z0m,U).



I would like to create this function with 5 required inouts and 3 optional inputs in which the optional value will be my default values that I have set if the user do not fill in. How can I make it? Is there any mistake in my coding?



nargin = 8;
if nargin > 8
disp (" the function unstable_L_new has only maximum of 8 input paramaters")

else
% Fill in unset optional values.
switch nargin
case 5
if isempty(z0h)
z0h = 0.005;

elseif isempty(z0m)
z0m = 0.005;

elseif isempty(U)
U = 2.0;

end

case 6
if isempty(z0m)
z0m = 0.005;

elseif isempty(U)
U = 2.0;

end

case 7
if isempty(U)
U = 2.0;
end
end
end









share|improve this question























  • It depends on which arguments are optional, are they always the same? Or does the function just require 5/8 to work?
    – rinkert
    Nov 11 at 9:41










  • the last three arguments are optional, but the function must have all 8 inputs to function. I would like to write the function in which in case the user does not have enough data for the last three arguments, then matlab will recognise the last three arguments and fill in the default values that I set. is it posiible to do that?
    – Yeoh
    Nov 11 at 10:04










  • See my answer below, that should do exactly that.
    – rinkert
    Nov 11 at 10:14












up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






1





There are 8 input arguments in my function unstable_L(T1,T2,z1,z2,z3,z0h,z0m,U).



I would like to create this function with 5 required inouts and 3 optional inputs in which the optional value will be my default values that I have set if the user do not fill in. How can I make it? Is there any mistake in my coding?



nargin = 8;
if nargin > 8
disp (" the function unstable_L_new has only maximum of 8 input paramaters")

else
% Fill in unset optional values.
switch nargin
case 5
if isempty(z0h)
z0h = 0.005;

elseif isempty(z0m)
z0m = 0.005;

elseif isempty(U)
U = 2.0;

end

case 6
if isempty(z0m)
z0m = 0.005;

elseif isempty(U)
U = 2.0;

end

case 7
if isempty(U)
U = 2.0;
end
end
end









share|improve this question















There are 8 input arguments in my function unstable_L(T1,T2,z1,z2,z3,z0h,z0m,U).



I would like to create this function with 5 required inouts and 3 optional inputs in which the optional value will be my default values that I have set if the user do not fill in. How can I make it? Is there any mistake in my coding?



nargin = 8;
if nargin > 8
disp (" the function unstable_L_new has only maximum of 8 input paramaters")

else
% Fill in unset optional values.
switch nargin
case 5
if isempty(z0h)
z0h = 0.005;

elseif isempty(z0m)
z0m = 0.005;

elseif isempty(U)
U = 2.0;

end

case 6
if isempty(z0m)
z0m = 0.005;

elseif isempty(U)
U = 2.0;

end

case 7
if isempty(U)
U = 2.0;
end
end
end






matlab






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 13:04









Foo

1




1










asked Nov 11 at 9:32









Yeoh

62




62











  • It depends on which arguments are optional, are they always the same? Or does the function just require 5/8 to work?
    – rinkert
    Nov 11 at 9:41










  • the last three arguments are optional, but the function must have all 8 inputs to function. I would like to write the function in which in case the user does not have enough data for the last three arguments, then matlab will recognise the last three arguments and fill in the default values that I set. is it posiible to do that?
    – Yeoh
    Nov 11 at 10:04










  • See my answer below, that should do exactly that.
    – rinkert
    Nov 11 at 10:14
















  • It depends on which arguments are optional, are they always the same? Or does the function just require 5/8 to work?
    – rinkert
    Nov 11 at 9:41










  • the last three arguments are optional, but the function must have all 8 inputs to function. I would like to write the function in which in case the user does not have enough data for the last three arguments, then matlab will recognise the last three arguments and fill in the default values that I set. is it posiible to do that?
    – Yeoh
    Nov 11 at 10:04










  • See my answer below, that should do exactly that.
    – rinkert
    Nov 11 at 10:14















It depends on which arguments are optional, are they always the same? Or does the function just require 5/8 to work?
– rinkert
Nov 11 at 9:41




It depends on which arguments are optional, are they always the same? Or does the function just require 5/8 to work?
– rinkert
Nov 11 at 9:41












the last three arguments are optional, but the function must have all 8 inputs to function. I would like to write the function in which in case the user does not have enough data for the last three arguments, then matlab will recognise the last three arguments and fill in the default values that I set. is it posiible to do that?
– Yeoh
Nov 11 at 10:04




the last three arguments are optional, but the function must have all 8 inputs to function. I would like to write the function in which in case the user does not have enough data for the last three arguments, then matlab will recognise the last three arguments and fill in the default values that I set. is it posiible to do that?
– Yeoh
Nov 11 at 10:04












See my answer below, that should do exactly that.
– rinkert
Nov 11 at 10:14




See my answer below, that should do exactly that.
– rinkert
Nov 11 at 10:14












1 Answer
1






active

oldest

votes

















up vote
0
down vote













The way I understand your problem is that your last three arguments z0h, z0m and U are optional.



The way you can make this work is by checking if they exist



function unstable_L(T1,T2,z1,z2,z3,z0h,z0m,U)

if (~exist('z0h', 'var'))
z0h = 1;
end

if (~exist('z0m', 'var'))
z0m = 0.005;
end

if (~exist('U', 'var'))
U = 2.0;
end

% rest of function





share|improve this answer




















  • it works~! woa! Thanks a lot for helping! I really appreciate it
    – Yeoh
    Nov 11 at 10:36










  • Glad I could help, however, for future use of SO take a brief look here: stackoverflow.com/help/someone-answers
    – rinkert
    Nov 11 at 10:55











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247425%2fmatlab-too-many-input-arguments-and-how-to-set-default-values-on-that%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













The way I understand your problem is that your last three arguments z0h, z0m and U are optional.



The way you can make this work is by checking if they exist



function unstable_L(T1,T2,z1,z2,z3,z0h,z0m,U)

if (~exist('z0h', 'var'))
z0h = 1;
end

if (~exist('z0m', 'var'))
z0m = 0.005;
end

if (~exist('U', 'var'))
U = 2.0;
end

% rest of function





share|improve this answer




















  • it works~! woa! Thanks a lot for helping! I really appreciate it
    – Yeoh
    Nov 11 at 10:36










  • Glad I could help, however, for future use of SO take a brief look here: stackoverflow.com/help/someone-answers
    – rinkert
    Nov 11 at 10:55















up vote
0
down vote













The way I understand your problem is that your last three arguments z0h, z0m and U are optional.



The way you can make this work is by checking if they exist



function unstable_L(T1,T2,z1,z2,z3,z0h,z0m,U)

if (~exist('z0h', 'var'))
z0h = 1;
end

if (~exist('z0m', 'var'))
z0m = 0.005;
end

if (~exist('U', 'var'))
U = 2.0;
end

% rest of function





share|improve this answer




















  • it works~! woa! Thanks a lot for helping! I really appreciate it
    – Yeoh
    Nov 11 at 10:36










  • Glad I could help, however, for future use of SO take a brief look here: stackoverflow.com/help/someone-answers
    – rinkert
    Nov 11 at 10:55













up vote
0
down vote










up vote
0
down vote









The way I understand your problem is that your last three arguments z0h, z0m and U are optional.



The way you can make this work is by checking if they exist



function unstable_L(T1,T2,z1,z2,z3,z0h,z0m,U)

if (~exist('z0h', 'var'))
z0h = 1;
end

if (~exist('z0m', 'var'))
z0m = 0.005;
end

if (~exist('U', 'var'))
U = 2.0;
end

% rest of function





share|improve this answer












The way I understand your problem is that your last three arguments z0h, z0m and U are optional.



The way you can make this work is by checking if they exist



function unstable_L(T1,T2,z1,z2,z3,z0h,z0m,U)

if (~exist('z0h', 'var'))
z0h = 1;
end

if (~exist('z0m', 'var'))
z0m = 0.005;
end

if (~exist('U', 'var'))
U = 2.0;
end

% rest of function






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 9:46









rinkert

1,227317




1,227317











  • it works~! woa! Thanks a lot for helping! I really appreciate it
    – Yeoh
    Nov 11 at 10:36










  • Glad I could help, however, for future use of SO take a brief look here: stackoverflow.com/help/someone-answers
    – rinkert
    Nov 11 at 10:55

















  • it works~! woa! Thanks a lot for helping! I really appreciate it
    – Yeoh
    Nov 11 at 10:36










  • Glad I could help, however, for future use of SO take a brief look here: stackoverflow.com/help/someone-answers
    – rinkert
    Nov 11 at 10:55
















it works~! woa! Thanks a lot for helping! I really appreciate it
– Yeoh
Nov 11 at 10:36




it works~! woa! Thanks a lot for helping! I really appreciate it
– Yeoh
Nov 11 at 10:36












Glad I could help, however, for future use of SO take a brief look here: stackoverflow.com/help/someone-answers
– rinkert
Nov 11 at 10:55





Glad I could help, however, for future use of SO take a brief look here: stackoverflow.com/help/someone-answers
– rinkert
Nov 11 at 10:55


















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247425%2fmatlab-too-many-input-arguments-and-how-to-set-default-values-on-that%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto