Save with identical file- and variable name

조회 수: 18 (최근 30일)
Aagje E
Aagje E 2018년 6월 19일
댓글: Aagje E 2018년 6월 20일
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
Stephen23
Stephen23 2018년 6월 19일
편집: Stephen23 2018년 6월 19일
"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 2018년 6월 19일
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 ;)

댓글을 달려면 로그인하십시오.

채택된 답변

OCDER
OCDER 2018년 6월 19일
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
Aagje E
Aagje E 2018년 6월 20일
Thank you! So easy to implement and short :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by