Get current axes from multiple figures.

96 ビュー (過去 30 日間)
Gurkenglas
Gurkenglas 2015 年 5 月 11 日
コメント済み: dpb 2023 年 11 月 11 日
Hi,
I have a simulink model and I am trying to plot in real time during the simulation via s-functions. This works. BUT: If I click during simulation with the mouse on the figures, all "line(...)"-fcts are drawn into the last clicked window. This means, the current axes change to the figure that was clicked last. How can I change this behaviour?
For example, this doesnt prevent that the line is drawn into the false figure if the user clicks into a different window:
axes_h = get(fig_h,'CurrentAxes');
line('XData',[0 t],'YData',[0 vel]);

採用された回答

Walter Roberson
Walter Roberson 2015 年 5 月 11 日
Please read why you should always parent your graphics operations here

その他の回答 (4 件)

dpb
dpb 2015 年 5 月 11 日
I don't use Simulink (in fact never even seen an installation), but I think the answer is to save the handle to the axes created when you begin the plot/simulation then use that specific handle instead of using the default form as above which plots to gca.
BTW, line is very unfriendly in that it, unlike plot and friends, doesn't accept a handle; it only knows how to plot to gca. Hence, if you are going to use line, then you need to add
axes(axes_h) % make axes_h current axes object for subsequent line()
prior to the call to line. The problem with this is still it's not atomic so that you can't completely rule out the focus being shifted in between.
I'd suggest plot instead, then
plot(axes_h, ...
works or you can even fold the get inside it to retrieve it (or save the target axes as a persistent global if there is just one instance during the simulation).
  2 件のコメント
Walter Roberson
Walter Roberson 2015 年 5 月 11 日
line(x,y,'Parent',TheAxis)
there are a number of functions that take a 'Parent' parameter, and all of the usual ones that can optionally take an axis as the first argument will accept the 'Parent' name-value pair.
dpb
dpb 2015 年 5 月 12 日
Ah! yeah. Sometimes the trees get in the way of the forest...but it seems peculiar that it's "odd man out" of so many of the plotting functions which do accept a handle; it's a nonorthogonal feature set that makes coding more difficult than should be having to remember the peculiar features.

サインインしてコメントする。


Stephen23
Stephen23 2015 年 5 月 11 日
編集済み: Stephen23 2015 年 5 月 11 日
The line documentation clearly lists this syntax:
line(X,Y,Z,'PropertyName',propertyvalue,...)
and the properties includes Parent... which means you can do this:
line('XData',[0,t], 'YData',[0,vel], 'Parent',axes_h)

Gurkenglas
Gurkenglas 2015 年 5 月 11 日
It works! Thanks. Here is my code:
fig_h = figure(...);
axes_h = axes('Parent',fig_h);
line('XData',[0 t],'YData',[0 vel],'parent',axes_h);

Alexander
Alexander 2023 年 11 月 8 日
In my case, I have a figure handle, but don't have an axis handle saved. Probably a more elegant solution exist, but I've come up with this:
fig = figure(); % this is known
axes %this I need to access
ax = findobj(fig.Children, 'type', 'axes')
  3 件のコメント
Alexander
Alexander 2023 年 11 月 10 日
Thank you for the detailed answer!
ax = gca(fig);
That solution works perfectly. It is very useful, because help page does not mention that you could pass figure handle as an argument to the gca.
dpb
dpb 2023 年 11 月 11 日
"...help page does not mention that you could pass figure handle as an argument to the gca."
Indeed, that seems an oversight worthy of a product improvement submittal to TMW support. Dunno that I had ever looked at the specific page before, it just seemed too self-evident that a figure handle would be a valid argument.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by