set uiaxes in matlab function as current figure to plot on

Hi folks,
I had a question on how to plot on uiaxes from an m-file. This question got solved:
I wonder if there's a more elgant way to solve this problem by defining the axes at the beginning of the function and then continuously plot on it instead of defining the axes in every single plot command?! Something like:
Call of function:
myFunction(app.uiaxes)
Function:
function myFunction(uiAxes)
axes(uiAxes)
% plot 1
...
% plot 2
...
% plot x
It works fine the way it is right now, but what if I have an old function with lots of plot commands I want to include to a gui?

 Accepted Answer

I would argue that it makes more sense (more elegant) to require specifying the parent axes to plot. The way that you want may cut down on some letters, but to me is less elegant and more prone to problems and confusion.
I would take the advice of the comment in the other answer: ctrl+F to update your old code.

7 Comments

I know about the command ctrl+F but I'm still curious if there's a way so set the current axes to uiaxes.
My answer is: don't attempt to do that, it is not good practice for app building.
Okay got it. Maybe that's the reason Matlab isn't supporting what I was trying to do ;)
There are older "answers"
in which a staffer suggests the same without a reason
but a recent comment shows that the uifigure has a "CurrentAxes" property that you can apparently set to whatever axes you want.
So it's not that it's no supported, and maybe my declaration of being bad practice shouldn't be taken all that seriously.
But just imagine other scenarios for your future apps (say if you need to manage multiple axes) and ask yourself is it really better for you to specify the target axes outside of your plotting command or inside it.
In my opinion it's depending on the situation which practice is better. If there's an old function existing that I want to implement to my new GUI, it depends on what that function does.
If it only plots in one and the same figure one definition at the beginning would be the easiest way. Okay, with CTRL+F it's easy to replace something but I have to go trough all the code and search for used commands to plot (plot, plot3, line, what ever...). Would be easier to define the axes once.
If there are more axes to be drawn to your suggested way is definitely the better one. Maybe a little more complex but everything get's drawn where it should.
plot(ax,___) creates the line in the axes specified by ax instead of in the current axes (gca). The option ax can precede any of the input argument combinations in the previous syntaxes.
plot function without ax argument, plots into gca - that is of following types:
  • An Axes object.
  • A PolarAxes object.
  • A GeographicAxes object.
  • A standalone visualization
Maybe in one of the future versions they may think of including uiaxes to the list.
CurrentAxes property is useful, but then handle of the figure would have to be stored and "plotting" would actually be changing the Data properties (as mentioned above in Cris' answer), which includes rewriting the old function. Well, changing properties is probably faster than plotting. One can notice the same method used in optimisation plot functions.
You can extend the functionality of the old code, by doing the ctrl+f thing and include axes to plot to, in the old function. add a line on start of the code
ax = gca;
Mario's comment reminds me that while you can set the uifigure's CurrentAxes property, that will probably not help with directing plot commands to specific uiaxes - so really not sure what it's there for (I never looked it up)

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!