Clear Filters
Clear Filters

Error using plot Vectors must be the same length.

2 views (last 30 days)
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 Comments
Dyuman Joshi
Dyuman Joshi on 16 Aug 2023
[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.
Govind Sankar Madhavan Pillai Ramachandran Nair
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.

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 16 Aug 2023
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

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!