Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
MATLAB function returning only 'ans'

Subject: MATLAB function returning only 'ans'

From: Adam

Date: 2 May, 2013 16:44:07

Message: 1 of 9

I've been trying to get a function to calculate two outputs for me, mtow and finalWeight. The script I'm using is shown below:

function [mtow,finalWeight] = WeightModule(fuelWeight)

fuelWeight=30000;
payload = 18500;
owenowingsnoengine = 36300;

mtow = owenowingsnoengine+payload+fuelWeight;

finalWeight = owenowingsnoengine+payload;


Does anybody have any ideas why the script stops after calculating mtow (and calls it 'ans') and does not calculate finalWeight as well as not putting any of the values into the workspace

Thanks for any help anyone can provide

Subject: MATLAB function returning only 'ans'

From: Yehonatan

Date: 2 May, 2013 16:58:09

Message: 2 of 9

Hi,
If you write this code:
x = 5*5;
x
Matlab will return this:
x =
       25

But if you write this code:
x = 5*5
Matlab will return you this:
ans =
          25
You see. When matlab has to caculate it will return ans and when it doesn't have to caculate it returns the variable name.

Subject: MATLAB function returning only 'ans'

From: Steven_Lord

Date: 2 May, 2013 17:53:59

Message: 3 of 9



"Adam " <akitchener88@gmail.com> wrote in message
news:klu54n$eqf$1@newscl01ah.mathworks.com...
> I've been trying to get a function to calculate two outputs for me, mtow
> and finalWeight. The script I'm using is shown below:
>
> function [mtow,finalWeight] = WeightModule(fuelWeight)
>
> fuelWeight=30000;

This line overrides the value that the user of your function passed in. If
you want to use a default of 30000 _if the user doesn't specify fuelWeight_
you can do that, but this isn't the way to accomplish that.

> payload = 18500;
> owenowingsnoengine = 36300;
>
> mtow = owenowingsnoengine+payload+fuelWeight;
>
> finalWeight = owenowingsnoengine+payload;
>
>
> Does anybody have any ideas why the script stops after calculating mtow
> (and calls it 'ans') and does not calculate finalWeight as well as not
> putting any of the values into the workspace

It does compute finalWeight. But you're not calling WeightModule with a
syntax that tells MATLAB what to call that variable in the workspace of the
function calling WeightModule.

http://www.mathworks.com/help/matlab/matlab_prog/base-and-function-workspaces.html

http://www.mathworks.com/help/matlab/learn_matlab/calling-functions.html?searchHighlight=multiple+output+arguments

In order for both the outputs of WeightModule to be accessible from the
function that called it, the caller function must call it with two output
arguments.

[firstOutput, secondOutput] = WeightModule(30000);

This will only return what you assigned to mtow inside WeightModule to the
"outer" calling function:

firstAndOnlyOutput = WeightModule(30000);

This will return what you assigned to mtow inside WeightModule to the
"outer" calling function and store it to a variable named ans.

WeightModule(30000)

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: MATLAB function returning only 'ans'

From: Adam

Date: 3 May, 2013 14:30:13

Message: 4 of 9

"Steven_Lord" <slord@mathworks.com> wrote in message <klu97o$sld$1@newscl01ah.mathworks.com>...
>
>
> "Adam " <akitchener88@gmail.com> wrote in message
> news:klu54n$eqf$1@newscl01ah.mathworks.com...
> > I've been trying to get a function to calculate two outputs for me, mtow
> > and finalWeight. The script I'm using is shown below:
> >
> > function [mtow,finalWeight] = WeightModule(fuelWeight)
> >
> > fuelWeight=30000;
>
> This line overrides the value that the user of your function passed in. If
> you want to use a default of 30000 _if the user doesn't specify fuelWeight_
> you can do that, but this isn't the way to accomplish that.
>
> > payload = 18500;
> > owenowingsnoengine = 36300;
> >
> > mtow = owenowingsnoengine+payload+fuelWeight;
> >
> > finalWeight = owenowingsnoengine+payload;
> >
> >
> > Does anybody have any ideas why the script stops after calculating mtow
> > (and calls it 'ans') and does not calculate finalWeight as well as not
> > putting any of the values into the workspace
>
> It does compute finalWeight. But you're not calling WeightModule with a
> syntax that tells MATLAB what to call that variable in the workspace of the
> function calling WeightModule.
>
> http://www.mathworks.com/help/matlab/matlab_prog/base-and-function-workspaces.html
>
> http://www.mathworks.com/help/matlab/learn_matlab/calling-functions.html?searchHighlight=multiple+output+arguments
>
> In order for both the outputs of WeightModule to be accessible from the
> function that called it, the caller function must call it with two output
> arguments.
>
> [firstOutput, secondOutput] = WeightModule(30000);
>
> This will only return what you assigned to mtow inside WeightModule to the
> "outer" calling function:
>
> firstAndOnlyOutput = WeightModule(30000);
>
> This will return what you assigned to mtow inside WeightModule to the
> "outer" calling function and store it to a variable named ans.
>
> WeightModule(30000)
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com



