Update multiple plots in loop and plot them in correct axes
Show older comments
Hi all,
I'm trying to implement a GUI in MatlabR2019b that displays the data sent from my embedded system. The main problem I'm facing is that my data gets plotted in the wrong window (I'm opening the GUI from another GUI window).
I'd like my GUI to be dynamic in the sense that I can adjust the amount of channels I want to plot. Thus my idea was to do something like this:
data = zeros(numberOfSamples,numberOfChannels);
time = linspace(0,recordingTime,numberOfSamples);
t = tiledlayout(app.Panel,numberOfChannels,1);
for ch=1:numberOfChannels
nexttile(t);
plotList{ch} = plot(time,data(:,ch));
end
for sample = 1:numberOfSamples
data(sample,:) = read(serialObj,numberOfChannels,'single');
for ch=1:numberOfChannels
set(plotList{ch},'YData', data(:,ch))
end
end
But the data does not get plotted in the tiles as I would like them to be. I've come to understand that the problem is based on Matlab deleting the plot objects when a new one is created (resulting in a empty GraphicsPlaceholder array). I have also tried to store the handles to the axes and/or addding them to the plot instead of replacing them, so I can later on retrieve the plot handles from the axes:
axesList{ch} = nexttile(t);
axesList{ch}.NextPlot = 'add'
.
.
.
plt = get(axesList{ch}, 'children') % or alternatively access the axes with app.Panel.Children.Children(ch)
set(plt,'YData', data(:,ch))
But also this did not draw the the plots correctly. However it seems that the axes handles can be saved like this.
As on of my main concerns is fast visual updatess of the GUI, my next hope was using animatedLines
axesList{ch} = nexttile(t);
linesList{ch} = animatedline;
.
.
.
axes(axesList{ch});
addpoints(linesList{ch},time(sample),data(sample,ch))
This does draw the plots in the old window as well. But Matlab seems to discourage axes() in a loop for performance reasons anyways.
Does anybody know a way of how to do this so it plots in the correct axes?
3 Comments
Jesus Sanchez
on 21 Feb 2020
Why don't you try to change the show property of the plots to hide them? This way, you select the plots that you want to show, and hide the rest, without deleting anything. Something like:
h = figure;
set(h, 'Visible', 'off');
Jan Zbinden
on 21 Feb 2020
Jesus Sanchez
on 21 Feb 2020
Oh OK. So if you want to open that GUI from another GUI, have you checked https://es.mathworks.com/help/matlab/creating_guis/creating-multiwindow-apps-in-app-designer.html?lang=en ?
Accepted Answer
More Answers (0)
Categories
Find more on 2-D and 3-D Plots 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!