How to pass an output from a function to be processed in another function

23 views (last 30 days)
So i have a function that inputs coordinates and plots a line
function linetranslation(xa,ya,xb,yb)
if nargin == 0
xa = input('First X Coordinate : ');
ya = input('First Y Coordinate : ');
xb = input('Second X Coordinate : ');
yb = input('Second Y Coordinate : ');
end
clf;
hold on;
axis([-10 10 -10 10]);
if xa==xb
if ya<yb
for yi=ya:0.01:yb;
xi=yi+xa-yi;
plot(xi,yi);
end
else
for yi=ya:-0.01:yb;
xi=yi+xa-yi;
plot(xi,yi);
end
end
else
m=(yb-ya)/(xb-xa);
if xa<xb
for xi=xa:0.01:xb;
yi=m*(xi-xa)+ya;
plot(xi,yi);
end
else
for xi=xa:-0.01:xb;
yi=m*(xi-xa)+ya;
plot(xi,yi);
end
end
end
end
the question is how can i pass xi & yi to another function that processes it further
a little idea here, i have tried adding to the first function
function [xi, yi] = linetranslation(xa,ya,xb,yb)
and adding the code below
function Translate(x,y)
>>>here [xi , yi] = linetranslation(xa,ya,xb,yb);
if nargin == 0
% Check the number of input arguments (nargin)
x = input('Translate X: ');
y = input('Translate Y: ');
end
hold on;
axescenter
axis([-10 10 -10 10]);
plot(xi+x,yi+y);
but it returns xa undefined, on the other hand i need xi and yi to be passed
thanks in advance
UPDATE
i have tried putting
[xi,yi] = translasigaris;
below
function translate(x,y)
but
if nargin == 0
xa = input('First X Coordinate : ');
ya = input('First Y Coordinate : ');
xb = input('Second X Coordinate : ');
yb = input('Second Y Coordinate : ');
end
returns as an infinite loop.

Accepted Answer

Star Strider
Star Strider on 31 Oct 2014
If you want to make ‘xi’ and ‘yi’ available to other functions, the easiest way is to have your ‘linetranslation’ function output them.
Change your original function statement to:
function [xi,yi] = linetranslation(xa,ya,xb,yb)
When you then call it such as in this statement:
[xi,yi] = linetranslation(xa,ya,xb,yb);
‘xi’ and ‘yi’ (by whatever variable names you want to assign them to) will be available in your workspace.
  30 Comments
Monty Ramadhan
Monty Ramadhan on 4 Nov 2014
The original code was intended just to plot a line in matlab, since i've found it that way and it works fine, i didn't bother to change it a bit, but since you've mentioned it, i've applied it to my code, and it works fine!, so thankyou once again. Yes 'xi' and 'yi' are translation functions, but the slanted line it plotted was not what i intend my application to plot, but thanks to your guide and patience i have achieved what i wanted.
What i wanted to achieve is to transform again the line that i've transformed and plotted , and i think i've achieved it, i have tested it and haven't found any bugs yet, if it's not trouble some i would like you to test the code, it's a fully running code, please tell me of flaws and bugs that you found.
Once again, Thankyou.
Star Strider
Star Strider on 4 Nov 2014
My pleasure!
If your code does what you want and you’re happy with it, that works for me!

Sign in to comment.

More Answers (1)

Abhiram Bhanuprakash
Abhiram Bhanuprakash on 31 Oct 2014
Edited: Abhiram Bhanuprakash on 31 Oct 2014
Hi Monty,
I think one way of solving this would be to use a kind of 'main' function.
You may name it whatever you like, for example, let's say, 'translate_main'.
It can be something like this:
function translate_main(xa,ya,xb,yb)
if nargin == 0
xa = input('First X Coordinate : ');
ya = input('First Y Coordinate : ');
xb = input('Second X Coordinate : ');
yb = input('Second Y Coordinate : ');
end
[xi, yi] = linetranslation(xa,ya,xb,yb);
Translate(xi,yi); % This passes the output of linetranslation as input to Translate
end
And, as you have mentioned, the function linetranslation can be declared as:
function [xi, yi] = linetranslation(xa,ya,xb,yb)
and the function Translate can be declared as:
function Translate(x,y)
From the Command Window, you can call translate_main as:
translate_main(xa,ya,xb,yb);
where xa,ya,xb,yb are defined previously in the MATLAB base workspace.
Hope this helps,
Cheers!
Abhiram.
  1 Comment
Monty Ramadhan
Monty Ramadhan on 31 Oct 2014
Edited: Monty Ramadhan on 31 Oct 2014
hi Abhiram, thanks for the answer, but that's not what i'm looking for. I'm trying to process xi & yi from the second function (Translate) which is a different .m file, i'm sorry i didnt mention this before.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!