Matlab GUI programmatically -> Slider disappears
조회 수: 3 (최근 30일)
이전 댓글 표시
Hello! I try to make a matlab GUI programmatically and face the problem that my *slider disappears after using it*. I isolated the problem to keep the code short. In this GUI i want to refresh the plotmatrix each time the slider is used (ignore the fact that the value of the slider is completely irrelevant to my programm, as mentioned before i really wanted to keep the code clean thats why i also removed this functionality). Heres the code:
function StackOverflowQuestion_GUI()
% clear memory
clear; close all; clc;
% initialize figure
f = figure;
% create main axes
AX_main = axes('Parent',f,...
'Units','normalized','Position',[.1 .2 .8 .7]);
% create slider
uicontrol('Parent',f,...
'Style','slider','Callback',{@sliderCallback,AX_main},...
'Units','normalized','Position',[0.05 0.05 0.9 0.05]);
plotmatrix(AX_main,randn(500,3));
title('Random Plotmatrix');
end
function sliderCallback(~,~,AX_main) % callback for slider
plotmatrix(AX_main,randn(500,3));
title('Random Plotmatrix NEW');
end
Any help is appreciated! I think i misunderstood the concept of AXES. When i plot to the AXES-handle i created, why are other parts of the figure affected as well? If someone could explain to me how this graphic-handle system basically works that would be very nice too!
댓글 수: 1
Adam
2017년 3월 24일
편집: Adam
2017년 3월 24일
I've never used plotmatrix, but it appears to be removing other components from the UI when continually called. Certainly when I tried your code the slider no longer existed as a child of the figure after I moved it twice.
There is no syntax that I can see though (in R2017a) unless I am missing something, for which an axes handle is a valid first argument. The code runs, but I'm not sure exactly what it is interpreting that axes handle as.
Never put this in a function though either:
% clear memory
clear; close all; clc;
답변 (1개)
Divyajyoti Nayak
2025년 6월 11일
The reason why the slider disappears is because the 'plotmatrix' function replaces all the children of the figure each time it is called. There are two workarounds for this:
1) Use 'hold on' and 'hold off' command before and after calling the 'plotmatrix' function.
hold on;
plotmatrix(AX_main,randn(500,3));
hold off;
2) Set the 'NextPlot' property of the figure object to 'add' after each use of 'plotmatrix'
plotmatrix(AX_main,randn(500,3));
f = AX_main.Parent;
f.NextPlot = 'add';
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!