If else statement now hanging up and error message Subscripting into a table using one subscript

9 views (last 30 days)
I ran this calculation in the R2023 version and now trying to run in the R2024 version. The only change is the variables since the last time it was used. I am now getting an error message Error using () (line 132)
Subscripting into a table using one subscript (as in t(i)) is not supported. Specify a row subscript and a variable subscript, as in t(rows,vars). To select variables, use t(:,i) or for one variable t.(i). To select rows, use t(i,:).
There is no code on line on 132 of matlab so I am assuming that this is reaching line 132 of the table which I am asking it to step through (Total_Power and demand) to do the calculation against. The Total_Power and demand files are 8760 x 1 tables.
This is a calculation for the state of charge of batteries if that is relevant.
Battery parameters
Cb=111.4;
Vb=90;
DisEff=0.94;
CharEff=0.94;
delta=0.0002;
Nb=5;
bmax=1.0;
bmin=0.0;
nd=numel(Total_Power);
Set the initial conditions
soc(1)=0.9;
dump(1)=0;
ld=demand;
Then the calculation - as I said this has previously worked okay with different battery parameters and a different Total_Power and demand table. It should be noted that the Total_Power and demand files whilst the same 8760 x 1 table the contents of each cell were different.
for tt=1:nd
if (Total_Power(tt)-ld(tt))>=0;
soc(tt+1)=soc(tt)*(1-delta)+((Total_Power(tt)-ld(tt))/(Nb*Cb*Vb))*CharEff;
dump(tt+1)=0;
if soc(tt+1)>=1;
soc(tt+1)=1;
dump(tt+1)=(Total_Power(tt))-ld(tt);
end
else
soc(tt+1)=soc(tt)*(1-delta)+(Total_Power(tt)-ld(tt))/(Nb*Cb*Vb*DisEff);
dump(tt+1)=0;
end
end
What is it that has changed and how do I get the for loop to step through all of the 8760 records and give a result rather than stopping at line 132 (which I am assuming it is doing as I have no code at line 132 of matlab and that is the line number it is giving in the error message.

Accepted Answer

Hitesh
Hitesh on 18 Nov 2024
I too have encountered the same issue. You need to use "Table_Name.Variable_Name" to access the value present in the table. I have modified your code and is working as expected. Kindly refer to the below code.
Total_Power = array2table(rand(8760, 1) * 100, 'VariableNames', {'Power'});
demand = array2table(rand(8760, 1) * 50, 'VariableNames', {'Demand'});
soc(1) = 0.9;
dump(1) = 0;
ld = demand.Demand;
% Calculation
for tt = 1:nd
if (Total_Power.Power(tt) - ld(tt)) >= 0
soc(tt + 1) = soc(tt) * (1 - delta) + ((Total_Power.Power(tt) - ld(tt)) / (Nb * Cb * Vb)) * CharEff;
dump(tt + 1) = 0;
if soc(tt + 1) >= 1
soc(tt + 1) = 1;
dump(tt + 1) = (Total_Power.Power(tt)) - ld(tt);
end
else
soc(tt + 1) = soc(tt) * (1 - delta) + (Total_Power.Power(tt) - ld(tt)) / (Nb * Cb * Vb * DisEff);
dump(tt + 1) = 0;
end
end
For more information regarding this error, please refer to the following MATLAB Answer Link:
  3 Comments
Walter Roberson
Walter Roberson on 18 Nov 2024
The rand() are there to create example data. You should replace those two statements with your actual code generating the tables.
Hitesh
Hitesh on 19 Nov 2024
Hi Marjorie,
I generated random sample data using the "rand" function. You should replace these two lines of code with your actual data, where you are retrieving the data from the table.

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 18 Nov 2024
If I remember correctly, indexing into a table array using parentheses and one subscript has never been supported. So if that worked in a previous release, perhaps Total_Power was a numeric array rather than a table array in that previous release.
This code looks suspicious if indeed Total_Power was intended to be a table array. I've included a snippet of your code:
nd=numel(Total_Power);
soc(1)=0.9;
dump(1)=0;
ld=demand;
for tt=1:nd
if (Total_Power(tt)-ld(tt))>=0;
Suppose Total_Power was a table with two variables. As written, if that worked MATLAB would attempt to access the elements in the first variable until it reached the last row then would continue with the elements in the second variable. Those two variables may contain completely different data; they may not be the same data type! So your use of numel here is suspicious.
If you still have access to the code you ran to create the Total_Power variable in that older release, please show that. As I said above, I strongly suspect that it created a numeric array rather than a table array. If you do want this to operate on a table, you'll need to use height instead of numel and use two subscripts (row and variable) to index into the table.

Categories

Find more on Large-Scale Modeling in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!