Assigning multiple variables to a structure

46 次查看(过去 30 天)
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
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 个评论
Merlin Scherer
Merlin Scherer 2022-1-7
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, ....)
Steven Lord
Steven Lord 2022-1-7
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by