Creating a function that gives the size and name of the variables in the mat-file

2 views (last 30 days)
Hi, I am trying to write a function that gives the size and name of the variables in the mat-file
I have this:
S = whos('-file','workspace313.mat')
% lists in alphabetical order the names, sizes, and types of all variables in the currently active workspace.
for k = 1:length(S)
disp([S(k).name, mat2str(S(k).size)]
How do I go on?

Answers (1)

Image Analyst
Image Analyst on 9 May 2021
Try this:
d = dir('*.mat'); % Get a list of all .mat files in the current folder.
for k = 1 : length(d)
s = load(d(k).name) % Load it.
names = sort(fieldnames(s)); % Get fieldnames and sort them.
for k2 = 1 : length(names)
fprintf(' File "%s" has a field called %s.\n', d(k).name, names{k2});
end
end

Tags

Community Treasure Hunt

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

Start Hunting!