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

15 次查看(过去 30 天)
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 个评论
Bill Tubbs
Bill Tubbs 2020-3-30
编辑:Bill Tubbs 2020-3-30
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.

请先登录,再进行评论。

采纳的回答

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

请先登录,再进行评论。

更多回答(2 个)

Fangjun Jiang
Fangjun Jiang 2020-3-30
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 个评论
Bill Tubbs
Bill Tubbs 2020-3-30
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 2020-3-30
编辑:Bill Tubbs 2020-3-30
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.

请先登录,再进行评论。


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

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by