How can I save ALL workspace variables?
21 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello! I have 7104 .mat files, each of them has different variables inside. I was asked to transfer these matlab files to .cdf or .txt files. Is it possible to do it automatically? My best guess is to open all of them one by one in a loop and save the variables into new files. But how can I save ALL workspace variables? They all have different names from file to file...
0 Kommentare
Antworten (3)
Stephen23
am 16 Feb. 2018
Bearbeitet: Stephen23
am 17 Feb. 2018
The simplest answer is don't.
S = load(...)
You can then easily loop over the fieldnames(S) or convert to cell array using struct2cell(S), making saving quite easy, whatever file format you choose. You will probably find it useful to read about dynamic fieldnames, and to keep in mind that not all data classes can be (easily) represented in a text file.
Edit: "Because my advisor has no MATLAB, but she needs to see the data..."
It is not required to convert the files to text: the .mat file is an openly defined standard, and there are plenty free applications that can read them. Some of the better ones will read .mat files, correctly handle ND arrays, and make it easy to view them.
0 Kommentare
Steven Lord
am 16 Feb. 2018
Don't load the MAT-files directly into the workspace in which you're trying to write them to a different file. Doing so may overwrite variables that already have that name in the workspace.
If you must do this, call load with an output argument. That output argument will become a struct array with one field per variable in the MAT-file. Iterate over the fieldnames of that struct and use dynamic field names to retrieve each variable's field from the struct then write the retrieved data to the file.
Before you begin, you probably want to determine how to handle cases that may not be so straightforward to represent in a flat text file like an N-dimensional array such as rand(2, 3, 4), objects, anonymous functions, etc.
But I am a bit curious: why were you asked to make .cdf or .txt files containing the same data as the MAT-files?
Guillaume
am 16 Feb. 2018
Loading the mat files efficiently so that you know what the variables within the files are is very easy if you follow the advice we keep on giving on this forum, which is to never use load without an output value. So:
matfiles = {'aaaa.mat', 'bbbb.mat', ...} %cell array of files to load. possibly obtained with dir
for fileidx = 1:numel(matfiles)
matcontent = load(matfiles{fileidx});
%save matcontent
end
For each file, matcontent will be a structure with each field one of the mat file variable. Therefore identifying the variables is easy.
As for saving them to a text file or cdf file that could be much harder depending on the type of these variables. How do you encode a 4-d array in a text file? Or a structure? Or a function handle? Or a graphics object? etc...
2 Kommentare
Guillaume
am 17 Feb. 2018
The process of saving variables so that they can be read by another (or same program) is called serialisation. Reading these is deserialisation. For it to work, both the writer and the reader need to agree on a common format. Therefore, I think your first step is to go back to your advisor and agree in more details what this common format is beside text / cdf.
What also needs to be established is what kind of matlab variables are to be supported for serialisation. Some types can be downright difficult to near impossible to serialise (e.g. most handles).
Matlab only offers one way to serialise its variables, it's the mat format. So as Stephen says, perhaps the best approach is to find a mat file viewer.
Siehe auch
Kategorien
Mehr zu Data Import and Analysis finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!