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

14 次查看(过去 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
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