Caused by: MATLAB expression '<output of containers.Map>' is not numeric.
12 views (last 30 days)
Show older comments
Rishika
on 30 Nov 2023
Answered: Walter Roberson
on 7 Dec 2023
I created a code in matlab SIMULINK using matlab function. But it keeps saying
Caused by: MATLAB expression '<output of containers.Map>' is not numeric.
Pasting the code for reference, thanks.
function activePlants = ctp(costs, loads, capacities)
coder.extrinsic('containers.Map')
costToPlant=zeros();
costToPlant = containers.Map('KeyType', 'double', 'ValueType', 'double');
for plant = 1:length(costs)
costToPlant(costs(plant)) = plant;
end
sortedCosts = sort(costs);
for loadIndex = 1:length(loads)
netCapacity = 0;
activePlants =zeros();
costIndex = 1;
while costIndex <= length(sortedCosts)
currentPlant = costToPlant(sortedCosts(costIndex));
if netCapacity >= loads(loadIndex)
break;
else
netCapacity = netCapacity + capacities(currentPlant);
activePlants = [activePlants, costs(currentPlant)];
end
costIndex = costIndex + 1;
end
if netCapacity >= loads(loadIndex)
fprintf('Active Plants for load %int8: %s\n', loads(loadIndex), (activePlants));
else
fprintf('Insufficient capacity for load %int8\n', loads(loadIndex));
end
disp(loads(loadIndex),(activePlants))
end
% Example arrays
%capacities = [3, 4, 5];
%loads = [7, 5, 8];
%activePlants = myFunction(costs, loads, capacities);
end
0 Comments
Accepted Answer
Walter Roberson
on 7 Dec 2023
costToPlant=zeros();
costToPlant = containers.Map('KeyType', 'double', 'ValueType', 'double');
You initialize costToPlant as an empty double precision array.
Then you try to store containers.Map onto the same variable.
In Simulink, the datatype of a variable is determined by the first assignment to a variable. Once you have assigned a double precision datatype to costToPlant you cannot change the variable to containers.Map .
The fix is easy: just leave out the initialization to zeros()
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!