Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab diary function help

  1. Dec 1, 2011 #1
    I am using the diary function to store simulation results into a txt file, e.g.


    however I expect to have around 50,000 rows of data and so not all my data will be stored. Is there anything I can do to specify the size of the file? Am I right in thinking the file will only hold what is displayed in the Command Window? If so, is there a way to change this? Unless there is a different command I can use? Thanks if you can help!
  2. jcsd
  3. Dec 1, 2011 #2

    Dr Transport

    User Avatar
    Science Advisor
    Gold Member

    how about writing your data to a file, no size limits....
  4. Dec 1, 2011 #3
    Yes thanks, I've been trying to do this using fprintf.

    With the example here: http://www.mathworks.com/help/techdoc/ref/fprintf.html

    B = [8.8 7.7 ; ...
    8800 7700];
    fprintf('X is %4.2f meters or %8.3f mm\n', 9.9, 9900, B)
    MATLAB displays:

    X is 9.90 meters or 9900.000 mm
    X is 8.80 meters or 8800.000 mm
    X is 7.70 meters or 7700.000 mm

    What does the %4.2f and %8.3f do apart from rounding values to 2 and 3 decimal places respectively? i.e. what's the 4 and 8?
  5. Dec 2, 2011 #4


    User Avatar
    Gold Member

    The 4 and 8 specify the width of the printed output. So, for %8.3f, if you has 12.345, it would print:
    Code (Text):

    and if you had 0.1236 it would print:
    Code (Text):

    padding with whitespace so that the column width is 8.

    Try this code to see how that works:
    Code (Text):

    number = 12.3456789;
    [COLOR="Blue"]for[/COLOR] i=1:10;
        format = sprintf([COLOR="DarkOrchid"]'%%%i.3f\n'[/COLOR], i);
        fprintf(format, number);
    It should produce:
    Code (Text):

    Last edited: Dec 2, 2011
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook