MATLAB Answers

0

How to create a function which accepts a name of a mat-file and returns a structure array with fields’ Name and Size?

Asked by Bertil Bwira on 28 May 2019
Latest activity Commented on by Rik
on 28 May 2019
I don't know if I'm on the right way to solvning this. Here is the assignment:
" Create a function which accepts a name of a mat-file and returns a structure array with fields’ Name and Size. The number of elements in the structure array corresponds to the number of the variables in the mat-file. Name nameand Size fields contain information about name and size of variables. Prepare several mat-files to demonstrate the function’s performance. Tips: Use the built-in function size to obtain a variable’s size."
Here is my function:
function se=se(name)
filename = name;
filename = name;
myVars = {'x','y','caption'};
S = load(filename,myVars{:})
end
And here are my two vector saved as "ma":
x=magic(20)
y=magic(10)

  3 Comments

Note that it is not required to explicitly load the data into MATLAB, whos already gives those metrics for variables in a .mat file:
S = whos('-file',name)
And after that select the fields that you need. Avoid using command syntax:
So I don’t need my expression for S? I just write
whos -’file’ ’ma.mat’ but it gives me the char aswell, how do I explicitly choose to only show Name and Size of variables?
How does this look?
S = whos('-file','ma.mat');
for k = 1:length(S)
disp([' Name ' S(k).name...
' Size ' mat2str(S(k).size) ...
' Type ' S(k).class]);
end

Sign in to comment.

1 Answer

Answer by Rik
on 28 May 2019
 Accepted Answer

Lets go through your proposed code:
function se=se(name)%don't give your ouput the same name to your output as your function
filename = name;%no need to rename the variable, you can also just use this name in the function def
filename = name;%no need to repeat this statement
myVars = {'x','y','caption'};%you don't need to define yourself what variables to load
S = load(filename,myVars{:})%you're on the right track by loading into a struct
end
You can skip those first three lines. Then you have a struct with the contents of the mat file. So your next step is to find the name and size of each variable, which is now the name and size of each field in your struct.
You can use the fieldnames function and make use of the fact that you can do something like this:
a=struct;a.foo='bar';
field_name='foo';
disp( a.(field_name) )%prints 'bar' to the command window

  5 Comments

So?
S=load(fieldnames('ma.mat'));
%Fieldnames here, storing the result in a variable called fn
output=struct;
for n=1:numel(fn)
output(n).Name='Name'
sz='Size'
output(n).Size=sz;
end
I did this
S = whos('-file','ma.mat');
for k = 1:length(S)
disp([' Name ' S(k).name...
' Size ' mat2str(S(k).size) ...
' Type ' S(k).class]);
end
No, don't change the lines I wrote.
function output=se(filename)
%Load variable names and sizes to a struct
%
%This function reads a .mat file and then returns the names and sizes of
%the variables inside of that file. The output is a struct array with one
%element for each variable inside the .mat file.
%
%Syntax:
% output=se('ma.mat');
S=load(filename);
%use fieldnames here, storing the result in a variable called fn
% <--- edit here
output=struct;
for n=1:numel(fn)
output(n).Name=____% <--- edit here
sz=____% <--- edit here
output(n).Size=sz;
end
end
Don't hard-code the value for the Name field. You should get the names from the result of the fieldnames function. Also, the filename of the mat file you need to load is the input to your function, so you shouldn't be directly entering that either.

Sign in to comment.