Using dendrogram in App Designer

11 visualizaciones (últimos 30 días)
Christopher Syhr
Christopher Syhr el 30 de Dic. de 2020
Comentada: Adam Danz el 8 de Jun. de 2023
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 comentario
Adam Danz
Adam Danz el 8 de Jun. de 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);

Iniciar sesión para comentar.

Respuesta aceptada

Adam Danz
Adam Danz el 30 de Dic. de 2020
Editada: Adam Danz el 8 de Jun. de 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 comentarios
Christopher Syhr
Christopher Syhr el 31 de Dic. de 2020
Thank you kind Sir.
Adam Danz
Adam Danz el 8 de Jun. de 2023
I've updated my answer on 08-June-2023 to include the use of onCleanup to restore the original HandleVisibility state.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating, Deleting, and Querying Graphics Objects en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by