Index in position 1 is invalid. Array indices must be positive integers or logical values.
3 次查看(过去 30 天)
显示 更早的评论
Hi,
I am trying to run a function inside a for loop with an index that should start at 1 however, MATLAB gives me an indexing error eventhough I have started the for loop from i = 1:c. I am running simulations in HYSYS through MATLAB.
global simname
simname = "C:\Users\Desktop\MEA1_25102021.tpl";
% Generating the design matrix with inputs
T = [45, 50]';
P = [1, 2]';
x = fullfact([2 2]);
x = [T(x(:, 1)), P(x(:,2))];
% Preallocating
c = length(x);
CO2removal = zeros(1, c);
reboil_duty = zeros(1, c);
% Calling the function
for i = 1:c
[CO2removal, reboil_duty]=simulation(x);
end
function [CO2removal, reboil_duty]=simulation(x)
hysys=actxserver('HYSYS.Application.V9.0');
hysys.ChangePreferencesToMinimizePopupWindows(1);
global simname
SimCase=hysys.SimulationCases.Open(simname);
SimCase.visible=true;
Solver=SimCase.solver;
Solver.Cansolve=false;
sprdsht=hysys.activeDocument.Flowsheet.Operations.Item('I/O');
%Setting input values (independent vars)
hyset(sprdsht.Cell('B10'),x(i, 1))
hyset(sprdsht.Cell('D10'),x(i, 2))
Solver.Cansolve=true;
while Solver.issolving
pause(on)
end
% Reading output/dependent/calculated values
sprdsht=hysys.activeDocument.Flowsheet.Operations.Item('I/O');
CO2removal(i) = sprdsht.Cell('D12').CellValue;
reboil_duty(i) = sprdsht.Cell('B12').CellValue;
end
Please let me know if you need more context.
Thanks.
0 个评论
采纳的回答
David Hill
2021-11-9
for i = 1:c
[CO2removal, reboil_duty]=simulation(x,i);%need to pass i to the function as well
end
function [CO2removal, reboil_duty]=simulation(x,i)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!