In Octave I would like to save a struct to a textfile where the name of the file is decided during the runtime of the script. With my approach I always get an error:
expecting all arguments to be strings.
(For a fixed filename this works fine.) So how to save a struct to a file using a variable filename?
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
In Octave, When using save as a function you need to do something like this:
The above code will create a stuff.txt file and the matrix data is put in there.
The above code will only work when mystruct is a matrix, if you have a cell of strings, it will fail. For those, you can roll your own: