Why plot handles don't pass back from function?

5 vues (au cours des 30 derniers jours)
Thrashercharged
Thrashercharged le 5 Avr 2015
Commenté : Star Strider le 13 Avr 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!

Réponse acceptée

Star Strider
Star Strider le 5 Avr 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 commentaires
Thrashercharged
Thrashercharged le 13 Avr 2015
@Star Strider - thanks for the response.
"If you want a variable to be global ... it has to be declared as global in the function workspace as well as the script workspace."
This didn't exactly work with nested functions. When I took the nested functions out and made the functions .m files I was able to declare global variables in all the functions, but as nested functions I got an error. Not important to me anymore - just thought I'd point this out.
I ended up using your 2nd suggestion. I mistakenly thought passing arguments inside the () as I'd done would go both ways. A more MATLAB experienced co-worker pointed out that the arguments passed within the () were inputs and any outputs would have to be passed in the OutputVariable = FunctionName(InputVariable) format as you recommended.
Star Strider
Star Strider le 13 Avr 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by