Info

此问题已关闭。 请重新打开它进行编辑或回答。

How can I assign subfield values of one structure to another structure by using specific row index number in one line of code?

1 次查看(过去 30 天)
% Hi everyone. I have a problem related with assigning series of values to
% a specific row indexes of a structure subfields from another structure's
% subfields
% Let me explain my case step by step with an example code
% I have an Output structure that I preallocated with nan values and subfield
% array sizes (in columns) are different from each other but row lengths are same
OutputStruct.SubField1 = nan(100, 3);
OutputStruct.SubField2 = nan(100, 1);
OutputStruct.SubField3 = nan(100, 30);
OutputStruct.SubField4 = nan(100, 30);
% I created an increment value that I will use in for loop
inc = 0;
% Now I have a for loop that will cycle as the number of row length of OutputStruct subfields
for i = 1:100
% I am refreshing increment value in every cycle
inc = inc +1;
%%%
% SOME CALCULATIONS
%%%
% I have an another structure with same number of subfields and fieldnames of OutputStruct
% but this time it's row length is 1 and column numbers are same with subfields of OutputStruct respectively.
% Instead of OutputStruct 'NaN' case, here we have calculated values.
% I am filling them with sample values here but their values are changing
% everytime we cycle in for loop and their column sizes stay the same
NewStruct.SubField1 = 1:3;
NewStruct.SubField2 = 1;
NewStruct.SubField3 = 1:30;
NewStruct.SubField4 = 1:30;
% Here I have an if statement that will check some calculations happening between "if" line and "for" line.
if
% Now my main issue is here. I want to assign all these subfield values of NewStruct to subfields of OutputStruct
% with specific row increment value and I don't want to use an extra for loop.
% For example, version with extra for loop solution is:
FieldNames = fieldnames(OutputStruct);
for j = 1:length(FieldNames)
% Here I am going to use increment "inc" for row index to fill the preallocated data with new values of NewStruct subfields.
% Due to if condition above, increment value can be without order like 4, 13, 42 .. etc
OutputStruct.(FieldNames{j})(inc,:) = NewStruct.(FieldNames{j});
end
% This approach solves the problem but it is time consuming.
% I want to do exactly what this "for loop" does in one line of code.
end
% I asked a similar question yesterday but that case was a bit different.
% Answers for that question were really helpful and saved me great time.
% Can you please help me again? I would be very appreciated, if you could.
end
%Thank you all for your great effort

回答(0 个)

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by