Writing to file


BStewart

Recommended Posts

Hi,

I am having trouble writing the values of variables to a file. I can get it to write out numerical values - ie. File.WriteDelim(handle,{{1,2,3,4},{5,6,7,8}},",",chr(10)) produces

1,2,3,4

5,6,7,8

but I cant get it to write out the values of variables in a similar fashion? (ie multiples values to a line)

Please help,

Thanks,

Ben

Link to comment
Share on other sites

It is probably because your variables aren't in the dimension you think they are. Perhaps you can post a simple sample showing it now working? Try also doing a ? statement just before the file.writedelim() on your variable to see what the variable contains:

? MyVar

File.WriteDelim(handle,MyVar,",",chr(10))

Link to comment
Share on other sites

if I have variables i,j,k and l defined as followed:

i=1

j=2

k=3

l=4

how would I write the values of these variables (using the variable names) to a file so that the output in the file looks like:

1,2,3,4

I assign 'mydata' using i,j,k,l so that ?mydata gives {1,2,3,4} and

File.WriteDelim(handle,mydata,",",chr(10)) gives:

1

2

3

4

which isnt what I am after..

Link to comment
Share on other sites

You are in the wrong dimension. The first dimension is rows and is delimited by the end of line character. You need columns to get it to delimit by commas. So you want more like:

mydata[0][0] = i

mydata[0][1] = j

mydata[0][2] = k

mydata[0][3] = l

this will make mydata give {{1,2,3,4}} instead of {1,2,3,4} and thus output 1,2,3,4.

The reason for this is that you can create two dimensional arrays and output a whole file worth instead of one line:

mydata[0][0] = i

mydata[0][1] = j

mydata[0][2] = k

mydata[0][3] = l

mydata[1][0] = m

mydata[1][1] = n

mydata[1][2] = o

mydata[1][3] = p

Assuming m,n,o,p contain 5,6,7,8, you'd get an mydata == {{1,2,3,4},{5,6,7,8}} and writedelim would output:

1,2,3,4

5,6,7,8

Obviously it would be tedious to do it this way, but if your data is already in arrays (like channel history), you can do stuff real fast. For example:

mydata[0][0] = mychannel.time

mydata[0][1] = mychannel

mydata[0][2] = myotherchannel

File.WriteDelim(handle,mydata,",",chr(10))

This would essentially dump those two channels to disk in a comma delimited file.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.