Matlab GUI programmatically -> Slider disappears
5 views (last 30 days)
Show older comments
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 Comment
Adam
on 24 Mar 2017
Edited: Adam
on 24 Mar 2017
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;
Answers (1)
Divyajyoti Nayak
on 11 Jun 2025
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 Comments
See Also
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!