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
Click For Summary

Discussion Overview

The discussion centers around how to use the `fprintf` function in MATLAB to print a title line along with data values. Participants explore various formatting techniques for displaying titles and data in a structured manner.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant seeks guidance on how to print a title line using `fprintf` in MATLAB.
  • Another participant suggests using `fprintf` to print three floating point numbers on a single line, providing a specific format string.
  • A different participant mentions using a matrix to print values, indicating that the data can be formatted in a single command.
  • One participant shares existing code and expresses the desire to add a title line before the printed data.
  • Another participant advises simply using `fprintf` with the title as a string.
  • A later reply discusses the limitations of formatting options in MATLAB for making text stand out, suggesting ASCII art as an alternative for visual emphasis.
  • One participant shares a personal approach to formatting titles and mentions printing tables formatted for LaTeX.

Areas of Agreement / Disagreement

Participants generally agree on the use of `fprintf` for printing titles and data, but there are differing opinions on how to best format the title and whether visual enhancements are possible in MATLAB's command-line interface.

Contextual Notes

Some participants reference specific formatting strings and methods without detailing the underlying assumptions or dependencies on data structures, such as matrices. There is also mention of limitations in visual formatting options within the MATLAB environment.

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 ·
Replies
4
Views
2K
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
10K
  • · Replies 5 ·
Replies
5
Views
7K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 7 ·
Replies
7
Views
7K
  • · Replies 6 ·
Replies
6
Views
1K