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

4 Ansichten (letzte 30 Tage)
Gabriel Maupin
Gabriel Maupin am 31 Jul. 2017
Kommentiert: Chad Greene am 1 Aug. 2017
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'.
>>

Antworten (3)

Chad Greene
Chad Greene am 31 Jul. 2017
Bearbeitet: Stephen23 am 1 Aug. 2017
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
  2 Kommentare
Stephen23
Stephen23 am 1 Aug. 2017
@Chad Greene: I added the output argument to the example showing the function being called.
Chad Greene
Chad Greene am 1 Aug. 2017
Yes, perhaps that's more clear. Thanks Stephen.

Melden Sie sich an, um zu kommentieren.


Jan
Jan am 31 Jul. 2017
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 Kommentar
Guillaume
Guillaume am 1 Aug. 2017
Bearbeitet: Guillaume am 1 Aug. 2017
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

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 31 Jul. 2017
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.

Kategorien

Mehr zu Desktop finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by