Assigning multiple variables to a structure

48 visualizzazioni (ultimi 30 giorni)
amg
amg il 21 Giu 2016
Commentato: Steven Lord il 7 Gen 2022
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

Risposte (1)

Piyush kant
Piyush kant il 29 Mag 2019
Modificato: Piyush kant il 29 Mag 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 Commenti
Merlin Scherer
Merlin Scherer il 7 Gen 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, ....)
Steven Lord
Steven Lord il 7 Gen 2022
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

Accedi per commentare.

Categorie

Scopri di più su Environment and Settings 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