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