How to code an fprintf if I am printing a value

In summary, the fprintf function in C can be used to print floating point values on a single line. The appropriate string format for data must be entered into the function before the data.
  • #1
Juanka
40
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
  • #2
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));
 
  • #3
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.
 
  • #4
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)
 
  • #5
Just use fprintf with the title in a string.
 
  • #6
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!
 
  • #7
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.
 

1. How do I code an fprintf statement to print a value?

To print a value using fprintf, you first need to specify the format in which you want the value to be printed. This is done using a format specifier, such as %d for integers or %f for floating point numbers. Then, you need to pass the value you want to print as an argument after the format specifier. For example, fprintf(file, "%d", num) will print the integer value stored in the variable "num" to the file specified by "file".

2. Can I print multiple values with a single fprintf statement?

Yes, you can print multiple values by including multiple format specifiers in the fprintf statement, separated by commas. For example, fprintf(file, "%d %f", num, num2) will print the integer value stored in "num" and the floating point value stored in "num2" to the file specified by "file".

3. How do I print a string using fprintf?

To print a string using fprintf, you can use the %s format specifier. This will print the string value passed as an argument after the format specifier. For example, fprintf(file, "%s", str) will print the string value stored in the variable "str" to the file specified by "file".

4. Can I specify the width and precision of the printed value?

Yes, you can specify the width and precision of a printed value by adding a number before the format specifier. For example, %5d will print an integer with a minimum width of 5 characters, adding leading spaces if necessary. Similarly, %.2f will print a floating point number with a precision of 2 decimal places. You can also combine these modifiers, such as %10.2f, to specify both the width and precision of the printed value.

5. How do I print a value to the screen instead of a file?

To print a value to the screen, you can use the fprintf function without specifying a file. Instead, you can use the predefined file pointer "stdout" to print to the standard output stream, which is typically the screen. For example, fprintf(stdout, "%d", num) will print the integer value stored in "num" to the screen.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
826
  • Programming and Computer Science
Replies
4
Views
744
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
9K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
6K
  • Programming and Computer Science
Replies
1
Views
691
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
987
  • Programming and Computer Science
Replies
4
Views
385
Back
Top