Error in table row subscript

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 Comments

Matt J
Matt J on 15 Feb 2024
Edited: Matt J on 15 Feb 2024
but I need it so the row being called = i, if that makes sense.
It's a bit fuzzy, actually. You've just read Northpopulation straight from a .csv file. How could the rows of Northpopulation.LNORTH contain specialized variables like dynamic state variables? How could they contain anything other than plain numbers or text?
I would recommend attaching the .csv file so it can be examined, and solutions demonstrated.
You refer to a loop but then never show any loop construct (for, while, parfor, etc.)
Please attach your CSV file (as Matt J requested) and show us a small and complete segment of code you can run to reproduce the problem. I suspect based on the error message that you think you've defined a variable named i while you actually haven't defined it before trying to use it, but seeing your code should help confirm or refute my suspicion.
Emma
Emma on 15 Feb 2024
Edited: Emma on 15 Feb 2024
Thanks for getting back to me so quickly - attached is the file!
I didn't set up the loop, I've just downloaded it - I'm using the MPC-DICE model, its quite a large code thats a construct with a page of dynamics and a page parameters feeding into it, I'm not sure I can share a part that works alone. The variable i is pre-determined within the downloaded model and used throughout with no problems.
I've had a look through and I think this is the set up for i, I think i is just a count for the number of loops that have taken place:
i = x(1); % time index considered as state variable
%% Dynamics / state recursion
i_NEXT = i+1; %time index
These codes both return what I need it to do when i use them in the workspace, as year in the table is just the row number
By row number:
Northpopulation = readtable("LN_NEXTGROWTH.csv");
LN = [];
LN = Northpopulation.LNORTH(1);
or by year == 1
Northpopulation = readtable("LN_NEXTGROWTH.csv")
LN = [];
LN = Northpopulation.LNORTH(Northpopulation.Year==1);
But I need it to work in the loop so the row being called corresponds to i as i increases, so that the next LNORTH value is chosen.
When I replace 1 with i, it just returns the error message as above even though i=1 in the first loop, and 2 in the next and so on
Matt J
Matt J on 15 Feb 2024
Edited: Matt J on 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.
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.

Sign in to comment.

Answers (1)

Matt J
Matt J on 15 Feb 2024
Edited: Matt J on 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 Comments

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.
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

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2023b

Asked:

on 15 Feb 2024

Edited:

on 15 Feb 2024

Community Treasure Hunt

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

Start Hunting!