Function with mat-file and return struct array

1 Ansicht (letzte 30 Tage)
Bertil Bwira
Bertil Bwira am 26 Mai 2019
Beantwortet: atharva am 16 Nov. 2023
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);

Antworten (1)

atharva
atharva am 16 Nov. 2023
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!

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by