필터 지우기
필터 지우기

Expand Struct Array Elements Rowwise

조회 수: 8 (최근 30일)
Jens Olschewski
Jens Olschewski 2021년 11월 17일
답변: Yukthi S 2024년 2월 16일
Hi everyone,
is there a possibility in Matlab to epand a existing structy array rowwise ?
Example for one Field Element
for i=1:Steps:End
[Output.temp.Results]=fun(Input);
Output.Curve.Results.A(i,:)=Output.temp.Results.A
end
Now my Struct Array has about 50 Fieldelements. Is there an elegant way to do this in a few lines?
Thans for your help.

답변 (1개)

Yukthi S
Yukthi S 2024년 2월 16일
Hi Jens Olschewski
I got that you wanted to expand the Struct array from one field element to 50 field elements. For that, you need to use a For loop in the function “fun” definition.
Since you did not provide any information about the rest of the code ,I am assuming some input values and also “fun” definition.
You can refer the following code which is verified in MATALB R2019a, to get a struct array with 50 field elements:
% Define Steps and End
Steps = 1;
End = 5;
% Initialize an empty struct for Output.Curve.Results
Output.Curve.Results = struct();
for i = 1:Steps:End
% Call the function and get the temporary results
Output.temp.Results = fun(i); % 'i' is passed as an input for the example
% Dynamically expand each field
fieldNames = fieldnames(Output.temp.Results);
for j = 1:numel(fieldNames)
fieldName = fieldNames{j};
% Check if the field already exists in Output.Curve.Results
if isfield(Output.Curve.Results, fieldName)
% Expand the field row-wise
Output.Curve.Results.(fieldName)(end+1,:) = Output.temp.Results.(fieldName);
else
% Initialize the field in Output.Curve.Results
Output.Curve.Results.(fieldName)(1,:) = Output.temp.Results.(fieldName);
end
end
end
% Display the structure of the final struct array
disp(fieldnames(Output.Curve.Results));
{'Field1' } {'Field2' } {'Field3' } {'Field4' } {'Field5' } {'Field6' } {'Field7' } {'Field8' } {'Field9' } {'Field10'} {'Field11'} {'Field12'} {'Field13'} {'Field14'} {'Field15'} {'Field16'} {'Field17'} {'Field18'} {'Field19'} {'Field20'} {'Field21'} {'Field22'} {'Field23'} {'Field24'} {'Field25'} {'Field26'} {'Field27'} {'Field28'} {'Field29'} {'Field30'} {'Field31'} {'Field32'} {'Field33'} {'Field34'} {'Field35'} {'Field36'} {'Field37'} {'Field38'} {'Field39'} {'Field40'} {'Field41'} {'Field42'} {'Field43'} {'Field44'} {'Field45'} {'Field46'} {'Field47'} {'Field48'} {'Field49'} {'Field50'}
function result = fun(input)
% Initialize an empty struct
result = struct();
% Loop through to create 50 fields with random numbers
for k = 1:50
fieldName = sprintf("Field%d", k); % Generate field name dynamically
result.(fieldName) = rand(1, 1); % Assign a random number to the field
end
end
You can refer to the following MATLAB documentation to have more information on "fieldnames":https://in.mathworks.com/help/releases/R2019a/matlab/ref/fieldnames.html?searchHighlight=fieldnames&s_tid=doc_srchtitle

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by