Why do I received Error using Plot?

8 ビュー (過去 30 日間)
Sofea Irene
Sofea Irene 2022 年 9 月 17 日
コメント済み: William Rose 2022 年 10 月 11 日
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 件のコメント
William Rose
William Rose 2022 年 10 月 11 日
@Sofea Irene, you are welcome. Good luck with your work.

サインインしてコメントする。

採用された回答

William Rose
William Rose 2022 年 9 月 17 日
編集済み: William Rose 2022 年 9 月 17 日
@Sofea Irene, I have moved my comment to be an answer, as I originally intended, and I have added to it.
[edit: correct spelling errors]
Your 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.
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')
The command above plots four data sets. You can look in the list of variables 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). When I run the code, I do not get this error. I don't know why you get the error, and I do not. For me, data_long and R and I are all 201 elements long. For me, date and I_exp and R_exp are all 935 long.
I had to make several adjustments to sird_covid_run_ita.m, to get it to run. You got to figure 6 before you had an error, so you did better than me!
I got an error when the script tried to download the data file from the web. Therefore I downloaded the text file before running the script, and commented out the lines in the script that fetch the file online. Now my version of the script reads the text file from my hard drive. I am currently using Matlab 2016a, which does not support the split() command. I hope to have my regular computer back soon, with up-to-date Matlab. So I wrote a work-around for the missing split() command. After these adjustments, the script has an error on figure 3. The error was "index exceeds matrix dimensions". This occurs because the code was written when they had less than 200 days of data, so they stopped the simulation at 200 days. Now they have over 900 days of data, but the simulation still stops at 200 days. This causes problems with figures 3, 4, and 5. I fixed these errors. Then the plot command in figure 6, which caused an error for you, ran fine for me. I did get an error later in figure 6, with the xlim() command. I fixed it by commenting out that command.
In case you do not know: by "commenting out the command", I mean that I placed a % character at the start of the line, to turn the command into a comment.
Then figure 8 works, but there is an error on figure 7, which is plotted after figure 8. The error is
Error using sird_covid_run_ita (line 167) (my line numbers are different now)
Subscript indices must either be real positive integers or logicals.
This error happens because repr_vect_mean is an empty vector. It is empty because the reproduction coefficient is never greater than about 10^-10.
-----
The model is failing. Figures 3, 4, and 5 show zero infections for the first 200 days. (They only evaluate the model for the first 200 days.)
----------
@Sofea Irene, you picked a very complicated program to get started in Matlab.
Good luck.
  1 件のコメント
William Rose
William Rose 2022 年 9 月 17 日
I am ataching my version of the script. As I noted above, this version plots figures 1-6 and 8. Then it has an error on figure 7, for the reason I explained. To run my version of the code, you also need a local copy of the data file, which I am also attaching.
When I run my version of the script, figures 3 through 6 show zero infections and zero deaths for the 200 simulated days. This is unrealistic, and this is why I say the model is failing. Perhaps the model formulation cannot adapt to the existence of 935 days of data. The plot commands for figures 3-6 in the File Exchange version of the script indicate that the authors did not anticipate what would fail when the data extended beyond 200 days.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Find more on Biological and Health Sciences in Help Center and File Exchange

製品


リリース

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by