saving an array

Hi,

I am running simulations and was using the following code to save several matrixes

name1="Data_"$+ftos((n),"%*.*lf",1,0)$+"_"$+ftos((mc),"%*.*lf",1,0)$+"_"$+ftos((spec),"%*.*lf",1,0);
saves1=saved(matrixA,name1,0);

where n, mc, and pec are running indexes for my simulation. matrixA is a matrix which holds the results I am interested in.

Now, I would like to save arrays in a similar way, but I cannot figure out how. An array, say arrayA holds the results. I would like to do this:

name1="Data_"$+ftos((n),"%*.*lf",1,0)$+"_"$+ftos((mc),"%*.*lf",1,0)$+"_"$+ftos((spec),"%*.*lf",1,0);
saves1=saved(arrayA,name1,0);

Could you tell me how I could do this?

1 Answer



0



Use ntos to create the name string

I'll get to the main points of your question next, but I thought you might want to know that you can use ntos instead of ftos and it will be much shorter. ntos will convert a number to a string.

Your name creation code with ntos would look like this:

name1="Data_"$+ntos(n)$+"_"$+ntos(mc)$+"_"$+ntos(spec);

Options for saving arrays in GAUSS

Save arrays to .fmt files

You can save arrays to .fmt files using the save function in GAUSS. Here are some examples:

// Create example 2x3x4 array
a = areshape(rndn(24,1), 2|3|4); 

// Save the array to the file 'a.fmt'
save a;

// Save the array to 'my_array.fmt'
// By default 'save' uses the literal value of
// the file name which is my_array in this case
save my_array = a;

// Set 'name1' equal to Data_22_14_5
n = 22;mc = 14;spec=5;
name1="Data_"$+ntos(n)$+"_"$+ntos(mc)$+"_"$+ntos(spec);

// The caret symbol, ^, tells GAUSS to use
// the value the string is holding instead of
// the literal value.
save ^name1 = a;

After running the above code, you should have three files in your current working directory: a.fmt, my_array.fmt and Data_22_14_5.fmt. They will all have the same contents.

You can load these files like this:

// Load a.fmt into a variable named 'a'
load a;

// Load my_array.fmt into a variable named 'my_array'
load my_array;

// Set 'name1' equal to Data_22_14_5
n = 22;mc = 14;spec=5;
name1="Data_"$+ntos(n)$+"_"$+ntos(mc)$+"_"$+ntos(spec);

// Load Data_22_14_5.fmt into a variable named 'a2'
load a2 = ^name1;

Save arrays to GAUSS Data Archives

You can also save multiple matrices, arrays, strings, etc to a GAUSS Data Archive GDA. Here is an example:

fname = "mydata.gda";

// Create 'mydata.gda'. If a file with that name
// already exists, overwrite it.
ret = gdaCreate(fname, 1);

// Create an example matrix
x = rndn(20,4);

// Create an example 2x3x4 array
a = areshape(rndn(24,1), 2|3|4);

vname = "Data_" $+ ntos(1);

// Save the contents of 'x' to a variable named 'Data_1' to 'mydata.gda'
ret = gdaWrite(fname, x, vname);

vname = "Data_" $+ ntos(2);

// Save the contents of 'a' to a variable named 'Data_2' to 'mydata.gda'
ret = gdaWrite(fname, a, vname);

You can then load the data like this:

vname = "Data_" $+ ntos(1);

// Load array named 'Data_1' into a GAUSS variable named 'xx'
xx = gdaRead("mydata.gda", vname);

vname = "Data_" $+ ntos(2);

// Load array named 'Data_2' into a GAUSS variable named 'aa'
aa = gdaRead("mydata.gda", vname);

aptech

1,773

Your Answer

1 Answer

0

Use ntos to create the name string

I'll get to the main points of your question next, but I thought you might want to know that you can use ntos instead of ftos and it will be much shorter. ntos will convert a number to a string.

Your name creation code with ntos would look like this:

name1="Data_"$+ntos(n)$+"_"$+ntos(mc)$+"_"$+ntos(spec);

Options for saving arrays in GAUSS

Save arrays to .fmt files

You can save arrays to .fmt files using the save function in GAUSS. Here are some examples:

// Create example 2x3x4 array
a = areshape(rndn(24,1), 2|3|4); 

// Save the array to the file 'a.fmt'
save a;

// Save the array to 'my_array.fmt'
// By default 'save' uses the literal value of
// the file name which is my_array in this case
save my_array = a;

// Set 'name1' equal to Data_22_14_5
n = 22;mc = 14;spec=5;
name1="Data_"$+ntos(n)$+"_"$+ntos(mc)$+"_"$+ntos(spec);

// The caret symbol, ^, tells GAUSS to use
// the value the string is holding instead of
// the literal value.
save ^name1 = a;

After running the above code, you should have three files in your current working directory: a.fmt, my_array.fmt and Data_22_14_5.fmt. They will all have the same contents.

You can load these files like this:

// Load a.fmt into a variable named 'a'
load a;

// Load my_array.fmt into a variable named 'my_array'
load my_array;

// Set 'name1' equal to Data_22_14_5
n = 22;mc = 14;spec=5;
name1="Data_"$+ntos(n)$+"_"$+ntos(mc)$+"_"$+ntos(spec);

// Load Data_22_14_5.fmt into a variable named 'a2'
load a2 = ^name1;

Save arrays to GAUSS Data Archives

You can also save multiple matrices, arrays, strings, etc to a GAUSS Data Archive GDA. Here is an example:

fname = "mydata.gda";

// Create 'mydata.gda'. If a file with that name
// already exists, overwrite it.
ret = gdaCreate(fname, 1);

// Create an example matrix
x = rndn(20,4);

// Create an example 2x3x4 array
a = areshape(rndn(24,1), 2|3|4);

vname = "Data_" $+ ntos(1);

// Save the contents of 'x' to a variable named 'Data_1' to 'mydata.gda'
ret = gdaWrite(fname, x, vname);

vname = "Data_" $+ ntos(2);

// Save the contents of 'a' to a variable named 'Data_2' to 'mydata.gda'
ret = gdaWrite(fname, a, vname);

You can then load the data like this:

vname = "Data_" $+ ntos(1);

// Load array named 'Data_1' into a GAUSS variable named 'xx'
xx = gdaRead("mydata.gda", vname);

vname = "Data_" $+ ntos(2);

// Load array named 'Data_2' into a GAUSS variable named 'aa'
aa = gdaRead("mydata.gda", vname);


You must login to post answers.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.