Avoiding "Out of Memory" Error: Writing Values to Text File

In summary: Suppose that you have an array of 7 elements (x = [1,2,3,4,5,6,7,8,9], y = [9,8,7,6,5,4,3,2]); then the code would look like this:% First, open the file 'test.txt' for writing.% Then, create an array of 7 elements to store the values in.% This array will be used to store the values generated by the for loop.% The loop starts at x = 1 and continues to x = 10.% Note that the values in y are copied over to x before the for loop is run.% Finally, close the file
  • #1
smk037
68
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.
 
Physics news on Phys.org
  • #2
You can open the file with fopen(filename,'a+') and after that fprintf to write to the end of the file.
 
  • #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);
fclose(fid)

but again, each time I run the code, the value just gets overwritten
 
  • #4
smk037 said:
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);
fclose(fid)

but again, each time I run the code, the value just gets overwritten

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:
% 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);
end

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

fclose(fid)

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.
 

1. How do I prevent an "Out of Memory" error when writing values to a text file?

To avoid an "Out of Memory" error when writing values to a text file, you can increase the memory available to your program or use a more efficient method of writing to the file.

2. What is causing the "Out of Memory" error when writing values to a text file?

The "Out of Memory" error is typically caused by attempting to write a large amount of data to a file using a method that requires a lot of memory. This can also occur if your computer does not have enough available memory to handle the operation.

3. How can I check the available memory on my computer?

You can check the available memory on your computer by opening the Task Manager (Windows) or Activity Monitor (Mac) and viewing the memory usage. You can also use a command line tool such as "free" or "top" on Linux systems.

4. Is there a specific method I should use to write values to a text file in order to avoid "Out of Memory" errors?

Yes, there are more efficient methods for writing values to a text file that can help prevent "Out of Memory" errors. These include using buffered streams or writing the data in smaller batches instead of all at once.

5. Are there any other measures I should take to avoid "Out of Memory" errors when writing values to a text file?

In addition to using more efficient writing methods, you can also monitor your program's memory usage and adjust your code to handle large amounts of data more efficiently. It can also be helpful to regularly clean up and close any unused resources to free up memory.

Similar threads

  • Programming and Computer Science
Replies
21
Views
532
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
11K
  • Programming and Computer Science
2
Replies
57
Views
3K
  • Programming and Computer Science
Replies
4
Views
384
  • Programming and Computer Science
Replies
9
Views
862
  • Programming and Computer Science
Replies
1
Views
279
  • Programming and Computer Science
2
Replies
65
Views
2K
  • Programming and Computer Science
2
Replies
41
Views
3K
  • Programming and Computer Science
Replies
16
Views
3K
Back
Top