Adding values to a table

64 ビュー (過去 30 日間)
John
John 2023 年 10 月 7 日
回答済み: Star Strider 2023 年 10 月 7 日
Hello everyone,
I'm trying to run through a specific column of a table and add values to the rows in that column.
How do I actually refer to the rows/cells of the table that I want to access? In a low level language like C it would be row[i] but I don't know the equivalent in MATLAB.
I've put the code below with a comment in the for loop explaining what I want to do.
Kind regards,
John
%Creates a table called 'tab' using the data in the coronavirus-cases.csv file.
tab = readtable("coronavirus-cases.csv");
%Creates a table called 'newTabCol' with the height of tab and a width of 1.
newTabCol = zeros(height(tab), 1);
%Adds the column and titles it "Seven Day Average".
tab.SevenDayAverage = newTabCol;
for i = 1: height(tab)
%Set all values in SevenDayAverage to 1.
end
  1 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 10 月 7 日
Why not set the values in the newTabCol array and assign it like you have done (instead of going through the for loop)?
"How do I actually refer to the rows/cells of the table that I want to access?"

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

採用された回答

Star Strider
Star Strider 2023 年 10 月 7 日
One approach (that works) —
DateTime = datetime([2020 01 01]) + calmonths(0:5)';
Incidence = randi(10,6,1);
tab = table(DateTime,Incidence)
tab = 6×2 table
DateTime Incidence ___________ _________ 01-Jan-2020 9 01-Feb-2020 8 01-Mar-2020 9 01-Apr-2020 4 01-May-2020 7 01-Jun-2020 2
newRow = {datetime('01-Jul-2020') 8};
tab(end+1,:) = newRow
tab = 7×2 table
DateTime Incidence ___________ _________ 01-Jan-2020 9 01-Feb-2020 8 01-Mar-2020 9 01-Apr-2020 4 01-May-2020 7 01-Jun-2020 2 01-Jul-2020 8
.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by