Matlab GUI programmatically -> Slider disappears

4 次查看(过去 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
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
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';

类别

Help CenterFile 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!

Translated by