Error using plot Vectors must be the same length.

I am trying to plot this.
plot(time,cellvoltages).
Size of cellvoltages is 36004 x 84.
That is fixed. Now the time varies according what we give. So Time is found out using a series of lines of code.
time_sec = (1:len_dataset)*dt;
time = time_sec/3600;
len_dataset is 36004. That is fixed. dt is what we give. So if we give dt as something less than 1 for eg 0.5 then plot works perfectly. The size of time in that case is 1 x 501. But if we give dt more than 1 say 1.25 then its giving an error
Error using plot
Vectors must be the same length.
This size of time now is 1 x 28800.
What is the cause of this error. Can anyone help me. Thank you.

6 comentarios

Assuming dt is a scalar value, the size of variables time_sec and time are not dependent on the value of dt.
If len_dataset is 36004 and is fixed, then time_sec and time both must have the size 1x36004.
Can you attach your code and data? Use the paperclip button to do so.
I am sorry I cant attach the code. Its classified. But yeah dt is a scalar value but the size of time keeps varying which is what I dont understand. As I said the code for time is
time_sec = (1:len_dataset)*dt;
time = time_sec/3600;
How is the size of time changing with dt. len_dataset is always 36004. So 1:36004 is 1,2,3 4...36004 which means that will always have 36004 values. And then multiply each value by dt should still have 36004 values and then divide each value by 3600 should again have 36004 values. So how is the size changing from 501 of 0.5 to 28800 for 1.25. How is that happening. Even if it changes both 501 and 28800 are less than 36004. So why is 501 working and not 28800. None of this makes sense. Thats what I am trying to figure out.
Sorry my mistake. The thing is I am working on a matlab script someone else wrote. So I didnt go through the whole code. The time is being changed at a certain location using this code
[index_min, index_max] = find_time_index(time, TIME_MIN, TIME_MAX);
if index_min < 1
index_min = 1;
end
time = time(index_min:index_max);
Thats why time is less. But still dont know why the plot is giving error. Since both cases with size 501 and 28800 is less than size of cellvoltages 36004. So why only 501 is working and not 28800. Thank you.
Also I want to let you know that the code is using plothandles. I dont know what that is.
plothandles = plot(time, cellvoltages);
This is where exactly the error is.
[index_min, index_max] = find_time_index(time, TIME_MIN, TIME_MAX);
if index_min < 1
index_min = 1;
end
time = time(index_min:index_max);
This snippet of code does not make sense to me.
Maybe you can attach the part of the code that is relevant to the problem you are facing.
Without the working code, it is difficult to suggest anything.
Thank you, I found the solution. So the function was kind of changing the time array but it changes from Time_MIN to Time_Max. Time_Max was set to 10, so if you change dt to 1 or more than 1, then the time value goes above 10 and then causes some error. So I changed Time_Max to round(max(time)). This will take the maximum value in the array and round it off and set that to time max. This solved the problem. Thank you.

Iniciar sesión para comentar.

Respuestas (1)

You can create a vector in MATLAB using any three of the four pieces of data starting point, ending point, space between points, and number of points. In this case you're using the first three of those pieces of data but then the number of points may not be what you want. In that case I'd use the starting and ending points and the desired number of points instead
Define data
startpoint = 1;
endpoint = 10;
spacing = 0.5;
numpoints = 19;
Start, end, space
A1 = startpoint:spacing:endpoint
A1 = 1×19
1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000 5.5000 6.0000 6.5000 7.0000 7.5000 8.0000 8.5000 9.0000 9.5000 10.0000
Start, end, number
A2 = linspace(startpoint, endpoint, numpoints)
A2 = 1×19
1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000 5.5000 6.0000 6.5000 7.0000 7.5000 8.0000 8.5000 9.0000 9.5000 10.0000
Start, spacing, number
A3 = startpoint + spacing*(0:numpoints-1)
A3 = 1×19
1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000 5.5000 6.0000 6.5000 7.0000 7.5000 8.0000 8.5000 9.0000 9.5000 10.0000
End, spacing, number
A4 = -spacing*flip(0:numpoints-1) + endpoint % Work backwards
A4 = 1×19
1.0000 1.5000 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000 5.5000 6.0000 6.5000 7.0000 7.5000 8.0000 8.5000 9.0000 9.5000 10.0000
Check
isequal(A1, A2, A3, A4)
ans = logical
1

Categorías

Productos

Versión

R2022b

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by