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:
using a workspace variable within a function

Subject: using a workspace variable within a function

From: Clayton Turner

Date: 9 Nov, 2010 19:02:04

Message: 1 of 2

I'm stuck trying to write some code, I'm pretty sure it's not a hard problem to fix but I'm not sure where to go from here! Here's what I've written:

function speed(tim)
rateup = 0.00004;
ratedown = -0.001;
speed = 49.5; %initial speed
    for i = 1:tim
        speed = speed - (PetrolValve(i,2)-1)*rateup + PetrolValve(i,2)*ratedown;
    end

but it gives the error:

??? Undefined function or method 'OutletValve' for input arguments of
type 'double'.

Error in ==> speed at 7
        speed = speed - (PetrolValve(i,2)-1)*rateup +
        PetrolValve(i,2)*ratedown;


PetrolValve is a variable I imported from a text document into the workspace, is there any way of allowing it to be used within the function? I'm guessing that's what the problem is.
If I type PetrolValve(1,2) into the command window it does output a number.

Thanks for any help :-)

Subject: using a workspace variable within a function

From: Sean

Date: 9 Nov, 2010 19:15:06

Message: 2 of 2

"Clayton Turner" <southwoodelectronics@hotmail.co.uk> wrote in message <ibc5rc$hv$1@fred.mathworks.com>...
> I'm stuck trying to write some code, I'm pretty sure it's not a hard problem to fix but I'm not sure where to go from here! Here's what I've written:
>
> function speed(tim)
> rateup = 0.00004;
> ratedown = -0.001;
> speed = 49.5; %initial speed
> for i = 1:tim
> speed = speed - (PetrolValve(i,2)-1)*rateup + PetrolValve(i,2)*ratedown;
> end
>
> but it gives the error:
>
> ??? Undefined function or method 'OutletValve' for input arguments of
> type 'double'.
>
> Error in ==> speed at 7
> speed = speed - (PetrolValve(i,2)-1)*rateup +
> PetrolValve(i,2)*ratedown;
>
>
> PetrolValve is a variable I imported from a text document into the workspace, is there any way of allowing it to be used within the function? I'm guessing that's what the problem is.
> If I type PetrolValve(1,2) into the command window it does output a number.
>
> Thanks for any help :-)

pass it in as an input argument

function speed(tim,OutletValve,PetrolValve)

...
end


Then it will have access to them.

doc function
is probably a worthwhile read too.

Tags for 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