Thanks for your help Steve, although i'm a bit confused as I thought I had already written this in the form:

[firstOutput, secondOutput] = WeightModule(30000);



It may be of use to try and explain more about what I'm attempting to do:

I want to have three modules (one of which is WeightModule). I've currently only been testing this to work with one module and will write the others when I'm happy with the first.

These modules shall then be run by a Script and will provide values that are then combined in a calculation at the end of the Script. The main issue at the moment is when I run the script the only return is 'ans' and it is the value for mtow (1.5474). Nothing else exists in the workspace.

Below is the Script (MScript) I am using to call the WeightModule and the WeightModule itself (in case I have made any changes to it since posting on here yesterday)

Thanks for any help you can provide.


SCRIPT: (FILE NAME: MScript)
-------------------------------------------------------------------------------------------------
function weight = MScript(mtow,finalWeight)

fuelWeight = 30000;

    % Weight Module
    [mtow,finalWeight] = WeightModule(fuelWeight);
    
weight = (mtow/finalWeight)
-------------------------------------------------------------------------------------------------




WEIGHT MODULE: (file name: WeightModule)
-------------------------------------------------------------------------------------------------
function[output1,output2] = WeightModule(inputA)

%fuelWeight=30000;
payload = 18500;
owenowingsnoengine = 36300;

output1 = owenowingsnoengine+payload+inputA;

output2 = owenowingsnoengine+payload;
-------------------------------------------------------------------------------------------------

Subject: MATLAB function returning only 'ans'

From: Steven_Lord

Date: 3 May, 2013 15:45:25

Message: 5 of 9



"Adam " <akitchener88@gmail.com> wrote in message
news:km0hll$kf9$1@newscl01ah.mathworks.com...
> "Steven_Lord" <slord@mathworks.com> wrote in message
> <klu97o$sld$1@newscl01ah.mathworks.com>...

*snip*

> Thanks for your help Steve, although i'm a bit confused as I thought I had
> already written this in the form:
>
> [firstOutput, secondOutput] = WeightModule(30000);
>
> It may be of use to try and explain more about what I'm attempting to do:
>
> I want to have three modules (one of which is WeightModule). I've
> currently only been testing this to work with one module and will write
> the others when I'm happy with the first.
>
> These modules shall then be run by a Script and will provide values that
> are then combined in a calculation at the end of the Script. The main
> issue at the moment is when I run the script the only return is 'ans' and
> it is the value for mtow (1.5474). Nothing else exists in the workspace.
>
> Below is the Script (MScript)

I'm going to be a bit pedantic here. What you have is not a MATLAB script.
You have a MATLAB function. The rules for MATLAB functions and MATLAB
scripts are different, particularly with respect to how you get information
into and out of them.

> I am using to call the WeightModule and the WeightModule itself (in case
> I have made any changes to it since posting on here yesterday)
>
> Thanks for any help you can provide.
>
>
> SCRIPT: (FILE NAME: MScript)
> -------------------------------------------------------------------------------------------------
> function weight = MScript(mtow,finalWeight)
>
> fuelWeight = 30000;
>
> % Weight Module
> [mtow,finalWeight] = WeightModule(fuelWeight);

So this will return both the contents of output1 and output2 from
WeightModule's workspace to the workspace of MScript. The contents of
output1 will be called mtow in MScript's workspace and the contents of
output2 will be called finalWeight.

And this line also overrides the mtow and finalWeight input arguments you
passed into MScript.

> weight = (mtow/finalWeight)

So this function will NOT return the contents of the variables mtow or
finalWeight to the workspace of the function or script in which MScript was
called. It will ONLY return the contents of the variable weight to that
workspace. What the contents of that variable from MScript's workspace will
be called in the caller's workspace depends on how you called MScript. If
you called it:

thisIsTheWeight = MScript

then thisIsTheWeight in that function's workspace will contain the contents
of weight in MScript's workspace. If you called it:

MScript

then MATLAB will assign the contents of weight in MScript's workspace to the
variable ans in the caller's workspace.

> -------------------------------------------------------------------------------------------------
>
> WEIGHT MODULE: (file name: WeightModule)
> -------------------------------------------------------------------------------------------------
> function[output1,output2] = WeightModule(inputA)
>
> %fuelWeight=30000;
> payload = 18500;
> owenowingsnoengine = 36300;
>
> output1 = owenowingsnoengine+payload+inputA;
>
> output2 = owenowingsnoengine+payload;
> -------------------------------------------------------------------------------------------------

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: MATLAB function returning only 'ans'

From: Adam

Date: 4 May, 2013 16:12:08

Message: 6 of 9

