Function with mat-file and return struct array

1 次查看(过去 30 天)
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 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by