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 commentaire

The plot isnt the issue, it appears to be the way my input is formatted, as can be seen in the table on the bottom

Connectez-vous pour commenter.

Réponses (1)

"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 commentaires

What about the table? I see the correct data on it but it appears to be outputting from left to right then starting the next group of data.
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);
I have tried many different ways including the suggested but my data still keeps being output as:
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
Time being the 0,5,10,15,20,25,30. it appears it is putting the 7 datapoints in a row and continuing to the next row after outputting
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);
Same result, with the 0-30 added from l to r then the next group of data is output in the same way. I think there has to be something wrong with matlab if every possible solution (of my own) as well as a different format of input all give the same result.
Ended up just making M = [t a1 a2 a3] and then
fprintf('%11.3f %11.3f %11.3f %11.3f\n',M.');
Annoying how this cant be done without this extra step
Stephen23
Stephen23 le 4 Fév 2018
Modifié(e) : Stephen23 le 4 Fév 2018
fprintf('%11.3f %11.3f %11.3f %11.3f\n',[t,a1,a2,a3].');
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.)

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by