How to code an fprintf if I am printing a value

  • Context: MATLAB 
  • Thread starter Thread starter Juanka
  • Start date Start date
  • Tags Tags
    Code Printing Value
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
6 replies · 10K views
Juanka
Messages
37
Reaction score
0
ok I know how to code an fprintf if I am printing a value, but I want to make a "title" print for example.


Mass Time Error
fprintf values here. ... ... ....
fprintf values here... ... ...



How do I fprintf the line that has mass, time and error?
 
Physics news on Phys.org
Here's a link to the Matlab documentation for fprintf. I haven't used it in Matlab (don't have this software), but it looks about the same as the C standard library fprintf function.

If you want to print three floating point numbers to the screen on a single line, you can do it this way:
Code:
fprintf('%f %f %f\n', mass(i), time(i), error(i));
 
Mark44 is correct.

If you have the value in a matrix, I would do the following:
Code:
fprintf('%f %f %f\n', data');
where data is a matrix with columns mass, time, and error.

You'll want to read up on the appropriate string formats for your data.
 
I already have the data printed for example I have the following code, but I want to make a "title line"



Code:
%%%%I WANT a TITLE HERE%%%% FOR EXAMPLE%%%%

%fprintf(' seconds     kg       error')

fprintf('            Euler Step-10   %7.1f \t\t%.2f \t\t%.4f \n',Opti_Euler_step10,Mass_1,PE_Euler10)

fprintf('           Euler Step-500   %7.1f \t\t%.2f \t\t%.4f \n',Opti_Euler_step500,Mass_2,PE_Euler500)

fprintf('   Heun Step-10, & 1-ITER   %7.1f \t\t%.2f \t\t%.4f \n',Opti_Heun_step10,Mass_3,PE_Heun10_1)

fprintf(' Heun Step-10, & 100-ITER   %7.1f \t\t%.2f \t\t%.4f \n',Opti_Heun_step10_Iter100,Mass_7,PE_Heun10_100)

fprintf('  Heun Step-500, & 1-ITER   %7.1f \t\t%.2f \t\t%.4f \n',Opti_Heun_step500,Mass_4,PE_Heun500_1)

fprintf('Heun Step-500, & 100-ITER   %7.1f \t\t%.2f \t\t%.4f \n',Opti_Heun_step500_Iter100,Mass_8,PE_Heun500_100)

fprintf('      Runge-Kutta Step-10   %7.1f \t\t%.2f \t\t%.4f \n',Opti_RungeKutta_step10,Mass_5,PE_RK10)

fprintf('     Runge-Kutta Step-500   %7.1f \t\t%.2f \t\t%.4f \n',Opti_RungeKutta_step500,Mass_6,PE_RK500)

fprintf('            Golden Search   %7.1f \n',Opti_Golden)
 
Just use fprintf with the title in a string.
 
Are you trying to make your title stand out (e.g. bold or flashing font)? Unfortunately, you can't do this in the MATLAB command-line (the 'text' function only works on graphics). Nevertheless, the state-of-the-art of text-based gaming and ASCII art (along with standard underscores and dashes) is available to you:
http://en.wikipedia.org/wiki/ASCII_art
http://en.wikipedia.org/wiki/Star_Trek_(text_game)

Why yes, I did grow up in the 80s / 90s!
 
I'll usually do something like:
Code:
fprintf('Awesome Table Title\n');
fprintf('-------------------\n');
% print data here
Though half the time I write my m-files so that they print tables formatted for LaTeX.