How can i fprintf a horizontally concatenated array

5 views (last 30 days)
THIS IS MY ASSIGNMENT AND IVE DONE ALL OF IT BUT I CANT FIGURE OUT HOW TO FPRINTF THE RESULTS CORRECTLY:
You will develop an m-script file to convert between radians per second, revolutions per minute, and degrees per second. Your program should create a 1 x n vector consisting of random integers between 0 and 4000. These values will be assigned units of revolutions per minute. The 1 x n vector will be part of an n x 3 array where the first column is revolutions per minute (rpm); the second column is radians per second (rad/s); and, the third column is degrees per second (deg/s). The program should prompt the user for the value of n. Your program should include a for loop with a print statement that will print n lines as follows:
XXX rpm = YYY rad/s = ZZZ deg/s
XXX rpm = YYY rad/s = ZZZ deg/s
XXX rpm = YYY rad/s = ZZZ deg/s
(hint) – use the function call to print … …
fprintf(%d rpm = %d rad/s = %d deg/s\n’,A(i,1),A(i,2),A(i,3));
pause(0.01);
… …
where A is the 2D array mentioned above and i is the increment in the for loop.
MY CODE SO FAR:
n=input('n=')
A=randi(4000,1,n)
for i=1:n;
rpm=A;
radps=A*.102172;
degps=A*6;
end
B=rpm';
C=radps';
D=degps';
E=horzcat(B,C,D)
fprintf('%d rpm = %d rad/s = %d deg/s\n',E(:,1),E(:,2),E(:,3))
~You will see when the numbers are printed that they are in the wrong order. They keep reading off like a book from left to right and the numbers for rpm, rads/s, and deg/s are all supposed to be in separate columns. THANK YOU ALL!!

Answers (1)

Walter Roberson
Walter Roberson on 25 Jan 2016
fprintf('%d rpm = %d rad/s = %d deg/s\n', E.')
This is a "trick" with fprintf() and sprintf(): to print values by rows, you have to put the values together as columns in a matrix and then transpose the rows and columns so that all the values intended to print as one row are handed to fprintf() as a column instead.
This method of processing is consistent with how MATLAB handles memory, but it is certainly not something that is obvious to users.
  1 Comment
ash
ash on 25 Jan 2016
You rock! It feels good to know I was so close:) Thank you so much!

Sign in to comment.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!