Function with mat-file and return struct array

1 回表示 (過去 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 Center および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by