Adding values to a table
59 次查看(过去 30 天)
显示 更早的评论
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
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
2023-10-7
One approach (that works) —
DateTime = datetime([2020 01 01]) + calmonths(0:5)';
Incidence = randi(10,6,1);
tab = table(DateTime,Incidence)
newRow = {datetime('01-Jul-2020') 8};
tab(end+1,:) = newRow
.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!