Save with identical file- and variable name

20 vues (au cours des 30 derniers jours)
Aagje E
Aagje E le 19 Juin 2018
Commenté : Aagje E le 20 Juin 2018
I am saving data in Matlab to open it in a python script. For some reason this requires that the name of the variable and the filename are the same. For example, this works (saving as version 7.3 makes it possible to import in python without losing the structure):
data = data.(filename{1,1})
save('data', 'data', '-v7.3')
but this only allows me to save with the filename 'data' , which I can also not change afterwards, because then python won't read the file anymore.
I know I could use this method to save each file manually with a name that I want, but there is quite a large number of files involved. So I would like to re-write it in a way that uses the filename stored in the array filename automatically. I have the feeling this should be possible, but I can't get it to work yet...
I have tried many things like this:
for i = 1:n
filename = (filename{1,i});
name = strcat(filename);
name = data.(filename{1,i})
save(name, 'name', '-v7.3')
end
But for some reason it seems not to be possible to save a file with a filename that is identical to the variable name. I hope my problem is clear, any ideas would be greatly appreciated!
  2 commentaires
Stephen23
Stephen23 le 19 Juin 2018
Modifié(e) : Stephen23 le 19 Juin 2018
"For some reason this requires that the name of the variable and the filename are the same"
I use numpy and scipy all the time at work, and its importing routines do NOT require the filename and the variable names to be the same (how would that even work for multiple variables?):
Whatever routine you are using is very very badly written if that really is the case, and you would be better off fixing the problem there, rather than trying to write hack MATLAB code to fix a problem in some Python code.
Note that dynamically accessing variables names is how beginners force themselves into writing slow, complex, buggy code. Read this to know why:
It is much more efficient to use indexing, fieldnames, or the columns/rows of a table.
Aagje E
Aagje E le 19 Juin 2018
I tried to fix the problem in python first by using scipy, the thing is that within the file I try to save as 'data', there are multiple variables (data/time, data/distance, etc.) and that this structure with the headers for each variable gets lost using scipy. That is why I prefer saving using '-v7.3', which saves the .mat in the same way as an hfd5, which is an easier file format. And normally with the h5py these are easy to read in python.
I am a beginner in Matlab, which is why I would rather have my data in python ;)

Connectez-vous pour commenter.

Réponse acceptée

OCDER
OCDER le 19 Juin 2018
Assuming your Data is a structure containing your variables, here's one way to make file names = variable names:
Data = struct('A', 1, 'B', 2, 'C', [1 2 3], 'D', [1 3 ; 3 4]); %Example structure of data
Fields = fieldnames(Data);
for f = 1:length(Fields)
save(Fields{f}, '-v7.3', '-struct', 'Data', Fields{f});
end
  1 commentaire
Aagje E
Aagje E le 20 Juin 2018
Thank you! So easy to implement and short :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange

Produits


Version

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by