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

MATLAB question

  1. May 27, 2008 #1
    I have been writing a program to compute and change a value a great number of times and writes each value into a file

    I did this by continuously storing each value (which is a 7 element vector) into an array, and then writing that into a file using the cvswrite function

    The problem I am having is that the array that the array eventually grows to the size that MATLAB gives me an "out of memory" error

    so, to fix this, I have been trying to write each value into the .txt file, closing the file, computing the next vector into the same vector that it was computed from (to avoid the memory error), and writing it to the same file, but I can't seem to get it to work

    I just end up overwriting the file each time

    Is there anyway to get the next vector to print to the next line of the text file, as opposed to just overwriting the initial value in the text file?

    any ideas?
    Thank you.
  2. jcsd
  3. May 27, 2008 #2
    You can open the file with fopen(filename,'a+') and after that fprintf to write to the end of the file.
  4. May 27, 2008 #3
    thanks for the advice

    ok, so I've been trying to do something like this

    x = [1 2 3 4 5 6]
    y = [6 5 4 3 2 1]
    fid = fopen('exp.txt', 'wt');
    fprintf(fid, '%s\n', y);

    but again, each time I run the code, the value just gets overwritten
  5. May 27, 2008 #4
    If you're running this particular block of code repeatedly to generate your file then of course the data will be rewritten. At the end you're closing the file, but when you loop around again you're opening the file and rewriting its contents.

    A simple example might help. Suppose that I want to create a file with a list of numbers in it; for the sake of argument, suppose I want to use Matlab to create a file "test.txt" that contains integers from one to ten. An easy way to do it is to use a for loop:

    Code (Text):
    % First, open the file 'test.txt' for writing.

    fid = fopen('test.txt', 'wt');

    % Now comes the for loop. At each step of the loop we assign a value to x
    % and write this value to the file. The loop starts at x = 1 and continues
    % to x = 10

    for i=1:10
        x = i;
        fprintf(fid, '%6.2f\n', x);

    % Now that the loop is finished and we have written the data to the file,
    % close the file.

    The important point to note is that you should close the file only once you've finished adding data to it, not after you add each data point. You should easily be able to modify the above example to work with your array.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: MATLAB question
  1. MATLAB questions (Replies: 0)

  2. MATLAB question (Replies: 2)

  3. MATLAB question (Replies: 1)

  4. Matlab question (Replies: 2)

  5. Matlab question (Replies: 10)