Index exceeds the number of array elements, when trying to plot

I am trying to plot multiple plots on a common x-axis. The problem that I am facing is that the size of my data sets (data1, and data2) are different from each other. Also, the length of the data to be used for x-axis is same as data2, but not data1.
This is resulting into the following error:
Index exceeds the number of array elements (683898).
Error in Plot_code
y1 = y1(dum)
data1= Test180diffcap;
data2= Test192diffcap;
data4= timediffcap;
y1=data1{:,1};
y2=data2{:,1};
x=data4{:,1};
[x, dum] = sort(x);
y1 = y1(dum);
[x, dum2] = sort(x);
y2 = y2(dum2);
L(1)=plot(x,y1, 'k','LineWidth',1.5);
hold on
L(2)=plot(x,y2, 'b','LineWidth',1.5);
Any suggestion to resolve this error would be highly appreciated.

16 commentaires

Perhaps we could be of more help if you tell us what it is you are trying to do. Why do you sort x? Why does Test192diffcap have more values? How do the values in Test180diffcap and Test192diffcap relate to the rows in Timediffcap?
Asadullah Khalid
Asadullah Khalid le 30 Mar 2020
Modifié(e) : Asadullah Khalid le 30 Mar 2020
Sorting x can be termed as a redundant step, but it simply means that the values are arranged in increasing order. timediffcap simply consists of values ranging from 0.1 to the maximum length of Test192diffcap, incrementing by 0.1 every row.
Every row value of Test192diffcap is the actual set of values that were obtained corresponding to timediffcap rows, and that is why Test192diffcap and timediffcap have the same lengths. For example the first value of Test192diffcap is obtained at 0.1 value of timediffcap. Similarly, the last value of Test192diffcap is obtained at 79984.1 value of timediffcap.
Test180diffcap follows the same pattern (of following rows of timediffcap)as Test192diffcap, but the values in Test180diffcap end much before (at 68389.9 value of timediffcap).
What I am trying to plot here is: the Test192diffcap vs timediffcap data. On top of that, I want to plot Test180diffcap vs timediffcap data. The reason for doing this is to visualize the overall flow of values in Test192diffcap and Test180diffcap corresponding to timediffcap data, and also be able to show that Test180diffcap actually stops at an earlier timediffcap value than Test192diffcap
Asadullah Khalid
Asadullah Khalid le 30 Mar 2020
Modifié(e) : Asadullah Khalid le 30 Mar 2020
Following is a sample screenshot (from excel) of what I am trying to plot on MATLAB:
L(2)=plot(x(1:length(y2), y2, 'b', 'LineWidth', 1.5);
Asadullah Khalid
Asadullah Khalid le 30 Mar 2020
Modifié(e) : Asadullah Khalid le 30 Mar 2020
I updated L(2)=plot(x,y2, 'b','LineWidth',1.5) with L(2)=plot(x(1:length(y2), y2, 'b', 'LineWidth', 1.5), but I'm still getting the same error
data1= Test180diffcap;
data2= Test192diffcap;
data4= timediffcap;
y1=data1{:,1};
y2=data2{:,1};
x=data4{:,1};
[x1, dum] = sort(x1);
y1 = y1(dum);
ny2 = length(y2);
[x2, dum2] = sort(x(1:ny2));;
y2 = y2(dum2);
L(1)=plot(x1, y1, 'k','LineWidth',1.5);
hold on
L(2)=plot(x2, y2, 'b','LineWidth',1.5);
Asadullah Khalid
Asadullah Khalid le 30 Mar 2020
Modifié(e) : Asadullah Khalid le 30 Mar 2020
Updated the code but still getting the same error.
Did I miss where you attached your data? ?‍♂️ People can't try anything without it.
Asadullah Khalid
Asadullah Khalid le 30 Mar 2020
Modifié(e) : Asadullah Khalid le 30 Mar 2020
I was unable to attach because of the size limitation per comment/post. Please find attached Test180diffcap and Test190diffcap zipped data here
Please find attached zipped timediffcap data
Hello.
I have provided all the details (data and the code) but I still haven't received any answer. Can anyone guide me on this?
I have been quite busy lately :(
Hello, Can anyone help me in debugging the code mentioned in this post?
@Asadullah Khalid, in response to your flag,
"Moderators, Kindly delete this question. Did not help me in posting the question here. If I have the option to delete which I'm unaware of, please advise."
there's no guarantee that questions will get a working solution in this forum. Common barricades that get in the way of finding a solution are
  • Not enough information to identify or solve the problem
  • Idiosyncrasies that are very difficult to address remotely
  • Improper application of viable solutions or lack of cooperation to attempt the solutions
  • Lack of interest or background knownlege by volunteers who answer questions
By posting a question, you are asking mostly unpaid volunteers to give their time to address your question and help you. Comments and answers often help countless individuals looking for similar solutions as evidenced by the 30-day view counts on threads that are years old. Deleting content is disrespectful to those who have invested their time to help you and the general community.
Please review
Original post by Asadullah Khalid retrieved from Google cache:
Index exceeds the number of array elements, when trying to plot
I am trying to plot multiple plots on a common x-axis. The problem that I am facing is that the size of my data sets (data1, and data2) are different from each other. Also, the length of the data to be used for x-axis is same as data2, but not data1.
This is resulting into the following error:
Index exceeds the number of array elements (683898).
Error in Plot_code
y1 = y1(dum)
data1= Test180diffcap;
data2= Test192diffcap;
data4= timediffcap;
y1=data1{:,1};
y2=data2{:,1};
x=data4{:,1};
[x, dum] = sort(x);
y1 = y1(dum);
[x, dum2] = sort(x);
y2 = y2(dum2);
L(1)=plot(x,y1, 'k','LineWidth',1.5);
hold on
L(2)=plot(x,y2, 'b','LineWidth',1.5);
Any suggestion to resolve this error would be highly appreciated.
(Answers Dev) Restored edit

Connectez-vous pour commenter.

Réponses (1)

Hiro Yoshino
Hiro Yoshino le 30 Mar 2020
Why not reampling the data to have the same length?
If you have Signal Processing toolbox, then just follow the instruction:
Also interpolation might work as well (available from MATLAB itself):

1 commentaire

I think that I will lose the originality of the data, that I am trying to visualize if I perform sampling.
And I think the interpolation would just add a datapoint from the last values in Test192diffcap and Test180diffcap, but I am actually trying to visualize the data as is.
Please correct me if I understood incorrectly.

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by