Error in table row subscript

9 次查看(过去 30 天)
Emma
Emma 2024-2-15
编辑: Matt J 2024-2-15
falling at the last hurdle - I've tried to include as much information as I can!
I've got what I need MatLab to do working in the workspace, but when it goes into the loop I can't figure out how to make the row extraction (? sorry if not the right thing to call it) correspond to the
i
variable .
In the loop of MPC_DICE its set up i as a dynamic state variable,
i = x(1)
and
i_next = i+1
, so i increases with every loop.
I've written the code that works when using integers but I need it so the row being called = i, if that makes sense.
Northpopulation = readtable("LN_NEXTGROWTH.csv");
LN = [];
LN = Northpopulation.LNORTH(1);
which is exactly what i need it to do and works great, but when I replace 1 with i
Northpopulation = readtable("LN_NEXTGROWTH.csv");
LN = [];
LN = Northpopulation.LNORTH(i);
I get this message:
Error using tabular/subs2inds A table row subscript must be a numeric array containing real positive integers, a logical array, a character vector, a string array, a cell array of character vectors, or a pattern scalar.
Error in tabular/dotParenReference (line 105) rowIndices = t.subs2inds(rowIndices,'rowDim',subsType.forwardedReference);
(i) is being used in other parts of the code as a variable, where i=1 etc... and works fine, i just can't figure out how to make it work with calling the correct row.
  5 个评论
Matt J
Matt J 2024-2-15
编辑:Matt J 2024-2-15
What does "time index considered as a state variable" mean? We need to be able to see what kind of data the variables (in particular x) contains, so you should display x for us and/or attach it in a .mat file.
Emma
Emma 2024-2-15
It's running on these two mat.files, casadi needs to be run first for the MPC-DICE file to operate.
I'm hoping to change the dynamic of the L_NEXT function so it reads my table instead of using the formula given in dice dynamics.

请先登录,再进行评论。

回答(1 个)

Matt J
Matt J 2024-2-15
编辑:Matt J 2024-2-15
In what way is this not what you want?
Northpopulation = readtable("LN_NEXTGROWTH.csv");
%LN = []; <-----You don't need this
x=[5,2 7, 10];
for j=1:numel(x)
i=x(j)
LN = Northpopulation.LNORTH(i)
end
i = 5
LN = 1.2794e+03
i = 2
LN = 1.2761e+03
i = 7
LN = 1.2810e+03
i = 10
LN = 1.2825e+03
  2 个评论
Emma
Emma 2024-2-15
This is exactly what I want, and it works fine when outside of the loop, just for some reason it wont let me use it inside - I have attached the mat files above so hopefully you can see what im getting on about.
Matt J
Matt J 2024-2-15
编辑:Matt J 2024-2-15
I combined all the code as below. Also, I don't know if you were aware, but you can run code right here in these forum posts. So, that's what I did below and the output shown is from that.
I don't recognize what casadi.MX variables are, but it doesn't look like they correspond to any numeric value. They are just symbols. Because of that, it is not clear what row indexing rule you want them to obey. If i=x, as shown below, which row in Northpopulation.LNORTH is that supposed to correspond to?
websave('MPC-DICE.zip',['https://github.com/cmkellett/MPC-DICE/archive/master.zip']);
unzip('MPC-DICE.zip');
addpath([pwd '/MPC-DICE-master']);
websave('casadi.tar.gz',['https://github.com/casadi/casadi/releases/download/3.5.5/casadi-linux-matlabR2014b-v3.5.5.tar.gz']);
untar('casadi.tar.gz','casadi');
addpath([pwd '/casadi']);
import casadi.*
x = MX.sym('x');
i=x
i = x
i_next=i+1
i_next = (1+x)
whos i i_next
Name Size Bytes Class Attributes i 1x1 8 casadi.MX i_next 1x1 8 casadi.MX

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by