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

8 visualizzazioni (ultimi 30 giorni)
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 Commenti
Bertil Bwira
Bertil Bwira il 28 Mag 2019
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?
Bertil Bwira
Bertil Bwira il 28 Mag 2019
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

Accedi per commentare.

Risposta accettata

Rik
Rik il 28 Mag 2019
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 Commenti
Bertil Bwira
Bertil Bwira il 28 Mag 2019
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
Rik
Rik il 28 Mag 2019
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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Structures in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by