Matlab GUI programmatically -> Slider disappears

5 Ansichten (letzte 30 Tage)
Thomas Starzynski
Thomas Starzynski am 24 Mär. 2017
Beantwortet: Divyajyoti Nayak am 11 Jun. 2025
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 Kommentar
Adam
Adam am 24 Mär. 2017
Bearbeitet: Adam am 24 Mär. 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;

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Divyajyoti Nayak
Divyajyoti Nayak am 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';

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by