Lines Not Connecting In graph

조회 수: 5 (최근 30일)
SkyRider44
SkyRider44 2020년 4월 19일
댓글: Ameer Hamza 2020년 4월 19일
I have the following code but i am not able to get the graph to join the points together:
a few of the files are attached.
for i = 1:62
T=(i-1)*30;
FTA=load(['XYpoints_timeFromDeparture_',num2str(T),'.txt']);
FTA = fopen(['XYpoints_timeFromDeparture_',num2str(T),'.txt']);
Xa=fscanf(FTA,'%f %*f %*f %*f',Inf);
fclose(FTA);
FTA = fopen(['XYpoints_timeFromDeparture_',num2str(T),'.txt']);
Ya=fscanf(FTA,'%*f %f %*f %*f',Inf);
fclose(FTA);
FTA = fopen(['XYpoints_timeFromDeparture_',num2str(T),'.txt']);
Xb=fscanf(FTA,'%*f %*f %f %*f',Inf);
fclose(FTA);
FTA = fopen(['XYpoints_timeFromDeparture_',num2str(T),'.txt']);
Yb=fscanf(FTA,'%*f %*f %*f %f',Inf);
fclose(FTA);
Op=([Xa]-[Xb]);
Ad=([Ya]-[Yb]);
Angle = atand(Op./Ad);
A = (i*30);
TTxt =sprintf('For Time At %.0f: ', A);
disp(TTxt)
AvgAngle = mean(Angle)*-1;
AvgAngleDISP = sprintf("The Average Angle is: %.3f°",AvgAngle);
disp(AvgAngleDISP)
V_MPH = ((tand(AvgAngle)*10*cosd(30)-10*sind(30))*2.237);
VT = sprintf("The Velocity is: %.3fmph",V_MPH);
disp(VT)
hold on
figure(3);
x = A/60;
y = V_MPH;
plot(x,y,'k-x')
xlabel('Time (Minutes)')
ylabel('Velocity (MPH)')
end
  댓글 수: 6
SkyRider44
SkyRider44 2020년 4월 19일
Each file contains 200+ rows and 4 columns of coordinates. 2 of the coloumns represent a raindrop at point x1,y1 and the other two columns represent a point x2,y2, i used the coordinates to find the angle of the line a in repspect to a verticaal line, from this i calulate the velocity and put this onto a graph on the y-axis and the time in minutes on the x-axis, however the points on the graph do not connect with a line.
SkyRider44
SkyRider44 2020년 4월 19일
Did you want the output like so? (attached image)

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

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 4월 19일
편집: Ameer Hamza 2020년 4월 19일
As you can see, x and y are 1x1, which indicates that for each file, you are just plotting a single point. If you want to draw a line, then you will need to move the plot() statement out of the for-loop and store the values of x and y in an array. Try the following code.
for i = 1:62
T=(i-1)*30;
FTA=load(['XYpoints_timeFromDeparture_',num2str(T),'.txt']);
FTA = fopen(['XYpoints_timeFromDeparture_',num2str(T),'.txt']);
Xa=fscanf(FTA,'%f %*f %*f %*f',Inf);
fclose(FTA);
FTA = fopen(['XYpoints_timeFromDeparture_',num2str(T),'.txt']);
Ya=fscanf(FTA,'%*f %f %*f %*f',Inf);
fclose(FTA);
FTA = fopen(['XYpoints_timeFromDeparture_',num2str(T),'.txt']);
Xb=fscanf(FTA,'%*f %*f %f %*f',Inf);
fclose(FTA);
FTA = fopen(['XYpoints_timeFromDeparture_',num2str(T),'.txt']);
Yb=fscanf(FTA,'%*f %*f %*f %f',Inf);
fclose(FTA);
Op=([Xa]-[Xb]);
Ad=([Ya]-[Yb]);
Angle = atand(Op./Ad);
A = (i*30);
TTxt =sprintf('For Time At %.0f: ', A);
disp(TTxt)
AvgAngle = mean(Angle)*-1;
AvgAngleDISP = sprintf("The Average Angle is: %.3f°",AvgAngle);
disp(AvgAngleDISP)
V_MPH = ((tand(AvgAngle)*10*cosd(30)-10*sind(30))*2.237);
VT = sprintf("The Velocity is: %.3fmph",V_MPH);
disp(VT)
x(i) = A/60;
y(i) = V_MPH;
end
figure;
plot(x,y,'k-x')
xlabel('Time (Minutes)')
ylabel('Velocity (MPH)')
  댓글 수: 2
SkyRider44
SkyRider44 2020년 4월 19일
Thank you very helpful!
Ameer Hamza
Ameer Hamza 2020년 4월 19일
Glad to be of help.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Read, Write, and Modify Image에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by