MATLAB How to code an fprintf if I am printing a value

AI Thread Summary
To print a title line in MATLAB using fprintf, simply include the title as a string in the fprintf function. For example, use fprintf('Your Title Here\n'); before printing your data. If you want to format your data in a table, you can follow the title with a line of dashes for clarity, like fprintf('-------------------\n');. While MATLAB does not support bold or flashing text in the command line, creative use of ASCII art can enhance the presentation. Incorporating these formatting techniques can help organize output effectively.
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.
 

Similar threads

Replies
4
Views
2K
Replies
2
Views
10K
Replies
5
Views
7K
Replies
2
Views
2K
Replies
4
Views
1K
Replies
7
Views
7K
Back
Top