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

1 回表示 (過去 30 日間)
Jakob Nikolas
Jakob Nikolas 2014 年 5 月 3 日
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 Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by