Using dendrogram in App Designer

11 views (last 30 days)
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 Comment
Adam Danz
Adam Danz on 8 Jun 2023
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);

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 30 Dec 2020
Edited: Adam Danz on 8 Jun 2023
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 Comments
Adam Danz
Adam Danz on 8 Jun 2023
I've updated my answer on 08-June-2023 to include the use of onCleanup to restore the original HandleVisibility state.

Sign in to comment.

More Answers (0)

Categories

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

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!