How can I prevent a function from creating a separate window when acting on a UIfigure handle?
3 次查看(过去 30 天)
显示 更早的评论
I have a script that reads in some data and a UI figure with uibuttons that change how it is plotted. I've stripped the non-essential parts that don't relate to the issue out of the code. This SHOULD create a UI window that has 2 UI slider elements and a push putton that reads the current value of the sliders and uses that to plot data in the same figure to an axes inside the window on the right created by UIaxes. It will plot the data as intended, BUT it also creates a new figure window with a blank axes in it when the "update" button is pressed. How do i get it to stop doing this? If the plot() function is called outside of a local function it will plot to the correct axes, but I need it to be inside the update function so that it can replot when UI elements are changed.
% some mock data to make the simplified code run
raw_spectra.ampl = [1 2 3];
raw_spectra.field = [1 2 3];
% make the uifigure and axes
fig = uifigure("Position", [50, 300, 1200, 900]);
ax = uiaxes(fig,'Position',[350, 40, 820, 820]);
% make an element in the uifigure that adjusts a parameter that affects how
% plotting is done.
fig.UserData.horizStagger = uislider(fig,"Limits",[0 7],"Position",[30, 250, 200 3],...
'Value', 0);
fig.UserData.vertStagger = uislider(fig,"Limits",[0 3],"Position",[30, 170, 200 3],...
'Value', 0.5);
% a ui button element that when pushed, plots or replots the data with the
% parameters selected with the other UI elements
update_btn = uibutton(fig, 'push', 'Position', [130, 450, 125, 22], ...
'Text', 'Update', 'ButtonPushedFcn', ...
@(update_btn, event) update(update_btn, fig, raw_spectra));
function update ( ~, fig, raw_spectra)
staggeredSpectra = stagger(raw_spectra, fig.UserData.horizStagger.Value, ...
fig.UserData.vertStagger.Value);
for i =1:numel(staggeredSpectra)
plot(fig.Children(end), staggeredSpectra(i).field, ...
staggeredSpectra(i).ampl);
hold on;
end
hold off;
end
function spectra = stagger (input_spectra, horizStagger, vertStagger)
for s = 1:numel(input_spectra)
%adjust center so that that maxima line up with staggering added for
%horizontal offset
spectra(s).field = input_spectra(s).field + horizStagger;
%scale relative to the largest spectrum height and apply vertical
%offset
spectra(s).ampl = input_spectra(s).ampl * vertStagger * (s-1);
end
end
4 个评论
Voss
2023-8-4
@Andrew Morris: Have you tried the modification given in my answer? I ran it, and it worked (version R2022a).
采纳的回答
Voss
2023-8-4
It's because of the
hold on
without specifying an axes.
Specify the axes explicitly, e.g.:
hold(fig.Children(end),'on');
Similarly for the hold off a couple lines later.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!