How to save structure to file using variable file name in Octave?

In Octave, I would like to save the structure to a text file where the file name is determined at runtime. With my approach, I always get an error message:

expecting all arguments to be strings. 

(For a fixed file name, this works fine.) So, how do I save the structure to a file using a variable's file name?

 clear all; myStruct(1).resultA = 1; myStruct(1).resultB = 2; myStruct(2).resultA = 3; myStruct(2).resultB = 4; variableFilename = strftime ("result_%Y-%m-%d_%H-%M.mat", localtime(time())) save fixedFilename.mat myStruct; % this works and saves the struct in fixedFilename.mat save( "-text", variableFilename, myStruct); % this gives error: expecting all arguments to be strings 
+4
source share
1 answer

In Octave, when using the save function, you need to do something like this:

 myfilename = "stuff.txt"; mystruct = [ 1 2; 3 4] save("-text", myfilename, "mystruct"); 

The stuff.txt file will be created in the above code, and the matrix data will be placed there.

The above code will only work when mystruct is a matrix, if you have a row cell, it will fail. For them you can roll yourself:

  xKey = cell(2, 1); xKey{1} = "Make me a sandwich..."; xKey{2} = "OUT OF BABIES!"; outfile = fopen("something.txt", "a"); for i=1:rows(xKey), fprintf(outfile, "%s\n", xKey{i,1}); end fflush(outfile); fclose(outfile); 
+4
source

All Articles