Why can't my user defined function use a variable it calculates?

3 次查看(过去 30 天)
function[Impulse_Over_Burn_Time] = Impulse_Over_Burn_Time(I,T)
%UNTITLED Summary of this function goes here
% The t= I/BT we use it to find q, which is used to find max velosity
I = input('What is the engines impulse?');
T = input('What is your engines burn time?');
t = I / T
end
Command Window
>> Impulse_Over_Burn_Time
What is the engines impulse?4
What is your engines burn time?8
t =
0.5000
>> t+1
Undefined function or variable 't'.
>>

回答(3 个)

Chad Greene
Chad Greene 2017-7-31
编辑:Stephen23 2017-8-1
The problem is your t=I/T line is printing the value of t to the command window, but it's not saving it to a variable. So change the top line to
function t = Impulse_Over_Burn_Time(I,T)
and suppress the printed version of t=I/T by placing a semicolon at the end of the line. Here's a rewrite of your function:
function t = Impulse_Over_Burn_Time(I,T)
% Impulse_Over_Burn_Time
% The t= I/BT we use it to find q, which is used to find max velosity
if nargin==0
I = input('What is the engines impulse?');
T = input('What is your engines burn time?');
end
t = I ./ T; % <-ADD A SEMICOLON TO SUPPRESS OUTPUT
end
Note the other changes I made. Now you can use the function much as you were doing before but with an output argument:
>> t = Impulse_Over_Burn_Time
What is the engines impulse?4
What is your engines burn time?8
t =
0.50
>> t+1
ans =
1.50
Or you can simply enter your desired values into the function directly like this:
>> t = Impulse_Over_Burn_Time(4,8)
t =
0.50
>> t+1
ans =
1.50
Or do it all in one line:
>> t = Impulse_Over_Burn_Time(4,8) + 1
t =
1.50

Jan
Jan 2017-7-31
Perhaps you want:
function t = Impulse_Over_Burn_Time
I = input('What is the engines impulse?');
T = input('What is your engines burn time?');
t = I / T
end
which replies "t", not "Impulse_Over_Burn_Time". In addition it does not use the inputs "I" and "T", because they are overwritten by the input() commands. Then call this like:
t = Impulse_Over_Burn_Time
It seems like the meaning of inputs and outputs of function is not clear yet. Read this: https://www.mathworks.com/help/matlab/ref/function.html .
  1 个评论
Guillaume
Guillaume 2017-8-1
编辑:Guillaume 2017-8-1
It seems like the meaning of inputs and outputs of function is not clear yet
I think you hit the nail on the head. Gabriel needs to learn how to write functions and how to use functions.
Functions are not scripts, whichever variables are created by the functions are only available within the function, unless they're defined as output variables.
When you invoke a function, if you don't assign its output to something it is immediately lost, the same as if you did
sin(3.14)
even if sin creates a t variable internally, you don't have access to t after calling sin. However, if you do:
t = sin(3.14)
then you can do
t+1

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2017-7-31
This is correct. You create t as a local variable inside of your function, and it will be destroyed as soon as your function returns.

类别

Help CenterFile Exchange 中查找有关 Desktop 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by