Function with mat-file and return struct array

조회 수: 5 (최근 30일)
Bertil Bwira
Bertil Bwira 2019년 5월 26일
답변: atharva 2023년 11월 16일
I have two questions about an assignment that I'm working on.
How do I specify that in my mat-files I only want the two first fields (Name and Size) in structure array? and not bytes, class sparse etc.
How do I make sure the number of elements in the structure array corresponds to the number of the variables in the mat-file?
This is what I've done, am I doing it right. It might be right before my eyes but I'm quite new to Matlab.
x = magic(20); % 20x20 array
y= magic(15);
z= magic(10);
save('A.mat','x'); %binary matfile
save('B.mat','y');
save('C.mat','z');
% whos('-.mat'); % info
e=whos('B.mat')
a=load('A.mat');
b=load('B.mat');
c=load('C.mat');
Asize = size(a.x);
Bsize = size(b.y);
Csize = size(c.z);
struct('Name',a,'Size',Asize);

답변 (1개)

atharva
atharva 2023년 11월 16일
Hey Bertil,
I understand that you only want the two first fields "Name and Size" in structure array and not bytes, class sparse etc.
You can try the following code which outputs 1 x 3 struct array with fields : Name and Size
x = magic(20); % 20x20 array
y = magic(15);
z = magic(10);
save('A.mat', 'x'); % binary matfile
save('B.mat', 'y');
save('C.mat', 'z');
a = load('A.mat');
b = load('B.mat');
c = load('C.mat');
Asize = size(a.x);
Bsize = size(b.y);
Csize = size(c.z);
% Create a structure array with 'Name' and 'Size' fields
dataStruct = struct('Name', {'A', 'B', 'C'}, 'Size', {Asize, Bsize, Csize});
% Display the structure array
disp(dataStruct);
I hope this helps!

카테고리

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