need some help with a function and calling it

조회 수: 1 (최근 30일)
slowlearner
slowlearner 2020년 5월 8일
답변: slowlearner 2020년 5월 12일
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
  댓글 수: 1
slowlearner
slowlearner 2020년 5월 8일
Yea sorry about that i didnt update the lower one before post

댓글을 달려면 로그인하십시오.

채택된 답변

slowlearner
slowlearner 2020년 5월 12일
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

추가 답변 (2개)

Ameer Hamza
Ameer Hamza 2020년 5월 8일
편집: Ameer Hamza 2020년 5월 8일
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 2020년 5월 8일
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
slowlearner
slowlearner 2020년 5월 8일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by