Search code examples
matlabsimulationnumerical-methodsscientific-computing

How to write and read array data to a file in MATLAB when the scale is huge?


I am now simulating a parabola differential equation using a FDTD-like method. The simulation area is differenced and in order to keep the convergence of simulation, footstep of the mesh is very small so that the matrix scale is very huge, like 2100*100000000. So I cannot directly run simulation in this matrix.

I choose to store only 1 column of data in the program, at the end of the loop each time, I want to write the data to a file, appending the current column to the previous ones. After the simulation is finished, I will import this file into MATLAB and do some plotting like HeatMap.

Here is my sample code:

for n = 1 : z
for m = 2 : (x - 1)
    if ((mod(m, 54) < 15) && (mod(m, 54) > 0)) || ((mod(m, 54) < 42) && (mod(m, 54) > 27))
        delta_2_E = E(m + 1) - 2 * E(m) + E(m - 1);
        E(m) = alpha * delta_2_E + beta(m) * E(m);
    end;
end
for m = 1 : x
    if (mod(m, 54) == 0) || (mod(m, 54) == 1) || ((mod(m, 54) >= 15) && (mod(m, 54) == 28)) || ((mod(m, 54) >= 42) && (mod(m, 54) <= 53))
        E(m) = 0;
    end
end
intensity = abs(E);
fidresult = fopen('final_result.txt', 'w');
fprintf(fidresult,' %f\t %f\t %f\n',intensity);
fclose(fidresult);
end

Now here come the questions:

  1. If I use text file, it seems incapable of containing 2100 rows(like matrix in MATLAB). Besides its size will be extremely huge to open. Should I, and how to use other format like .mat file?
  2. Can I do HeatMap to the data matrix (Let's say it is successfully stored and read) in an acceptable time?
  3. If the things above actually is not working, is there any better way to simulate this?
  4. If possible, how can I preserve the accuracy of data? Down to approximately 1e-6.

Solution

  • You could write your data into a binary file (ch.mathworks.com/help/matlab/ref/fwrite.html). It is super fast to read (fread) and quit small data size compared to a txt file.

    You could also just use .mat files, which are in hdf5 format (depending on your matlab version). You can create .mat files by just using the save command