Error using plot Vectors must be the same length.

clear, clc, close all
DataStruct_IMU = importdata("IMU_Data.tsv");
Time_IMU = DataStruct_IMU.data(:,1);
Omega_IMU = DataStruct_IMU.data(:,2);
h = mean(diff(Time_IMU));
n = length(Omega_IMU);
Aplha_IMU(1)=(-3*Omega_IMU(1) + 4*Omega_IMU(2) - Omega_IMU(3))/(2*h);
for i = 2:n-1
Alpha_IMU(i) = (Omega_IMU(i+1)-Omega_IMU(i-1))/(2*h);
end
Aplha_IMU(n) = (Omega_IMU(n-2)-4*Omega_IMU(n-2) + 3*Omega_IMU(n))/(2*h);
figure('units','normalized','outerposition',[0 0 1 1])
title({'CVEN 308 Module 3','Numerical integration and differentiation of pendulum motion'})
subplot(1,1,1)
hold on
plot(Time_IMU,Alpha_IMU,'b')
I got following error
Error using plot
Vectors must be the same length.
Error in Copy_2_of_Data_Analysis_Starter (line 23)
plot(Time_IMU,Alpha_IMU,'b')

댓글 수: 5

Can you upload the data file? (You can use the paper clip icon in the INSERT section of the toolbar.) That will make things much easier to debug.
clear, clc, close all
DataStruct_Vid = importdata("Video_Data.tsv");
Error using importdata
Unable to open file.
Time_Vid = DataStruct_Vid.data(:,1);
Theta_Vid = DataStruct_Vid.data(:,2);
DataStruct_IMU = importdata("IMU_Data.tsv");
Time_IMU = DataStruct_IMU.data(:,1);
Omega_IMU = DataStruct_IMU.data(:,2);
h = mean(diff(Time_IMU));
n = length(Omega_IMU);
Aplha_IMU(1)=(-3*Omega_IMU(1) + 4*Omega_IMU(2) - Omega_IMU(3))/(2*h);
for i = 2:n-1
Alpha_IMU(i) = (Omega_IMU(i+1)-Omega_IMU(i-1))/(2*h);
end
Aplha_IMU(n) = (Omega_IMU(n-2)-4*Omega_IMU(n-2) + 3*Omega_IMU(n))/(2*h);
figure('units','normalized','outerposition',[0 0 1 1])
subplot(1,1,1)
hold on
plot(Time_IMU,numel(Alpha_IMU)-1,'b')
I Cannot able to create graph I dunno where I making mistake.
Thanks
Voss
Voss 2023년 9월 24일
Change "Aplha_IMU" to "Alpha_IMU" in the two places I mentioned in my answer.
Thanks you so much.
Voss
Voss 2023년 9월 24일
You're welcome! So is the problem solved now?

댓글을 달려면 로그인하십시오.

 채택된 답변

Voss
Voss 2023년 9월 24일

0 개 추천

You assign to

Aplha_IMU(n)

but it should be

Alpha_IMU(n)

(Similarly for Aplha_IMU(1), but that's not the cause of the error.)

댓글 수: 4

Thanks
Voss
Voss 2023년 9월 24일
You're welcome! Any questions, let me know. Otherwise, please Accept this answer. Thanks!
Voss
Voss 2023년 9월 25일
@NANDEESWARAN: Did this fix the problem?
An explanation: Those typos have the effect of creating two vectors: Aplha_IMU (the erroneous one), which is of length n, and Alpha_IMU (the one you wanted to plot with), which is of length n-1 (because its nth element never gets assigned).
See below:
n = 5;
Aplha_IMU(1) = 1;
for i = 2:n-1
Alpha_IMU(i) = i;
end
Aplha_IMU(n) = n;
% the one you wanted to create has n-1 elements.
% notice its first element is zero becuase it was never explicitly
% assigned.
Alpha_IMU
Alpha_IMU = 1×4
0 2 3 4
% the erroneously created vector has n elements.
% but it's all zeros except for the first and last element because only its
% first and last elements were explicitly assigned.
Aplha_IMU
Aplha_IMU = 1×5
1 0 0 0 5
So then when you try to plot Alpha_IMU against Time_IMU, you get the error you got because Alpha_IMU has n-1 elements while Time_IMU has n elements.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

질문:

2023년 9월 24일

댓글:

2023년 9월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by