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
3
  • It depends on which arguments are optional, are they always the same? Or does the function just require 5/8 to work? Commented Nov 11, 2018 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? Commented Nov 11, 2018 at 10:04
  • See my answer below, that should do exactly that. Commented Nov 11, 2018 at 10:14

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

2 Comments

it works~! woa! Thanks a lot for helping! I really appreciate it
Glad I could help, however, for future use of SO take a brief look here: stackoverflow.com/help/someone-answers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.