Plot using radio buttons overwritten instead of substituted
    조회 수: 3 (최근 30일)
  
       이전 댓글 표시
    
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
채택된 답변
  Voss
      
      
 2022년 3월 21일
        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
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


