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:
Function call error

Subject: Function call error

From: Gamini

Date: 6 Oct, 2010 12:08:03

Message: 1 of 2

Hello Everybody;

I came across an interesting issue and couldn't figure it out after reading all the blog postings and I was wondering whether you could give me any hints.

It is a very simple function call: Input variable is 'x' and as you can see select the output depending on the value of 'x'

function [pinkCar yellowCar whiteCar blueCar greenCar]=newCar(x)

     if x==2; pinkCar=100;
        elseif x==3; yellowCar=100;
        elseif x==4; whiteCar=100;
        elseif x==5; blueCar=100;
        elseif x==6; greenCar=100;
     end
end

When I call this function to the main program the execution of the program terminate with the following error message.

Error in ==> newCar at 2

??? Output argument "pinkCar" (and
maybe others) not assigned during
call to
"C:\Users\root\Documents\MATLAB\newCar.m>newCar".

Error in ==> testSelectCar5 at 79
    [pinkCar1 yellowCar1 whiteCar1
    blueCar1 greenCar1]=newCar(x1);
 
??? Input argument "x" is undefined.

Error in ==> newCar at 3
     if x==2; pinkCar=100;
 
Any help I would really appreciate.
 
Regards;

Gamini

Subject: Function call error

From: Sean

Date: 6 Oct, 2010 13:30:23

Message: 2 of 2


> function [pinkCar yellowCar whiteCar blueCar greenCar]=newCar(x)
>
> if x==2; pinkCar=100;
> elseif x==3; yellowCar=100;
> elseif x==4; whiteCar=100;
> elseif x==5; blueCar=100;
> elseif x==6; greenCar=100;
> end
> end
>
> When I call this function to the main program the execution of the program terminate with the following error message.
>
> Error in ==> newCar at 2
>
> ??? Output argument "pinkCar" (and
> maybe others) not assigned during
> call to
> "C:\Users\root\Documents\MATLAB\newCar.m>newCar".
>
> Error in ==> testSelectCar5 at 79
> [pinkCar1 yellowCar1 whiteCar1
> blueCar1 greenCar1]=newCar(x1);
>
> ??? Input argument "x" is undefined.
>
> Error in ==> newCar at 3
> if x==2; pinkCar=100;

Well what happens if x ==3? Where is pinkcar assigned? The error message is trying to tell you something. It assigned isn't because you have exclusive conditions.

Depending on what you actually want to do with the cars you could initialize all of the cars and then only change the one car to 100. I.e.
pinkcar = 0;
yellocar = 0;
bluecar = 0;
...

if x== 2
pinkcar = 100;
elseif...
end

That way all of the cars are zero except for the chosen one and you won't get that error.

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