LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Saving matrix{i}{j}{k} to file

I have a 3D matrix ratio{i}{j}{k} I want to save to a file and later opet with Origin.

 

Tried using:

 

ArrayToFile ("C:\\deposit\\", ratio, VAL_FLOAT, (800)*(280)*(280), 1, VAL_GROUPS_TOGETHER,
VAL_GROUPS_AS_COLUMNS, VAL_CONST_WIDTH, 10, VAL_BINARY, VAL_TRUNCATE);

 

but I am getting that the type of argument does not match type specified.

 

I realize its a large file, but there is no other way to do it.

0 Kudos
Message 1 of 6
(3,241 Views)

HI,

 

I guess you specified a double array in the parameters of the "ArrayToFile" function.

But in fact you have a 3D array, that's why you have this error.

 

As you have a large file, you should try to separetely save your data into your file.

 

Regards,

 

 

Celine
National Instruments France

Message 2 of 6
(3,223 Views)

Since you are saving in binary format, I was wondering if something like that could be of help:

 

WRITING FUNCTION

#include <ansi_c.h>

// Define variables
static int		i;
static double	m[3][3][3];
static FILE	*fH = NULL;

// Fill in some value
for (i = 0; i < 3; i++) {
	m[0][0][i] = i;
	m[0][1][i] = i + 3;
	m[0][2][i] = i + 6;
	m[1][0][i] = 10 + i;
	m[1][1][i] = 10 + i + 3;
	m[1][2][i] = 10 + i + 6;
}

// Save to file
fH = fopen ("c:\\Temp\\tmp.dat", "wb");
if (errno) {
   // Add proper error handling code
}

fwrite ((char *)m, sizeof(double) * 9, 3, fH);
if (errno) {
   // Add proper error handling code
}

if (fH) fclose (fH);

 

READING FUNCTION

#include <ansi_c.h>

// Define variables
static double	m[3][3][3];
static FILE	*fH = NULL;

// Read data from disk
fH = fopen ("c:\\Temp\\tmp.dat", "rb");
if (errno) {
   // Add proper error handling code
}

fread ((char *)m, sizeof(double) * 9, 3, fH);
if (errno) {
   // Add proper error handling code
}

// Close file
if (fH) fclose (fH);

 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 3 of 6
(3,215 Views)

First, thank you for your replies.

 

I can save in ASCII, but I assume binary is faster.

 

So, the code should look something like this?

 

fH = fopen ("C:\\deposit\\tmp.dat", "wb");
fwrite ((char *)ratio, sizeof(double) * 280*280*800, 3, fH);
if (fH) fclose (fH);

 

Since this is inside a loop creating multiple "ratio" files, it writes inside the same .dat file?

0 Kudos
Message 4 of 6
(3,208 Views)

Look at options for fopen: "wb" rewrites old content, while "wb+" appends data to the file. You must be sure how to read them back in this case: this way of reading/writing has no control over data structure and if you use different indexes you read back wrong data.

 

Also, following my example you should use fwrite ((char *)ratio, sizeof(double) * 280*280, 800, fH); otherwise you are trying to write a 4-dimensions matrix (something like an array of 3 3-dimensions matrices).



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 5 of 6
(3,204 Views)

"wb" rewrites old content, while "wb+" appends data to the file

 

No, "ab" opens a binary in append mode (file pointer positioned at the end, ready to write more). "wb+" truncates, so it deletes the content of the file.

0 Kudos
Message 6 of 6
(3,175 Views)