필터 지우기
필터 지우기

Saving a structure element as a .mat file

조회 수: 226 (최근 30일)
sai prasanna sai prasanna m s
sai prasanna sai prasanna m s 2022년 10월 31일
댓글: Stephen23 2022년 11월 1일
I have a set of structures 1.mat , 2.mat and so on.
Each structure has a field as below example:
1.mat.data has a 110 X 116 array
I want to copy 1.mat.data into a separate mat file
The same for the rest of the files.
How can I do it ?
  댓글 수: 2
Rik
Rik 2022년 10월 31일
Your question is a bit confusing. mat files contain variables and can be loaded to a struct. Is that what you mean? So you want to do this?
S=load('1.mat');
data=S.data;
save('new_1.mat',data)
Is that what you mean?
What have you tried already?
Matt J
Matt J 2022년 10월 31일
I have a set of structures 1.mat , 2.mat and so on.
Not possible. Those are illegal names for structures, e.g.,
1.mat.data=5
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.

Error in connector.internal.fevalMatlab

Error in connector.internal.fevalJSON

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

답변 (1개)

Steven Lord
Steven Lord 2022년 10월 31일
If you want to extract some but not all of the variables stored in a MAT-file into a separate file, I'd use load to read just the variables you want to copy over into a struct array then call save with the -struct option. For this example let's work in a temporary directory.
cd(tempdir)
The patients.mat file has a number of variables.
whos -file patients.mat
Name Size Bytes Class Attributes Age 100x1 800 double Diastolic 100x1 800 double Gender 100x1 11412 cell Height 100x1 800 double LastName 100x1 11616 cell Location 100x1 14208 cell SelfAssessedHealthStatus 100x1 11540 cell Smoker 100x1 100 logical Systolic 100x1 800 double Weight 100x1 800 double
Let's make a new MAT-file named bloodPressure.mat (I'll add suffixes 1 and 2 to that name to distinguish two approaches) that contains only the Diastolic and Systolic variables from patients.mat. First load those two variables into a struct.
data = load('patients.mat', 'Diastolic', 'Systolic')
data = struct with fields:
Diastolic: [100×1 double] Systolic: [100×1 double]
Now call save to save that data to a new MAT-file. We could save the data struct array or we could use the -struct option to save each field as a separate variable in the MAT-file.
save('bloodPressure1.mat', 'data') % Storing the struct
whos -file bloodPressure1.mat
Name Size Bytes Class Attributes data 1x1 1936 struct
save('bloodPressure2.mat', '-struct', 'data') % Storing each field separately
whos -file bloodPressure2.mat
Name Size Bytes Class Attributes Diastolic 100x1 800 double Systolic 100x1 800 double
Note that in the case of bloodPressure2.mat the name data doesn't appear in the file anywhere. So if you wanted to do this to a series of files in a for loop you wouldn't have to use a different variable for each loop iteration. Just load the data into the temporary variable which will overwrite the contents from the previous loop iteration.
  댓글 수: 6
sai prasanna sai prasanna m s
sai prasanna sai prasanna m s 2022년 11월 1일
originalfile =
struct with fields:
data: [196×111 double]
This is exactly what I have. I have multiple such files. I am running the exact same code as above. But it is resulting in files of this format, and thats my issue:
newfile =
struct with fields:
data: [196×111 double]
I want it to be newfile=[196×111 double]
Stephen23
Stephen23 2022년 11월 1일
S = load(..)
newfile = S.data

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

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by