Issues making the table and plot output properly
이전 댓글 표시
Question is: Write a script file that accepts each of the four vectors as inputs. The script file should plot the data on three different graphs in the same window. The function should also display the data in a table with the following form (including the header). In this problem you’re required to use fprintf to generate the table below.
Time (s) A1 (m/s2) A2 (m/s2) A3 (m/s2)
0 0 0 0
5 7 10 9
10 11 15 13
15 19 21 17
20 15 16 22
25 14 11 25
30 17 13 21
My code: t = input('Input the initial and final time(X is start Y is end), ex:[X:5:Y]');
a1 = input('Enter A1 data ex: [0 7 11 ...]');
a2 = input('Enter A2 data ex: [0 7 11 ...]');
a3 = input('Enter A3 data ex: [0 7 11 ...]');
plot(t,a1);
hold on;
plot(t,a2);
hold on;
plot(t,a3);
hold off;
legend('A1=a1','A2=a2','A3=a3');
fprintf('%6s %6s %6s %6s\n','Time(s)','A1(m/s2)','A2(m/s2)','A3(m/s2)');
fprintf('%.3f \t %.3f \t %.3f \t %.3f\n',t,a1,a2,a3);
I am getting an incorrect plot and table, all mismatched numbers. Debugging while I wait for an answer but I feel someone else will be able to recognize the error faster than I can.
Thanks in advance ;)
Edit: This is the output I get. I feel there is an issue with the way I had the information inputted, but I havent been able to figure out how to have it print properly:
Time(s) A1(m/s2) A2(m/s2) A3(m/s2)
0.000 5.000 10.000 15.000
20.000 25.000 30.000 0.000
7.000 11.000 19.000 15.000
14.000 17.000 0.000 10.000
15.000 21.000 16.000 11.000
13.000 0.000 9.000 13.000
17.000 22.000 25.000 21.000
답변 (1개)
Image Analyst
2018년 2월 3일
"three different graphs in the same window." could mean
subplot(2,2,1);
plot(.....
subplot(2,2,2);
plot(.....
subplot(2,2,3);
plot(.....
To get a table, you should use the uitable() function.
댓글 수: 8
Michael spillane
2018년 2월 3일
Image Analyst
2018년 2월 3일
Give a field width, like 10.3 or whatever.
fprintf('%10.3f \t %10.3f \t %10.3f \t %10.3f\n',t,a1,a2,a3);
Michael spillane
2018년 2월 4일
Image Analyst
2018년 2월 4일
The tabs are messing you up. Get rid of them. Try this:
% Time (s) A1 (m/s2) A2 (m/s2) A3 (m/s2)
A=[...
0 0 0 0
5 7 10 9
10 11 15 13
15 19 21 17
20 15 16 22
25 14 11 25
30 17 13 21]
% My code:
% t = input('Input the initial and final time(X is start Y is end), ex:[X:5:Y]');
% a1 = input('Enter A1 data ex: [0 7 11 ...]');
% a2 = input('Enter A2 data ex: [0 7 11 ...]');
% a3 = input('Enter A3 data ex: [0 7 11 ...]');
t = A(:, 1)
a1 = A(:, 2)
a2 = A(:, 3)
a3 = A(:, 4)
plot(t,a1);
hold on;
plot(t,a2);
hold on;
plot(t,a3);
hold off;
legend('A1=a1','A2=a2','A3=a3');
fprintf(' Time(s) A1(m/s2) A2(m/s2) A3(m/s2)\n');
fprintf('%11.3f %11.3f %11.3f %11.3f\n',t,a1,a2,a3);
Michael spillane
2018년 2월 4일
Michael spillane
2018년 2월 4일
fprintf('%11.3f %11.3f %11.3f %11.3f\n',[t,a1,a2,a3].');
Image Analyst
2018년 2월 4일
That's because my a1, etc. were column vectors and your were row vectors because of the different ways we created the vectors. (I was lazy and didn't want to type in all that stuff while I was developing the code for you.)
카테고리
도움말 센터 및 File Exchange에서 MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!