Why do I received Error using Plot?
Mostra commenti meno recenti
Dear Matlab Expert!
I am a student and still new to Matlab. Currently, I had been learning the COVID-19 model and I would like to learn the coding of this model. In order to learn the coding, I had downloaded the "SIRD model for COVID-19 outbreaks" coding that I found in the File Exchange and tried to run the coding. But when I run the sird_covid_run_ita.m, I got this error :
Error using plot
Vectors must be the same length.
Error in sird_covid_run_ita (line 121)
plot(data_long,I,'-r',date,I_exp,'r*',data_long,R,'-g',date,R_exp,'gd');
Can anyone help me? What is meant by the Vectors must be the same length? I did tried many times to fix the error but it is quite hard to do so as I am still new with the Matlab. I would be really, really grateful if anyone can help me run this coding without error.
Thank you very much and I appreciate your help so much :)
5 Commenti
"What is meant by the Vectors must be the same length?"
Exactly that -- the "X" and "Y" vectors to plot must have the same number of elements -- and in the command line above at least one pair of those isn't for your case for some reason...
The command
plot(data_long,I,'-r',date,I_exp,'r*',data_long,R,'-g',date,R_exp,'gd');
is trying to plot four separate lines
plot(data_long,I ,'-r', ...
date ,I_exp,'r*', ...
data_long,R ,'-g', ...
date ,R_exp,'gd');
rewriting the format of the command to separate the four.
I and R are trying to be plotted against data_long and I_exp and R_exp are trying to be plotted against data, so those two pairs will have to have same number of values in each.
Which is the offending you'll have to see -- set a breakpoint in the function just before the plot() command and look at each of the variables in turn to see how long they are -- at least one will be different. Now, why, would take going to see just how they're being computed to see where there must be some sort of logic error.
Is there a sample dataset that is with the download? Does it not run successfully?
William Rose
il 17 Set 2022
I cannot run the code since I am currently using Matlab 2016. This old version does not suport some of the commands in the script. I hope to have my regular computer back soon.
The error message indicates that the number of x-values and the number of y-values are not equal. They must be equal for plot() to plot the data set.
I have looked at the code on the File Exchange site. I see you posted your question there also. The error occurs in figure 6. The command that fails is
plot(data_long,I,'-r',date,I_exp,'r*',data_long,R,'-g',date,R_exp,'gd')
hold on
The command above plots four data sets. You can look in the list of variab;les in the "Workspace" pane of the Matlab window, to determine the length of data_long and I and R (they should all be the same length), and the length of date, I_exp, and R_exp (they should all be the same length).
You can also try dividing the plot() command above into 4 separate commands.
plot(data_long,I,'-r') %
hold on
plot(date,I_exp,'r*')
plot(data_long,R,'-g')
plot(date,R_exp,'gd')
This will let you determine which plots fail and which plots work. You can comment out the plots that do not work. This will allow the script will run to completion.
Good luck.
Image Analyst
il 17 Set 2022
I don't get that error when I run the m-file in the URL you gave. I get this:
Error using split
First argument must be text.
Error in test6 (line 7)
date=(split(data{3:end,1},'T'));
So if you're getting past that you must have fixed the file somehow. Can you attach your actual m-file?
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
Sofea Irene
il 11 Ott 2022
William Rose
il 11 Ott 2022
@Sofea Irene, you are welcome. Good luck with your work.
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Biological and Health Sciences in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!