need some help with a function and calling it

1 visualizzazione (ultimi 30 giorni)
I've come quite far on my script but function and calling them is still hard i was thinking of calling it for an equation, and now I'm tying it out in an if statement before putting it in the loop. The function looks like:
function z = SEKDrive(x,y) %cost for the driving distance
z = 0.17 * x*(y/1000);%nissan leaf e+ 17kwh/100km
end
and the if statment like this:
x = input('How many km is the driving distance? ')
if x > 0 % need to make an && for SOC%
o = SEKDrive(x,SEK_in);
sekleft = SEK_ut - o
else isnan(t2)
disp('number please')
end
However matlab's been telling me its to many input values, and on both the books I have it's kind similar to what i did but it works and mine doesn't.
edited abit late tho

Risposta accettata

slowlearner
slowlearner il 12 Mag 2020
solved with SEK_in was a [10x1] array and not the correct input the script was supposed to be:
q = input('How many km is the driving distance? '); %need to include this in the loops somehow
if q > 0 %need to make an && for SOC%
o = SEKDrive(q,SEK_bat); %call the function SEKDrive
sek_left = SEK_in - o
and the function as corrected above

Più risposte (2)

Ameer Hamza
Ameer Hamza il 8 Mag 2020
Modificato: Ameer Hamza il 8 Mag 2020
SEKDrive have two input arguments. You should pass both.
o = SEKDrive(x, 5); % y=5 for example. You should change it according to your own requirements.
See here for basics of MATLAB functions: https://www.mathworks.com/help/matlab/ref/function.html

Steven Lord
Steven Lord il 8 Mag 2020
Your function is defined to accept and use two input arguments.
function SEK_ut = SEKDrive(x,y) %cost for the driving distance
You are only specifying one of those input arguments when you call it. [I'm assuming either that your SEKDrive function is defined in a function SEKdrive.m or you're actually calling it in your code as SEKDrive. The case of function and function file names matters in MATLAB.]
o = SEKdrive(x);
What value or values should MATLAB use when the commands inside SEKDrive try to operate on y?
It's as though you asked me "I want you to add two numbers. The first is 3. What's the total?" I don't have enough information to answer the question, and when MATLAB encounters that situation often its response is an error like:
>> plus(3)
Error using +
Not enough input arguments.
To correct the problem, specify a value or a variable for you as the second input when you call SEKDrive or modify the SEKDrive function so it only accepts and uses one input argument.
  1 Commento
slowlearner
slowlearner il 8 Mag 2020
Didnt rereview the post before posting it but there are 2 values in the function SEK_in is 146,42 it's a sum of two arrays devided by eachother

Accedi per commentare.

Categorie

Scopri di più su Programming in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by