"Steven_Lord" <slord@mathworks.com> wrote in message <km0m2l$5n6$1@newscl01ah.mathworks.com>...
>
>
> "Adam " <akitchener88@gmail.com> wrote in message
> news:km0hll$kf9$1@newscl01ah.mathworks.com...
> > "Steven_Lord" <slord@mathworks.com> wrote in message
> > <klu97o$sld$1@newscl01ah.mathworks.com>...
>
> *snip*
>
> > Thanks for your help Steve, although i'm a bit confused as I thought I had
> > already written this in the form:
> >
> > [firstOutput, secondOutput] = WeightModule(30000);
> >
> > It may be of use to try and explain more about what I'm attempting to do:
> >
> > I want to have three modules (one of which is WeightModule). I've
> > currently only been testing this to work with one module and will write
> > the others when I'm happy with the first.
> >
> > These modules shall then be run by a Script and will provide values that
> > are then combined in a calculation at the end of the Script. The main
> > issue at the moment is when I run the script the only return is 'ans' and
> > it is the value for mtow (1.5474). Nothing else exists in the workspace.
> >
> > Below is the Script (MScript)
>
> I'm going to be a bit pedantic here. What you have is not a MATLAB script.
> You have a MATLAB function. The rules for MATLAB functions and MATLAB
> scripts are different, particularly with respect to how you get information
> into and out of them.
>
> > I am using to call the WeightModule and the WeightModule itself (in case
> > I have made any changes to it since posting on here yesterday)
> >
> > Thanks for any help you can provide.
> >
> >
> > SCRIPT: (FILE NAME: MScript)
> > -------------------------------------------------------------------------------------------------
> > function weight = MScript(mtow,finalWeight)
> >
> > fuelWeight = 30000;
> >
> > % Weight Module
> > [mtow,finalWeight] = WeightModule(fuelWeight);
>
> So this will return both the contents of output1 and output2 from
> WeightModule's workspace to the workspace of MScript. The contents of
> output1 will be called mtow in MScript's workspace and the contents of
> output2 will be called finalWeight.
>
> And this line also overrides the mtow and finalWeight input arguments you
> passed into MScript.
>
> > weight = (mtow/finalWeight)
>
> So this function will NOT return the contents of the variables mtow or
> finalWeight to the workspace of the function or script in which MScript was
> called. It will ONLY return the contents of the variable weight to that
> workspace. What the contents of that variable from MScript's workspace will
> be called in the caller's workspace depends on how you called MScript. If
> you called it:
>
> thisIsTheWeight = MScript
>
> then thisIsTheWeight in that function's workspace will contain the contents
> of weight in MScript's workspace. If you called it:
>
> MScript
>
> then MATLAB will assign the contents of weight in MScript's workspace to the
> variable ans in the caller's workspace.
>
> > -------------------------------------------------------------------------------------------------
> >
> > WEIGHT MODULE: (file name: WeightModule)
> > -------------------------------------------------------------------------------------------------
> > function[output1,output2] = WeightModule(inputA)
> >
> > %fuelWeight=30000;
> > payload = 18500;
> > owenowingsnoengine = 36300;
> >
> > output1 = owenowingsnoengine+payload+inputA;
> >
> > output2 = owenowingsnoengine+payload;
> > -------------------------------------------------------------------------------------------------
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com



Thanks for your explanations so far Steve, they've been very helpful and I think I now understand how the variables are stored within different workspaces.

Is there a way I can make every single thing I define or calculate appear in one top level workspace? (i.e. if i've understood correctly can I go and 'grab' the values from WeightModule's workspace and copy them into the base workspace)


Thanks

Subject: MATLAB function returning only 'ans'

From: Steven_Lord

Date: 6 May, 2013 02:36:53

Message: 7 of 9



"Adam " <akitchener88@gmail.com> wrote in message
news:km3c0o$p9t$1@newscl01ah.mathworks.com...
> "Steven_Lord" <slord@mathworks.com> wrote in message
> <km0m2l$5n6$1@newscl01ah.mathworks.com>...

> Thanks for your explanations so far Steve, they've been very helpful and I
> think I now understand how the variables are stored within different
> workspaces.
>
> Is there a way I can make every single thing I define or calculate appear
> in one top level workspace? (i.e. if i've understood correctly can I go
> and 'grab' the values from WeightModule's workspace and copy them into the
> base workspace)

Technically, yes. You could do everything with script files. However, that
increases the chances that changes to variables used in one step in your
program will interact with changes to variables used in another, so I don't
really recommend that for large and/or complicated programs.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: MATLAB function returning only 'ans'

From: Eugenijus Januškevičius

Date: 11 May, 2013 19:32:12

Message: 8 of 9

2013.05.06 05:36, Steven_Lord rašė:

> Technically, yes. You could do everything with script files. However,
> that increases the chances that changes to variables used in one step in
> your program will interact with changes to variables used in another, so
> I don't really recommend that for large and/or complicated programs.

also, it's possible to use global variables, but would not suggest to.

--
Eugenijus Januškevičius
Dept. of Building Structures
Vilnius Gedimina's Technical University

Subject: MATLAB function returning only 'ans'

From: Adam

Date: 11 May, 2013 19:52:07

Message: 9 of 9

Thanks for the replies all. As you can probably tell I was more or less a complete MATLAB novice.

I have now solved what I wanted to do and have been able to get everything 'talking' to everything else exactly how I wanted, and have learnt a lot about MATLAB in the process!


Cheers

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us