Subscripted assignment between dissimilar structures.

2 views (last 30 days)
I got this function that's been running well but now its now throwing this error: "Subscripted assignment between dissimilar structures".
wellSol is a: 1x2 struct fields array. ws fails to inherit this 1x2 struct, so at the end of function wellSol could not match equating it to ws, apparently causing this error.
I'd appreciate it if anyone could help me here.
==========================================================
function wellSol = assignWellValuesFromControl(model, wellSol, W, wi)
for w = 1:numel(wellSol)
ws = wellSol(w);
if ~ws.status
continue
end
tp = ws.type;
v = ws.val;
switch tp
case 'bhp'
ws.bhp = v;
case 'rate'
if ws.sign < 1
continue;
end
if model.water
ws.qWs = v*W(w).compi(wi);
end
case 'wrat'
ws.qWs = v;
otherwise
error('Unknown well control mode %s', tp);
end
wellSol(w) = ws;
end

Answers (1)

Voss
Voss on 30 Mar 2022
It seems likely that wellSol is missing some field that is assigned to ws in your code.
For instance, suppose that wellSol does not have a field 'bhp', but the code assigns a value to the 'bhp' field of the scalar struct ws and then tries to put ws back into wellSol, like this:
% 1-by-2 struct array that does not have a 'bhp' field:
wellSol = struct( ...
'status',{true true}, ...
'type',{'wrat' 'bhp'}, ...
'val',{1 2}, ...
'qWs',{0 0});
for w = 1:numel(wellSol)
ws = wellSol(w);
if ~ws.status
continue
end
tp = ws.type;
v = ws.val;
switch tp
case 'bhp'
% assign v to field 'bhp' in ws:
ws.bhp = v;
% now ws has a 'bhp' field but wellSol does not
case 'wrat'
ws.qWs = v;
end
% now the error happens here because ws and wellSol do not have the
% same set of fields:
try
wellSol(w) = ws;
catch ME
fprintf('error on iteration %d (type %s):\n%s',w,tp,ME.message);
end
end
error on iteration 2 (type bhp): Subscripted assignment between dissimilar structures.

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!