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

조회 수: 8 (최근 30일)
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
Bertil Bwira
Bertil Bwira 2019년 5월 28일
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 2019년 5월 28일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Rik
Rik 2019년 5월 28일
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
Bertil Bwira
Bertil Bwira 2019년 5월 28일
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 2019년 5월 28일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by