MATLAB Answers

B_K
0

How to name a set of numbers coming out of a loop for fprint

Asked by B_K
on 10 Nov 2018 at 20:21
Latest activity Edited by Walter Roberson
on 10 Nov 2018 at 21:28
I'm trying to have my loop display each variation up to the certain row I picked.
I can not figure out how to display/fprint my loop with a variable name such as;
I'm trying to do this within only a few fprintf statments without adding more variables.
fileName= 'EXCELDATA';
Data=xlsread('EXCELDATA');
for i=2:6
time=(i+1);
Acceleration_Data=Data(:,time);
Max_Accel(i)=max(Acceleration_Data);
Min_Accel(i)=min(Acceleration_Data);
fprintf('Maximum Acceleration is %.2f.\n', Max_Accel(i));
fprintf('Minimum Acceleration is %.2f.\n\n', Min_Accel(i));
end
the fprintf output:
Maximum Acceleration is -41.10.
Minimum Acceleration is -90.60.
Maximum Acceleration is 9.30.
Minimum Acceleration is -11.10.
Maximum Acceleration is -0.10.
Minimum Acceleration is -10.70.
what I would like is:
Maximum Acceleration 'in the x' is 10.30.
Minimum Acceleration 'in the x' is -33.30.
Maximum Acceleration 'in the y' is 10.30.
Minimum Acceleration 'in the y' is -33.30.....

  0 Comments

Sign in to comment.

Products


Release

R2018b

1 Answer

Answer by Walter Roberson
on 10 Nov 2018 at 21:25
Edited by Walter Roberson
on 10 Nov 2018 at 21:28

Without a loop:
varnames = {'x', 'y', 'i', 'j', 'k'};
datarows = [varnames(:), num2cell(Max_Accel(:)), varnames(:), num2cell(Min_accel(:))] .';
fprintf('Maximum Acceleration ''in the %s'' is %.2f.\nMinimum Acceleration ''in the %s'' is %.2f.\n\n', datarows{:});
Now, you could get away without an extra variable for the variable names, if you are willing to repeat the content, but it is difficult to get away without the cell array datarows in any situation where you are mixing character and numeric data as parameters.

  0 Comments

Sign in to comment.