concatination of matalb structures with diffrent set of fields in recursive call

5 views (last 30 days)
I have a recursive function that i use to explore diffrent parallel problems, then each probelm may have diffrents parallel solutions, then I have to store the solutions in the same structure. This is my code witch works well if the the pblems Have juste one solution but when it parallel I get the folowing error "Number of fields in structure arrays being concatenated do not match. Concatenation of structure arrays requires that these arrays have the same set of fields.
"
here is my function code
function Result =RecursiveProgramTest(Sol,Concept_matrix,FPrime,Concept,Level,EndFPrime,EndFPrimeLast,s)
[Levelbis,NumberofParallelLevel,EndFPrime,IndexToDelete] = ServiceSemanticMatchingFunction(Sol.UR.Plus.Ancestors.Name,Sol.UR.Plus.Concept,Concept_matrix,FPrime,length(FPrime),Concept,EndFPrime,s,Level);
if isempty(Levelbis) || EndFPrime==EndFPrimeLast
Result.AS=[];
Result.UR=[];
else
if length(Levelbis)==1
FPrime(IndexToDelete)=[];
AbstactCompositionPlan.AS=Levelbis;
Variable.Concept.Name = Levelbis.Outputs.Name;
Variable.Ancestors.Name =Levelbis.Outputs.Ancestors.Name;
AbstactCompositionPlan.UR.Plus = Variable;
Result= cat(1,AbstactCompositionPlan,RecursiveProgramTest(AbstactCompositionPlan,Concept_matrix,FPrime,Concept,Level,EndFPrime,EndFPrimeLast,s));
EndFPrime= EndFPrime+1;
else
FPrime(IndexToDelete)=[];
for i=1:length(Levelbis)
x=i
EndFPrimeParellel{i}=EndFPrime;
AbstactCompositionPlan(i).AS=Levelbis(i);
Variable(i).Concept.Name =Levelbis(i).Outputs.Name;
Variable(i).Ancestors.Name =Levelbis(i).Outputs.Ancestors.Name;
AbstactCompositionPlan(i).UR.Plus = Variable(i);
FPrimeParalell{i}=FPrime;
Result.AS{i}= cat(1,AbstactCompositionPlan(i),RecursiveProgramTest(AbstactCompositionPlan(i),Concept_matrix,FPrimeParalell{i},Concept,Level,EndFPrime,EndFPrimeLast,s));
EndFPrimeParellel{i}= EndFPrimeParellel{i}+1; % le metre en parl
end
end
end
Here some figures to show he result, wel the first 6 problem have not parallel solution so there is no problem, but the 7 probelm have 3 parallel solutios, so the cancatination in the recursive cal under the loop get me the error.
If anywone can help me I will be very gratful

Accepted Answer

Bruno Luong
Bruno Luong on 23 Oct 2022
Edited: Bruno Luong on 6 Nov 2022
Try to include the following function and replace in your code
cat(1, ...)
by
catstruct(1, ...)
function S = catstruct(dim, varargin)
% S = catstruct(dim, S1, S2, ...) or ...
% S = catstruct(dim, {S1, S2, ...})
% concatenate structures arrays but can tolerate non-matched fieldnames
% Structures without a specific fielname with be populated with [] in the
% missing field.
%
% See also: mergestruct
if length(varargin)==1 && iscell(varargin)
c = varargin{1};
else
c = varargin;
end
iss = cellfun(@isstruct, c);
c(~iss) = []; %{struct()};
template = emptytemplate(c);
S = cellfun(@(s) mergestruct(s, template), c, 'unif', 0);
S = cat(dim, S{:});
end % catstruct
%%
function s = emptytemplate(c)
f = cellfun(@fieldnames, c, 'unif', 0);
f = unique(cat(1, f{:}));
sarg = f.';
sarg(2,:) = {[]};
s = struct(sarg{:});
end
function S = mergestruct(S1, S2)
% S = mergestruct(S1, S2)
% Merge two structures arrays to a single one
% Note: for fieldnames conflict values in S1 are selected
%
% See also: catstruct, mmergstruct
sz = size(S1);
S1 = S1(:);
S2 = S2(:);
f1 = fieldnames(S1);
f2 = fieldnames(S2);
v1 = struct2cell(S1);
v2 = struct2cell(S2);
[f2, ikeep] = setdiff(f2,f1,'stable');
v2 = v2(ikeep,:);
v1arg = num2cell(v1,2);
v2arg = num2cell(v2,2);
sarg = [f1 v1arg; ...
f2 v2arg].';
S = struct(sarg{:});
S = reshape(S,sz);
end % mergestruct
  7 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!