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

15 visualizaciones (últimos 30 días)
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 comentario
Bill Tubbs
Bill Tubbs el 30 de Mzo. de 2020
Editada: Bill Tubbs el 30 de Mzo. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Peng Li
Peng Li el 30 de Mzo. de 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 comentarios
Bill Tubbs
Bill Tubbs el 30 de Mzo. de 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 el 31 de Mzo. de 2020
Oh yeah this is correct. So better apply things according to our own understanding of our data.

Iniciar sesión para comentar.

Más respuestas (2)

Fangjun Jiang
Fangjun Jiang el 30 de Mzo. de 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 comentarios
Bill Tubbs
Bill Tubbs el 30 de Mzo. de 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 el 30 de Mzo. de 2020
Editada: Bill Tubbs el 30 de Mzo. de 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.

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 30 de Mzo. de 2020
data(string(k),'y') = 2*data(string(k-1),'u')

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by