Why plot handles don't pass back from function?
5 views (last 30 days)
Show older comments
Thrashercharged
on 5 Apr 2015
Commented: Star Strider
on 13 Apr 2015
My main script (MainFunc) is a Function that contains nested functions. MainFunc calls a nested function (Func1) to do some plotting. But since I'm plotting multiple plots where 80% of the code is repetitive, Func1 calls another function (SubFunc) to do the plots and I pass variables between the 2.
The user is able to erase and replot one of these lines in a different place, so I assign a handle to this line in SubFunc. After SubFunc plots everything and we return to Func1, the lines are deleted and replotted at its new value. I attempt to pass the handle name from SubFunc back to Func1 under a new name. SubFunc uses the same handle every time, but when Func1 calls SubFunc, Func1 is using a different argument each time. The problem is, when the handle is passed back out of SubFunc back to Func1, that different handle name is null, so I'm not able to delete or do anything with that line.
And yes, I have to delete that line in Func1, I can't delete it in SubFunc. Here's a sample of code boiled down to my problem with all extraneous stuff taken out:
function MainFunc
global Line1 Line2;
CylA = 1;
Func1(CylA);
function Func1(Cyl)
figure;
S1 = subplot(411);
SubFunc(S1,Line1)
S2 = subplot(412);
SubFunc(S2,Line2)
Line1 %display value
Line2 %display value
delete(Line1);
delete(Line2);
function SubFunc(S,Line)
Line = plot(S,Cyl*[1 1],[0 10],'m-');
Line %display value
end
end
end
Here's my results (I'm not copying the plot - it plots a line but obviously doesn't delete it because Line1 and Line2 are [].
Line = 177.0051
Line = 179.0018
Line1 = []
Line2 = []
The handle "Line" in SubFunc exists, but when passed back to Func1 they don't. So - how do I plot lines in subplots in a Subfunction using a single handle, and then delete and replot these lines from the main script (or a function above the Subfunction in my case) in these subplots?
Thanks for any insight!
0 Comments
Accepted Answer
Star Strider
on 5 Apr 2015
If you want a variable to be global (not a good idea, avoid globals if at all possible), it has to be declared as global in the function workspace as well as the script workspace. You haven’t defined them in either function workspace.
I would just pass them back as returned variables, for example:
function [Line1, Line2] = Func1(Cyl)
...
[Line1, Line2] = SubFunc(S,Line);
...
function [Line1, Line2] = SubFunc(S,Line)
...
end
end
then of course call them from your script so the variables return to its workspace.
2 Comments
Star Strider
on 13 Apr 2015
My pleasure.
I usually only pass the output of nested functions back to the workspace of the function they’re nested in. I avoid globals at all costs, so I’ve not used them since MATLAB introduced anonymous functions, one of the benefits of them being the ability to pass additional parameters easily to functions they call.
I’m glad one of my ideas solved your problem! I appreciate your feedback, since I learned something about globals.
More Answers (0)
See Also
Categories
Find more on Interactive Control and Callbacks 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!