Using dendrogram in App Designer

11 次查看(过去 30 天)
I'm trying to integrate a dendrogram in to my App in App Designer.
However i can't seem to find a way to plot the dendrogram into the grap within the App.
This is my code:
X = table2array(t);
Z = linkage(X,'single','euclidean');
dendrogram(app.UIAxes,Z);
It seems that the sytax
dendrogram(app.UIAxes,Z);
does not work. Is there a workaround? Maybe a way to use plot() instead of dendrogram().
Thank you very much!
Chris
  1 个评论
Adam Danz
Adam Danz 2023-6-8
dendrogram started supporting an optional axes argument in MATLAB R2022a.
For MATLAB releases before R2022a, see the answer below for a solution. Otherwise, specify the axes handle,
dendrogram(app.UIAxes,Z);

请先登录,再进行评论。

采纳的回答

Adam Danz
Adam Danz 2020-12-30
编辑:Adam Danz 2023-6-8
How to call an external plotting function without an axes argument in AppDesigner
When calling an external plotting function from App Designer or from a uifigure, the best approach is to provide an axes argument to specify the axes parent within the graphics functions.
Example:
myPlottingFcn(app.UIAxes, x, y)
function myPlottingFcn(ax, x, y)
plot(ax, x, y)
end
What if I cannot add an axes argument to the external function?
Sometimes the external plotting function does not offer an axes argument and only plots on the current axes (gca). This poses a problem with axes within a UIFigure (e.g. App Designer). By default, the HandleVisibility of UIFigures is off so the axes within those figures are not discoverable by gca.
From r2020a and later, you can set the UIFigure's HandleVisibility to on so that the app's axes can be detected as "current". Then, programmatically make the app's axes "current". Then, you can run the plotting function which will access your app's axes.
Demo
  • app.UIFigure is the handle to your app's figure
  • app.UIAxes is the handle to the app's axes
% These 2 lines will ensure that the original HandleVisibility
% values will be restored after this section runs. These lines
% are optional but recommended.
origState = app.UIFigure.HandleVisibility;
handleVisCleanup = onCleanup(@()set(app.UIFigure,'HandleVisibility',origState));
% Temporarily turn on the figure's HandleVisibility so the
% axes are detected by gca()
app.UIFigure.HandleVisibility = 'on';
% Set your app's axes to be current so gca() chooses the correct axes
set(groot, 'CurrentFigure', app.UIFigure)
set(app.UIFigure,'CurrentAxes',app.UIAxes)
% Call the external plotting function
dendrogram(__);
% This line is optional if your function ends here. It will run the
% restoration.
clear handleVisCleanup
  2 个评论
Adam Danz
Adam Danz 2023-6-8
I've updated my answer on 08-June-2023 to include the use of onCleanup to restore the original HandleVisibility state.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by