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

조회 수: 11 (최근 30일)
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.

채택된 답변

Star Strider
Star Strider 2014년 10월 31일
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
Monty Ramadhan
Monty Ramadhan 2014년 11월 4일
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 2014년 11월 4일
My pleasure!
If your code does what you want and you’re happy with it, that works for me!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Abhiram Bhanuprakash
Abhiram Bhanuprakash 2014년 10월 31일
편집: Abhiram Bhanuprakash 2014년 10월 31일
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
Monty Ramadhan
Monty Ramadhan 2014년 10월 31일
편집: Monty Ramadhan 2014년 10월 31일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with Problem-Based Optimization and Equations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by