Saving data from 2D-array to a text file.

Hi,

I have done lots of googling but I have a 2D-array of type double and I want to save it to a file, so I can then plot the data using matplotlib in python, need help with saving it though please :D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <fstream>
#include <cmath>
#include <gsl/gsl_sf_legendre.h>
#include <fstream>
using namespace std;
#include "filters.h"



int main() 
{

	double thetaMin = 0.3, thetaMax = 0.6, theta;
	int thetaSize = 7, mode_in = 2, Nmodes = 3;
	double dTheta = (thetaMax - thetaMin)/thetaSize;
	double U[thetaSize][Nmodes];

	filters filter;
	
	//Set the thetaMax and thetaMin values being analysed 
	filter.setValues(thetaMin,thetaMax);

	//Loop over the Theta index
	for(int idxTheta = 0; idxTheta<thetaSize; idxTheta++)
	{

		//Loop through the theta bins
		theta = thetaMin + idxTheta*dTheta;

		//Loop of the modes
		for (int idxMode = 0; idxMode<4; idxMode++)
		{

			U[idxTheta][idxMode] = filter.U(idxMode,theta);
			cout << U[idxTheta][idxMode] << endl;
		


		}		

	}

	***Want to save U to a file here**




}
Last edited on
You can do this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <fstream>

int main()
{
    const int row = 5;
    const int col = 5;
    double x[row][col] = {0}; // set all values to 0, just for example.

    std::ofstream write;
    std::string fileName("data.dat");
    write.open(fileName.c_str());
    for(int i = 0; i < row; i++)
    {
        for(int j = 0; j < col; j++)
        {
            write << x[i][j] << " ";
        }
        write << std::endl;
    }
	write.close();
    return 0;
}


This will write to a text file with this format:
1
2
3
4
5
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
0 0 0 0 0 
@indiarose27,
It depends how you want your data filed for plotting. If you have only 4 modes then the file could be in columns something like
theta mode0 mode1 mode2 mode3
but with a lot of modes this would be impractical.

You are not consistent about the number of modes: it is 3 on line 15, but implied to be 4 on line 32. This will lead to access outside array bounds.

Do you actually need an array for U? Unless you have other plans for it, just work out a particular modal value on line 35 and write this columnar value to file (not cout) on line 36; you will need spaces or other delimiter between columns.

So a suggestion is:
- use an ofstream command to open your output file attached to a stream before any loops;
- output theta (but no linefeed) to that filestream after line 29
- for each U evaluated write immediately to file (instead of line 36)
- write a linefeed after the end of your idxMode loop.
Thankyou for this @fiji885 it worked perfectly! @lastchance for this particular case I will be using a small number of modes and U will need to be used in further calculations this plotting is more just a check/test of the code so far, but really useful suggestions for the future nonetheless so thank you for this :D
Topic archived. No new replies allowed.