Hi there,
Is there any simple single-line function or call that can be used to write the contents of a matrix into a CSV?
Thank you!
2 Answers
0
accepted
If you need to save the headers then the following work-around will work for GAUSS 17+
headers = "A"$~"B"$~"C"$~"D";
data = reshape(seqa(1,1,100), 25, 4);
csvWriteData(data, headers, "test.csv");
proc (1) = csvWriteData(data, headers, fname);
local f;
f = fopen(fname, "w");
call fputs(f, strjoin(headers, ","));
call fputs(f, "\n");
call close(f);
// call csvWriteM with append flag set to 1 for numeric data
retp(csvWriteM(data, fname, ",", 15, 1));
endp;
A more convenient method to save headers to disk is on the roadmap for GAUSS 19.
0
Hi there,
I just found the csvWriteM command. It really helps! But I'm having some trouble writing the headers to the output .csv file.
My headers are: VAR1_D VAR2_F VAR3_S
When I try to save the headers in string array and pass it to csvWriteM like this:
heads = "VAR1_D" $| "VAR2_F" $| "VAR3_S";
ret = csvWriteM(heads', "out_data.csv",",", 15, 0, "\n");
I get the following error:
G0644 : Input 1 of wrong type [csv.src, line 658]
Currently active call:
File csv.src, line 658, in __csvWriteM
ret = fputs(f, strcombine(ntos(data, prec), d, 0));
Traceback:
File csv.src, line 606, in csvWriteM
retp(__csvWriteM(data, fname, sep, prec, append, newline));
And if I try passing the headers in a character array (which I know is deprecated) like this:
heads = {"VAR1_D" "VAR2_F" "VAR3_S"};
ret = csvWriteM(heads, "out_data.csv",",", 15, 0, "\n");
I don't get an error, but the .CSV file contains numbers instead of the textual headers:
3.71417011558708e-310,3.82281712898911e-310,4.52901815712696e-310,537164304713.021,537164304713.021
Is there any way GAUSS can handle writing the textual headers to the disk?
Thanks!
Your Answer
2 Answers
If you need to save the headers then the following work-around will work for GAUSS 17+
headers = "A"$~"B"$~"C"$~"D";
data = reshape(seqa(1,1,100), 25, 4);
csvWriteData(data, headers, "test.csv");
proc (1) = csvWriteData(data, headers, fname);
local f;
f = fopen(fname, "w");
call fputs(f, strjoin(headers, ","));
call fputs(f, "\n");
call close(f);
// call csvWriteM with append flag set to 1 for numeric data
retp(csvWriteM(data, fname, ",", 15, 1));
endp;
A more convenient method to save headers to disk is on the roadmap for GAUSS 19.
Hi there,
I just found the csvWriteM command. It really helps! But I'm having some trouble writing the headers to the output .csv file.
My headers are: VAR1_D VAR2_F VAR3_S
When I try to save the headers in string array and pass it to csvWriteM like this:
heads = "VAR1_D" $| "VAR2_F" $| "VAR3_S";
ret = csvWriteM(heads', "out_data.csv",",", 15, 0, "\n");
I get the following error:
G0644 : Input 1 of wrong type [csv.src, line 658]
Currently active call:
File csv.src, line 658, in __csvWriteM
ret = fputs(f, strcombine(ntos(data, prec), d, 0));
Traceback:
File csv.src, line 606, in csvWriteM
retp(__csvWriteM(data, fname, sep, prec, append, newline));
And if I try passing the headers in a character array (which I know is deprecated) like this:
heads = {"VAR1_D" "VAR2_F" "VAR3_S"};
ret = csvWriteM(heads, "out_data.csv",",", 15, 0, "\n");
I don't get an error, but the .CSV file contains numbers instead of the textual headers:
3.71417011558708e-310,3.82281712898911e-310,4.52901815712696e-310,537164304713.021,537164304713.021
Is there any way GAUSS can handle writing the textual headers to the disk?
Thanks!