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

21 views (last 30 days)
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 =
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 Comment
Bill Tubbs
Bill Tubbs on 30 Mar 2020
Edited: Bill Tubbs on 30 Mar 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.

Sign in to comment.

Accepted Answer

Peng Li
Peng Li on 30 Mar 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.
Peng Li
Peng Li on 31 Mar 2020
Oh yeah this is correct. So better apply things according to our own understanding of our data.

Sign in to comment.

More Answers (2)

Fangjun Jiang
Fangjun Jiang on 30 Mar 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)'))
Bill Tubbs
Bill Tubbs on 30 Mar 2020
Edited: Bill Tubbs on 30 Mar 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.

Sign in to comment.

Walter Roberson
Walter Roberson on 30 Mar 2020
data(string(k),'y') = 2*data(string(k-1),'u')


Find more on Resizing and Reshaping Matrices in Help Center and File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by