Got Questions? Get Answers.
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:
simple return parameter issue

Subject: simple return parameter issue

From: Shane Martin

Date: 9 Apr, 2009 02:47:02

Message: 1 of 6

The problem is I want both return variables to be output on the screen. Instead, the first variable gets output and the second does nothing.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [location velocity] = missile_estimation(t, radarpoints)
format long g;
t_array = 0:0.1:0.5;
x = radarpoints;

loc = 1:3;
vel = 1:3;
z = t;
for i = 1:3
    bx0=x(1,i);
    bx1=(x(2,i)-x(1,i))/(t_array(2)-t_array(1));
    bx2=((x(3,i)-x(2,i))/(t_array(3)-t_array(2))-....
    bx3=(((x(4,i)-x(3,i))/(t_array(4)-t_array(3))-.....
    fx= bx0 +bx1*(z-t_array(1)) + bx2*(z-t_array(1)).....
    loc(i) = fx;
    f2x = bx1 + 2*bx2*z - bx2*t_array(2.....
    vel(i) = f2x;
end
location = loc(1:3);
velocity = vel(1:3);
%%%%%%%%%%%%%%%%%%%

Only location gets output, velocity does not. Maybe, there is some fundamental misunderstanding on matlab return parameters on my side.

OUTPUT
ans =

          9374.84640000009 10040.8144 509.000000000012


I need both variable to output this way and I'm not sure what I'm doing wrong. Any help would be appreciated.

Subject: simple return parameter issue

From: Roger Stafford

Date: 9 Apr, 2009 03:02:03

Message: 2 of 6

"Shane Martin" <llcoolpup882003@yahoo.com> wrote in message <grjnj6$dn6$1@fred.mathworks.com>...
> The problem is I want both return variables to be output on the screen. Instead, the first variable gets output and the second does nothing.
>
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> function [location velocity] = missile_estimation(t, radarpoints)
> format long g;
> t_array = 0:0.1:0.5;
> x = radarpoints;
>
> loc = 1:3;
> vel = 1:3;
> z = t;
> for i = 1:3
> bx0=x(1,i);
> bx1=(x(2,i)-x(1,i))/(t_array(2)-t_array(1));
> bx2=((x(3,i)-x(2,i))/(t_array(3)-t_array(2))-....
> bx3=(((x(4,i)-x(3,i))/(t_array(4)-t_array(3))-.....
> fx= bx0 +bx1*(z-t_array(1)) + bx2*(z-t_array(1)).....
> loc(i) = fx;
> f2x = bx1 + 2*bx2*z - bx2*t_array(2.....
> vel(i) = f2x;
> end
> location = loc(1:3);
> velocity = vel(1:3);
> %%%%%%%%%%%%%%%%%%%
>
> Only location gets output, velocity does not. Maybe, there is some fundamental misunderstanding on matlab return parameters on my side.
>
> OUTPUT
> ans =
>
> 9374.84640000009 10040.8144 509.000000000012
>
> I need both variable to output this way and I'm not sure what I'm doing wrong. Any help would be appreciated.

  Show us how you called on this function. My guess is the trouble lies there.

Roger Stafford

Subject: simple return parameter issue

From: Trance

Date: 9 Apr, 2009 03:05:58

Message: 3 of 6


Obviously, if you called like

>> missile_estimation(t, radarpoints)

then only the first variable is recorded.

if you do

>>[location velocity] = missile_estimation(t, radarpoints)

then nothing is displayed and "location" and "velocity will contain
your values.

If you want to display them just stick

display(location);
display(velocity);

at the end of your function

Subject: simple return parameter issue

From: Matt Fig

Date: 9 Apr, 2009 03:12:04

Message: 4 of 6

Do you see a difference here:

A = randperm(10);
% Now call the sort function.
B = sort(A) % Return the sorted matrix
[B,ind] = sort(A) % Return the sorted matrix and the index

Subject: simple return parameter issue

From: Shane Martin

Date: 9 Apr, 2009 03:13:04

Message: 5 of 6

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <grjofb$b90$1@fred.mathworks.com>...
> "Shane Martin" <llcoolpup882003@yahoo.com> wrote in message <grjnj6$dn6$1@fred.mathworks.com>...

>
> Show us how you called on this function. My guess is the trouble lies there.
>
> Roger Stafford

function [location velocity] = project_step2(t)

radarpoints = radar();
t = 1;
[location velocity] = missile_estimation(t, radarpoints);

end
%%%%%%%%%%%%%%%%

This is the only place the function is called...I pass it t =1 just to test my code but I don't think that effects it.

Subject: simple return parameter issue

From: Roger Stafford

Date: 9 Apr, 2009 05:57:01

Message: 6 of 6

"Shane Martin" <llcoolpup882003@yahoo.com> wrote in message <grjnj6$dn6$1@fred.mathworks.com>...
> .....
> function [location velocity] = missile_estimation(t, radarpoints)
> .....

  On my ancient version of matlab, if there is no comma placed between the output arguments in the definition of a function, it gives an error when the function is called. Perhaps on your version, it simply fails to produce the second argument. As an experiment try placing a comma in both the function's definition and in the calling sequence. See if that helps. Anyway, that is the way functions are supposed to be defined and called - with commas separating all arguments.

Roger Stafford

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