Update multiple plots in loop and plot them in correct axes

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

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');
The problem is more that the graphs get plotted into the wrong axes, and not that they need to be hidden. But thanks for the suggestion.

Sign in to comment.

 Accepted Answer

Found a solution (link) that fixed my problem and figured I'd share it.
In case of the animatedline example, assigning the parent axes fixes the plotting problem:
for ch=1:numberOfChannels
axesList{ch} = nexttile(t);
linesList{ch} = animatedline('Parent', axesList{ch});
end

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Release

R2019b

Community Treasure Hunt

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

Start Hunting!