You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How to pass an output from a function to be processed in another function
11 views (last 30 days)
Show older comments
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
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
on 31 Oct 2014
Edited: Monty Ramadhan
on 31 Oct 2014
i have tried putting
[xi,yi] = linetranslation(xa,ya,xb,yb);
in function translate right below
function Translate(x,y)
but it still returns 'xa' is undefined
and when i put
[xi,yi] = linetranslation;
under
function Translate(x,y)
it returns this code below as an infinite loop
if nargin == 0
xa = input('First X Coordinate : ');
ya = input('First Y Coordinate : ');
xb = input('Second X Coordinate : ');
yb = input('Second Y Coordinate : ');
end
Star Strider
on 31 Oct 2014
If you don’t supply your function with any input arguments, the nargin function will always evaluate to zero. Nothing you can do inside the function will change that.
One way around that would be to have another if block:
if nargin == 0
argnr = 0;
end
if argnr == 0
xa = input('First X Coordinate : ');
ya = input('First Y Coordinate : ');
xb = input('Second X Coordinate : ');
yb = input('Second Y Coordinate : ');
argnr = 1;
end
However I would completely delete this if block:
if nargin == 0
xa = input('First X Coordinate : ');
ya = input('First Y Coordinate : ');
xb = input('Second X Coordinate : ');
yb = input('Second Y Coordinate : ');
end
First, because anything you enter there is going to overwrite whatever you pass as arguments to your function, and...
Second, because MATLAB will throw a ‘not enough input arguments’ error if you don’t pass all of them and you don’t have a nargin check.
The solution is to:
- Delete the current if block,
- Pass all the arguments to the function in your script,
- Return the arguments you want as your function outputs.
Monty Ramadhan
on 1 Nov 2014
Edited: Monty Ramadhan
on 1 Nov 2014
i want the user to input the variables to the function and pass the output arguments, is there a solution?
could you give me the example?
Star Strider
on 1 Nov 2014
If you want the user to input the information interactively using input-type statements within the function and not pass them as arguments, I would do this:
function [xi,yi] = linetranslation()
so there are no input arguments, then instead of the if block and your input lines, replace them with this code to use a GUI to input the coordinates:
dlg_title = 'Enter Coordinates:';
prompt = {'First X Coordinate:','First Y Coordinate:','Second X Coordinate:','Second Y Coordinate:'};
coords = inputdlg(prompt, dlg_title,[1 45; 1 45; 1 45; 1 45]);
[xa,ya,xb,yb] = deal(str2num(char(coords)));
It’s easier to work with than the Command Window input function, and doesn’t clutter the Command Window.
The dialog box looks like this:
It works like any other dialog box, and returns the ‘xa’, ‘ya’, ‘xb’, ‘yb’ as numbers in the deal call in the line that follows the ‘coords’ assignment. Use them in your code exactly as you do now. The function will then return [xi,yi] as you want.
Monty Ramadhan
on 1 Nov 2014
Edited: Monty Ramadhan
on 1 Nov 2014
okay so i've modified my code by replacing the function header with yours
and replacing the if input block with your GUI input dialogue
when i input coordinates with my if block which is 1,1,1,5
i get
when i input with my if nargin block i get
and the second function still calls the dialogue for looping even when
function [xi,yi] = linetranslation() %is empty
Star Strider
on 1 Nov 2014
Use this:
dlg_title = 'Enter Coordinates:';
prompt = {'First X Coordinate:','First Y Coordinate:','Second X Coordinate:','Second Y Coordinate:'};
coords = inputdlg(prompt, dlg_title,[1 45; 1 45; 1 45; 1 45]);
[xa,ya,xb,yb] = coords{:};
xa = str2num(xa);
ya = str2num(ya);
xb = str2num(xb);
yb = str2num(yb);
It will work.
Monty Ramadhan
on 1 Nov 2014
Edited: Monty Ramadhan
on 1 Nov 2014
thank you! it did work!, but it still loops the dialogue in the second
and didnt pass the output arguments, when i closed the dialogue it returns this error
Star Strider
on 1 Nov 2014
My pleasure!
I need to see your complete code for your ‘linetranslation’ function in order to see what’s wrong.
Also by calling it as:
linetranslation
you will only get ‘xi’. You have to call it as:
[xi,yi] = linetranslation
to get both outputs.
Monty Ramadhan
on 1 Nov 2014
Edited: Monty Ramadhan
on 1 Nov 2014
first of all thankyou for helping me and here's my 'linetranslation' code
function [xi,yi] = linetranslation()
dlg_title = 'Enter Coordinates:';
prompt = {'First X Coordinate:','First Y Coordinate:','Second X Coordinate:','Second Y Coordinate:'};
coords = inputdlg(prompt, dlg_title,[1 45; 1 45; 1 45; 1 45]);
[xa,ya,xb,yb] = coords{:};
xa = str2num(xa);
ya = str2num(ya);
xb = str2num(xb);
yb = str2num(yb);
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);
plot(xi,yi);
end
else
for xi=xa:-0.01:xb;
yi=m*(xi-xa)+ya;
plot(xi,yi);
plot(xi,yi);
end
end
end
axescenter
translate
and this is my 'translate' function
function translate(x,y)
[xi,yi] = linetranslation();
dlg_title = 'Enter Coordinates:';
prompt = {'Translate X:','Translate Y:'};
coords = inputdlg(prompt, dlg_title,[1 45; 1 45]);
[x,y] = coords{:};
hold on;
axescenter
axis([-10 10 -10 10]);
plot(xi+x,yi+y);
the code axescenter refers it to this file here
Star Strider
on 1 Nov 2014
My pleasure!
I haven’t run your code with ‘translate’, because I believe one of your problems is that inputdlg returns a cell, and the following line:
[x,y] = coords{:};
deals the two inputs to ‘x’ and ‘y’ respectively, as desired. They are however character representations of the numbers entered in inputdlg, so you have to add these lines:
x = str2num(x);
y = str2num(y);
just after the inputdlg call to convert the strings to actual double-precision numbers.
See if that improves things.
Also, if you’re not going to pass (x,y) to ‘translate’ as input arguments (entering them within the function using inputdlg), avoid confusion and define it as:
function translate()
instead.
I have ‘axiscenter’. It’s one of Matt Fig’s brilliant contributions.
Monty Ramadhan
on 1 Nov 2014
silly me, i've forgot about adding the string to number conversion. Right.., so i changed the function header of 'translate' and added the conversion, and it still returns the same error as the image i've posted above. i'm really wondering why it returns this error when i call 'translate' from 'linetranslation'
??? Reference to non-existent element of a cell array.
Error in ==> linetranslation at 6
[xa,ya,xb,yb] = coords{:};
Error in ==> translate at 2
[xi,yi] = linetranslation();
Star Strider
on 1 Nov 2014
The code I provided works, and it apparently worked before. I don’t know why it’s not working now in your code.
Remove the semicolons from all the inputdlg calls and see what the result is.
Monty Ramadhan
on 1 Nov 2014
Edited: Monty Ramadhan
on 1 Nov 2014
My mistake, the function 'linetranslation' works fine, but when running it with 'translate' by adding it under 'axescenter' it loops the dialogue from 'linetranslation' and returns the error above when i close it
this is my 'translate' function as it is now
function translate()
[xi,yi] = linetranslation();
dlg_title = 'Enter Coordinates:';
prompt = {'Translate X:','Translate Y:'};
coords = inputdlg(prompt, dlg_title,[1 45; 1 45]);
x = str2num(x);
y = str2num(y);
hold on;
axescenter
axis([-10 10 -10 10]);
plot(xi+x,yi+y);
Star Strider
on 1 Nov 2014
The easiest way to solve that problem is to not call ‘linetranslation’ from within ‘translate’. Do this instead:
- Take the ‘linetranslation’ call out of ‘translate’,
- Put (x,y) input arguments into ‘translate’,
- Call ‘linetranslation’ first,
- Consider wrapping both functions in another function file, depending on what you want to do with theses functions.
Make appropriate changes to the initial lines of your functions so that you can use them this way:
[xi,yi] = linetranslation
translate(xi,yi)
That should solve your problem. Sometimes, nesting functions such as you did, with potentially interfering inputdlg calls simply won’t work. You might consider using clf after the str2num calls, since UI objects are considered figures (or at least they were before R2014b), but I can’t guarantee that will work.
I have no other suggestions. See if the sequential call idea works.
Monty Ramadhan
on 2 Nov 2014
Edited: Monty Ramadhan
on 2 Nov 2014
thanks for helping, i'll figure out something for the moment.
Is there any way to pass the arguments with a different method? using classes maybe?
I've found an interesting link here ,
i'm new at 'MATLAB' so i'm having quite a difficult time understanding the method formats
Star Strider
on 2 Nov 2014
My pleasure!
I’m not sure how the ideas in that link would help you. The data already have to exist in the workspace for that to work, and in your situation, they do not.
I would simply enter all the data in your main program (using inputdlg), pass it to ‘linetranslation’ through its input argument list as data, then pass the data returned by ‘linetranslation’ to ‘translate’ in its input argument list, as I illustrated earlier.
Using inputdlg from within your functions as you are, you’re making a relatively simple problem difficult to the point that it may have no workable solutions.
Monty Ramadhan
on 2 Nov 2014
thanks!, i've modified my code so that the function 'linetranslation' plots 'xi' and 'yi' and plot 'xj' and 'yj' which is
xj=xi+x;
yj=yi+y;
As you suggested i nested 'translate' within 'linetranslation' and share the variables 'xj' and 'yj', now it plots 2 line from 'linetranslation', after plotting the lines i've modified it to call 'translate' and calling another input dialogue inside the function.
What i hope to achieve now is that the user could translate the second line from the 'linetranslation' function which is 'xj' and 'yj', this is my modified code in one m file.
The function does not returns any errors, but the problem is that function 'translate' doesn't plot a new line in the figure after executed, please review my code and thanks in advance, i think i might have made a stupid mistake somewhere
function [xj,yj] = linetranslation()
dlg_title = 'Enter Coordinates:';
prompt = {'First X Coordinate:','First Y Coordinate:','Second X Coordinate:','Second Y Coordinate:','X Translation','Y Translation'};
coords = inputdlg(prompt, dlg_title,[1 45; 1 45; 1 45; 1 45; 1 45; 1 45]);
[xa,ya,xb,yb,x,y] = coords{:};
xa = str2num(xa);
ya = str2num(ya);
xb = str2num(xb);
yb = str2num(yb);
x = str2num(x);
y = str2num(y);
clf;
axescenter
hold on;
axis([-10 10 -10 10]);
if xa==xb
if ya<yb
for yi=ya:0.01:yb;
xi=yi+xa-yi;
xj=xi+x;
yj=yi+y;
plot(xi,yi);
plot(xj,yj,'r');
end
else
for yi=ya:-0.01:yb;
xi=yi+xa-yi;
xj=xi+x;
yj=yi+y;
plot(xi,yi);
plot(xj,yj,'r');
end
end
else
m=(yb-ya)/(xb-xa);
if xa<xb
for xi=xa:0.01:xb;
yi=m*(xi-xa)+ya;
xj=xi+x;
yj=yi+y;
plot(xi,yi);
plot(xj,yj,'r');
end
else
for xi=xa:-0.01:xb;
yi=m*(xi-xa)+ya;
xj=xi+x;
yj=yi+y;
plot(xi,yi);
plot(xj,yj,'r');
end
end
end
function translate()
dlg_title = 'Enter Coordinates:';
prompt = {'Translate X:','Translate Y:'};
coords = inputdlg(prompt, dlg_title,[1 45; 1 45]);
[a,b] = coords{:};
a = str2num(a);
b = str2num(b);
hold on;
axescenter
axis([-10 10 -10 10]);
plot(xj+a,yj+b);
end
translate(xj,yj);
end
Star Strider
on 2 Nov 2014
Save the quantities you want to plot in an array and then plot them after the loop. Plotting inside an iterating loop will likely not give you the result you want.
Star Strider
on 2 Nov 2014
Initialise a counter just before the start of your if blocks, for instance:
k = 1;
if xa==xb % Start Of Outer ‘if’ Block
then in your loop, set new variables, for instance ‘fx’ and ‘fy’ and set:
fx(k) = xj+a; % The last time you calculate ‘xj+a’ In Loop
fy(k) = yj+b; % The last time you calculate ‘yj+b’ In Loop
then just before the end of the loop, add:
k = k+1;
end % Last Statement In Loop
then after the loop:
figure
plot(fx, fy)
axis([-10 10 -10 10])
Monty Ramadhan
on 3 Nov 2014
thanks for bearing with my questions. Yes, i've proved your point where you said that it won't give the results i wanted. I made a new file without the if blocks and it passes the arguments from the parent function to the nested functions just fine. Just to clarify the example in your post, is this what you mean?
k = 1;
if xa==xb
if ya<yb
for yi=ya:0.01:yb;
xi=yi+xa-yi;
xj(k)= xi+a;
yj(k)= yi+b;
k = k+1;
end
axescenter
plot(xi,yi);
plot(xj,yj);
axis([-10 10 -10 10]);
Star Strider
on 3 Nov 2014
I doubt plotting ‘xi’ and ‘yi’ would work, since they still seem to be single values, and they will be the last values they had at the end of the loop.
You are correct on the other plot call however. This is what I intended:
. . . CODE . . .
xj(k)= xi+a;
yj(k)= yi+b;
. . . CODE . . .
plot(xj,yj);
axis([-10 10 -10 10]);
Monty Ramadhan
on 4 Nov 2014
Yes, it returns xi & yi as a point on the figure not as a line as i intended. How i can manage this to pass the variables inside the loop to the nested function?
Monty Ramadhan
on 4 Nov 2014
Edited: Monty Ramadhan
on 4 Nov 2014
First of all, thankyou for commenting back and your patience guiding me.
I have a feeling that this is going to be done soon! the line shows in the figure and it calls 'translate' brilliantly! but a little problem when i subscript xi & yi, the lines comes out as a diagonal line, but xj & yj comes out vertical as my inputs are 'xa,ya,xb,yb' are '1,1,1,5' and the translations as shown in the image below, the third line which is located in the nested function 'translate' is 'xk' & 'yk' and it shows the line brilliantly as i intended it to be, the only problem now is 'xi' and 'yi' it self
as my if block code is now
k = 1;
if xa==xb
if ya<yb
for yi=ya:0.01:yb;
xi=yi+xa-yi;
xj(k)= xi+x;
yj(k)= yi+y;
xi(k)= xi;
yi(k)= yi;
k = k+1;
end
plot(xi,yi);
plot(xj,yj,'r');
Star Strider
on 4 Nov 2014
My pleasure!
I don’t understand this:
xi=yi+xa-yi;
since it’s equivalent to:
xi = xa;
I don’t know how your calculate ‘xa’ or ‘yi’, or the reason you’re plotting them. If ‘xj’ and ‘yj’ work as you want them to, isn’t that enough? Besides, if ‘xi’ and ‘yi’ are translation functions, it would stand to reason that they would plot as a slanted line reflecting the translation you want them to perform.
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
on 4 Nov 2014
My pleasure!
If your code does what you want and you’re happy with it, that works for me!
More Answers (1)
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
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.
See Also
Categories
Find more on Get Started with Problem-Based Optimization and Equations 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)