Error "The length of X must match either the number of rows or the number of columns of Y" while trying to plot data

2 views (last 30 days)
I am trying to plot data following this code:
% Data calculation
load CovidData.mat covid_data
[m,n]=size(covid_data);
country_daily_cases=zeros(1,n);
z=0;
index = find(strcmp([covid_data],app.CountryListBox.Value)); % This allows me to look for data for a particular country
cond = length(index)==1; % Some of these countries have regions associated to them (cond = 0), while some others don't (cond = 1)
if cond == 1
for w=3:n
if w==3
z = z + covid_data{index,w}(1);
else
z = z + covid_data{index,w}(1)-covid_data{index,w-1}(1); % I want to compute the daily cases, so I used this for loop to substract the cases from the previous day end
country_daily_cases(w-2)=z;
z=0;
end
end
end
app.CovidDailyCases = country_daily_cases(1:end-2); % Due to my code I get two extra 0 at the end of my vector, so I remove them, getting a 1x707 vector
% The code for cond == 0 is not part of the problem, so I removed it for better understanding
% Time points
dates=datetime(covid_data(1,3:end),'Format','dd-MMM-uuuu');
app.TimePoints=datenum(dates);
% Plotting code
cla (app.UIAxes,"reset");
if app.CasesButton.Value
if app.DailyButton.Value
bar(app.UIAxes,app.TimePoints,app.CovidDailyCases, 'blue');
app.UIAxes.YColor = 'b';
datetick(app.UIAxes,'x','mmm yy');
end
end
The app.TimePoints vector has 1x707 dimensions, and the app.CovidDailyCases has also 1x707 dimensions, but when I try to run the script, the following error message appears:
Error using bar (line 171)
The length of X must match either the number of rows or the number of columns of Y.
Error in CovidData/OptionButtonGroupSelectionChanged (line 422)
app.plotData();
Why does it say that lengths must match if they do actually match? Sorry if the information provided is scarce, it comes from long parts of code. If you need any extra data or code, I would be happy to provide it. Thank you

Answers (1)

Walter Roberson
Walter Roberson on 6 Apr 2022
When cond == 0 then country_daily_cases=zeros(1,n); which is 1 x number of columns in the covid data.
bar(app.UIAxes,app.TimePoints,app.CovidDailyCases, 'blue');
we have no idea where app.TimePoints comes from. You are not load()'ing it, and it seems to be independent of the number of columns in the data. It seems unlikely that it happens to exactly match the number of columns in the file.
  4 Comments
Pablo Rodríguez Suárez
Pablo Rodríguez Suárez on 7 Apr 2022
Same problem...the weird thing is that when I copy the exact same code into Matlab (not in the app designer), I get a nice plot...so I don't understand why it doesn't work on the gui...
Pablo Rodríguez Suárez
Pablo Rodríguez Suárez on 7 Apr 2022
Ok, it's solved...I forgot to add call the function that computes the daily cases before calling the function to plot the data...
% Selection changed function: OptionButtonGroup
function OptionButtonGroupSelectionChanged(app, event)
app.dailyCountryCases(); % This line solved everything
app.plotData();
Thank you!

Sign in to comment.

Categories

Find more on Develop Apps Using App Designer 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!