Insert time depending on sample time and measurement points into existing table

2 ビュー (過去 30 日間)
Robin L
Robin L 2021 年 1 月 21 日
コメント済み: Star Strider 2021 年 1 月 21 日
Hello,
I am trying to add time to my existing table. I have tried different methods but I am just getting started with MatLab and can't seem to figure this one out.
Where N = samples = 250.000 and ST = SampleTime = 3 and TS = Timestep = ST/N.
So I want it to add to my 6th column which I have already declared and where row 1 = 0, row 2 = 0 + TS, etc.. until I reach row 250.000 = ST.
Code I wrote for this part:
%add column6 and time to T1
T1.time = rand(250000,1);
ST = 3; %declare SampleTime
N = 250000; %SampleSize
TS = ST/N; %calc TimeStep
i = 0;
while i < N
T1(i,6) = 0 + i.*TS;
i = i + 1;
end
This would give me the error:
"Right hand side of an assignment into a table must be another table or a cell array."
I suppose it has something to do with declaration of which column the data should be placed but I have no idea how I could fix this.

採用された回答

dpb
dpb 2021 年 1 月 21 日
In "the MATLAB way", don't use loops where not needed...since you have end points and number, use linspace
ST = 3; %declare SampleTime
N = 250000;
T.Var6=linspace(0,ST,N);
You didn't tell us the name for the sixth column, I used the MATLAB default "VarN' form as a placeholder.
Your code has a problem in that you use regular parentheses for assignment where inside the table to write a single value you need {}. It's confusing, I know, but read the section on accessing data in a table; there's a chart that shows all the various addressing modes and what each returns.
  2 件のコメント
Robin L
Robin L 2021 年 1 月 21 日
Thank you! Much simpler indeed.
This is the code I use now:
ST = 3; %declare SampleTime
N = 250000; %SampleSize
Tnew=linspace(0,ST,N); %create linear space
Tnew = array2table(Tnew); %convert array to table
Tnew = table2cell(Tnew); %transpose table
Tnew = cell2table(Tnew');
T1 = [T1 Tnew]; %add Tnew to T1
T1.Properties.VariableNames{'Var1'} = 'Time_s';
Star Strider
Star Strider 2021 年 1 月 21 日
ST = 3; %declare SampleTime
N = 250000; %SampleSize
Tnew=linspace(0,ST,N); %create linear space
T1 = [T1 table(Tnew(:))]; %add Tnew to T1
T1.Properties.VariableNames{'Var1'} = 'Time_s';
.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTest Model Components についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by