Plot using radio buttons overwritten instead of substituted
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Pablo Rodríguez Suárez
am 21 Mär. 2022
Kommentiert: Pablo Rodríguez Suárez
am 24 Mär. 2022
I am trying to change the plot depicted on my app using radio buttons (one for cases, other for deads and other for both), but when I click on other button to change the plot, the new data overwrites the previous one, instead of substituting it. I will add a couple of screenshots for better understanding:

In this picture you can see how the Cases button is selected, and cases are depicted. Nothing wrong with that.

But in this second picture you can see how the deaths button is pressed but both deaths (red line) and cases (blue histogram) are depicted. And that blue histogram would never come out.
The code I am using for plotting is this:
function plotData(app)
load CovidData.mat covid_data
if app.CasesButton.Value
yyaxis(app.UIAxes,'left');
bar(app.UIAxes,app.TimePoints,app.CovidCases, 'blue');
datetick(app.UIAxes,'x','mmm yy');
end
if app.DeathsButton.Value
yyaxis(app.UIAxes,'right');
plot(app.UIAxes, app.TimePoints, app.CovidDeaths, 'r');
datetick(app.UIAxes, 'x','mmm yy');
end
if app.BothButton.Value
yyaxis(app.UIAxes,'left');
bar(app.UIAxes,app.TimePoints,app.CovidCases, 'blue');
yyaxis(app.UIAxes,'right');
plot(app.UIAxes, app.TimePoints, app.CovidDeaths, 'r');
datetick(app.UIAxes, 'x','mmm yy');
end
And the radio button callback looks like this:
% Selection changed function: DatatoplotButtonGroup
function DatatoplotButtonGroupSelectionChanged(app, event)
app.plotData();
end
What am I doing wrong?
0 Kommentare
Akzeptierte Antwort
Voss
am 21 Mär. 2022
You need some way to delete old plots whose corresponding radiobutton was selected at some point but now is no longer selected. In this case, the easiest way is probably to put
cla(app.UIAxes);
in plotData() before the plotting functions are called, which will clear the axes before the selected plots are created. So plotData() would look like this:
function plotData(app)
load CovidData.mat covid_data
cla(app.UIAxes); % clear out old plots
if app.CasesButton.Value
% make new plots, same as you have now
end
% etc., same as you have now
end
3 Kommentare
Weitere Antworten (0)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!