Using dendrogram in App Designer

조회 수: 20 (최근 30일)
Christopher Syhr
Christopher Syhr 2020년 12월 30일
댓글: Adam Danz 2023년 6월 8일
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
Christopher Syhr
Christopher Syhr 2020년 12월 31일
Thank you kind Sir.
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