Assigning multiple variables to a structure

I have a collection of video clips, and for each frame of every video I calculate some stats (here, the mean R, G, and B values). I want to store this data for all the clips in a single struct, ideally by somehow assigning the variables into a struct that has the same field names. Is there a more succinct way of assigning the variables from each clip to a structure than the way I have coded it below?
Here is what I have so far:
% Initialize the struct.
clipData = struct('meanRedLevels',cell(1,numClips),'meanBlueLevels',...
cell(1,numClips),'meanGreenLevels',cell(1,numClips));
for clipNum = 1:length(videoList)
% Find movie get some basic stats.
movieFullFileName = [videoPath '/' videoList(clipNum).name ];
vidObj = VideoReader(movieFullFileName);
vidObjHeight = vidObj.Height;
vidObjWidth = vidObj.Width;
% Read in individual movie frames to a movie structure.
mov = struct('cdata',zeros(vidObjHeight,vidObjWidth,3,'uint8'),...
'colormap',[]);
k = 1;
while hasFrame(vidObj)
mov(k).cdata = readFrame(vidObj);
k = k+1;
end
numberOfFrames = size(mov, 2);
% Initialize variables.
meanRedLevels = zeros(numberOfFrames, 1);
meanGreenLevels = zeros(numberOfFrames, 1);
meanBlueLevels = zeros(numberOfFrames, 1);
for frame = 1 : numberOfFrames
% Extract the frame from the movie structure.
thisFrame = mov(frame).cdata;
% Calculate the mean R, G, and B levels.
meanRedLevels(frame) = mean(mean(thisFrame(:, :, 1)));
meanGreenLevels(frame) = mean(mean(thisFrame(:, :, 2)));
meanBlueLevels(frame) = mean(mean(thisFrame(:, :, 3)));
end
% Assign variables into struct with the same field names.
clipData(clipNum).meanRedLevels = meanRedLevels;
clipData(clipNum).meanBlueLevels = meanBlueLevels;
clipData(clipNum).meanGreenLevels = meanGreenLevels;
end

답변 (1개)

Piyush kant
Piyush kant 2019년 5월 29일
편집: Piyush kant 2019년 5월 29일

0 개 추천

If a structure is defined by:
details(1).name='a';
details(2).age='29';
details(2).gender='female';
for above example multiple fields to a Struct-Array can be assigned as:
details = struct(name,'a',age,'29',gender,'female')
and if you already have name, age and gender variables in your workspace following command will suffice.
details=struct('details',{'name','age','gender'});

댓글 수: 3

"and if you already have name, age and gender variables in your workspace following command will suffice details=struct('details',{'name','age','gender'});"
While that line will certainly generate a structure it is entirely unrelated to anything else that might exist in the workspace. In fact it is equivalent to:
details(1).details = 'name';
details(2).details = 'age';
details(3).details = 'gender';
which has absolutely nothing to do with any variables that might have those names.
as rightly commented by Stephen,
"and if you already have name, age and gender variables in your workspace following command will suffice details=struct('details',{'name','age','gender'});",
will not generate the structure that was asked in the question.
Refer to the question Adding multiple fields and values to structure array answered by Walter Roberson for a solution.
data = struct('name', name, 'age', age, 'gender', gender, ....)
If you had field name and value cell arrays in the workspace you could use cell2struct.
names = {'name', 'hair', 'gender', 'tenure'};
values = {'Steve', 'black', 'male', 20.5};
S = cell2struct(values, names, 2)
S = struct with fields:
name: 'Steve' hair: 'black' gender: 'male' tenure: 20.5000

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

카테고리

도움말 센터File Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

태그

질문:

amg
2016년 6월 21일

댓글:

2022년 1월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by