Plot using radio buttons overwritten instead of substituted
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Pablo Rodríguez Suárez
il 21 Mar 2022
Commentato: Pablo Rodríguez Suárez
il 24 Mar 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 Commenti
Risposta accettata
Voss
il 21 Mar 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 Commenti
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!