Assigning multiple variables to a structure
Afficher commentaires plus anciens
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
Réponses (1)
Piyush kant
le 29 Mai 2019
Modifié(e) : Piyush kant
le 29 Mai 2019
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 commentaires
Stephen23
le 29 Mai 2019
"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.
Merlin Scherer
le 7 Jan 2022
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)
Catégories
En savoir plus sur Logical dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!