필터 지우기
필터 지우기

Assigning multiple variables to a structure

조회 수: 45 (최근 30일)
amg
amg 2016년 6월 21일
댓글: Steven Lord 2022년 1월 7일
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