Filter löschen
Filter löschen

Error in table row subscript

6 Ansichten (letzte 30 Tage)
Emma
Emma am 15 Feb. 2024
Bearbeitet: Matt J am 15 Feb. 2024
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 Kommentare
Matt J
Matt J am 15 Feb. 2024
Bearbeitet: Matt J am 15 Feb. 2024
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 am 15 Feb. 2024
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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Matt J
Matt J am 15 Feb. 2024
Bearbeitet: Matt J am 15 Feb. 2024
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 Kommentare
Emma
Emma am 15 Feb. 2024
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 am 15 Feb. 2024
Bearbeitet: Matt J am 15 Feb. 2024
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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Produkte


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by