Is it possible to have a table with a numerical index instead of strings?

15 Ansichten (letzte 30 Tage)
I'm new to MATLAB and wanted to make a table containing time-series data where the index (time) is a series of integers.
I tried this but I think it doesn't like the fact that the row names are not strings.
>> N = 5;
>> k_init = 5;
>> u = zeros(N+k_init,1);
>> y = zeros(N+k_init,1);
>> ts = [1-k_init:N]';
>> data = table(u,y,'RowNames',num2cell(ts));
Error using table (line 326)
The RowNames property must be a string array or a cell array, with each element containing one nonempty name.
>> class(num2cell(ts))
ans =
'cell'
It seems to only work with strings:
>> ts = {'-4','-3','-2','-1','0','1','2','3','4','5'};
>> data = table(u, y,'RowNames',ts)
data =
10×2 table
u y
_ _
-4 0 0
-3 0 0
-2 0 0
-1 0 0
0 0 0
1 0 0
2 0 0
3 0 0
4 0 0
5 0 0
What I am ultimately trying to do is access the data using the ts as an index, something like this:
>> k = 2
>> data(k,'y') = 2*data(k-1,'u')
(Similar to a DataFrame in Python)
  1 Kommentar
Bill Tubbs
Bill Tubbs am 30 Mär. 2020
Bearbeitet: Bill Tubbs am 30 Mär. 2020
An alternative perhaps: Is there any way to index an iddata object with a timestep value when it doesn't start at 1?
>> data = iddata(y,u,1,'Tstart',-4);
>> data.y(-4)
Error using iddata/subsref (line 49)
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Other than data.u(-4+data.Tstart) obviously.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Peng Li
Peng Li am 30 Mär. 2020
Why do you want to make the time as row names? I think it's better to keep it as one of the variable in the table. It is not necessary that each row has a name.
It's easier to index table by dot indexing. e.g., data.u(k) and data.y(k-1) for example. If you make ts as a variable, you can use for example data.u(data.ts == 2) to index variable u in the table where ts is equal to 2.
  5 Kommentare
Bill Tubbs
Bill Tubbs am 30 Mär. 2020
I was just making the point that the solution `data.u(data.ts == 2)` could return more than one value if you're not careful.
Peng Li
Peng Li am 31 Mär. 2020
Oh yeah this is correct. So better apply things according to our own understanding of our data.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Fangjun Jiang
Fangjun Jiang am 30 Mär. 2020
Keep in mind that in MATLAB, indexing like data(RowIndex, ColIndex) can be numerical or logical. The numerical index is 1-based. It can't be negative numerical value. It can't be zero like in C.
Your value is -4 to 5. There is an easy way to specify an offset.
The other thing you might be able to use is cellstr(num2str((-4:5)'))
  2 Kommentare
Bill Tubbs
Bill Tubbs am 30 Mär. 2020
Thanks, I realise I can use an offset. I just thought there might be a way to do indexing in Matlab when the index doesn't start at 1.
Bill Tubbs
Bill Tubbs am 30 Mär. 2020
Bearbeitet: Bill Tubbs am 30 Mär. 2020
Given that this seems to be impossible, I think the best solution might be to give up and just maintain two seperate variables, one for the actual timestep (k=-4:5) and another for the index (i=1:10).
In other words:
>> k = 2
>> i = k - k_start;
>> data(i,'y') = 2*data(i-1,'u')
However, this only works here were the timestep is 1.

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 30 Mär. 2020
data(string(k),'y') = 2*data(string(k-1),'u')

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by