How can I add a column to a table / add a variable to a table?

1 次查看(过去 30 天)
Hi there, I want to add columns to a table which seems to be impossible by default. I created a workaround to do this but I wonder if there is a more straightforward way to do this? Thanks very much for your help!
Here's the code:
clear all, clc;
% the point of this script is to analyze region properties of a
% binary image and write them to a file. However, more columns
% should be added before.
% read coins sample and analyze region properties
imageName = 'coins.png';
I = im2bw(imread(imageName));
Cstruct = bwconncomp(I,8);
LC = labelmatrix(Cstruct);
ObjectInfo = regionprops(Cstruct, {'Area','Centroid'});
% Problem:
% it is not possible to add another variable to ObjInfo
% like ObjInfo.newCol = 1:length(ObjectInfo)
% create Table from Results
ObjTable = struct2table(ObjectInfo);
% I want to add columns to the table.
% But adding columns to a table is not possible.
% this is a dirty workaround
ObjTable.rowID = cellstr(num2str(ObjTable.Area));
ObjTable.sampleID = ObjTable.rowID;
ObjTable.imgID = ObjTable.rowID;
a = ObjTable.rowID;
b = ObjTable.sampleID;
c = ObjTable.imgID;
for m=1:numel(a)
a(m) = {m};
b(m) = cellstr(imageName);
c(m) = cellstr(imageName);
end
ObjTable.rowID = a;
ObjTable.sampleID = b;
ObjTable.imgID = c;
clear a b c m;
% done
ObjTable

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by