Inserting additional data in an already created table

3 次查看(过去 30 天)
Hello, Im messing around with tables again and create one from the user inputs from inputdlg
prompt = {'Soak(s)','Soak Move (ymm)','Laser Volts (Soak, Image)','numImages(afterSOAK)','ZTLpos(soak), abs','Back To Nominal Wait Time(s)','NumImages (Back To Nominal)'};
dlgtitle = 'Laser Soak';
dims = [1 35];
definput = {'30','-1','2, 0.18','4','-1833','60','12'};
answer = inputdlg(prompt,dlgtitle,dims,definput);
answer
T = table(char(answer))
I want to add another entry to the end and thought this would do it
defaultZTL='-2000';
T(end+1,1)={defaultZTL}
But Im getting this error
Assigning to a character variable in a table is only supported when the right-hand side value is a table. Consider using a string
variable in the table for text data.
Error in HTS_TestSoftware/LaserSOAKButtonPushed (line 12557)
T(end+1,1)={defaultZTL}
  1 个评论
Jason
Jason 2025-5-9
编辑:Jason 2025-5-9
I've also tried to create a new table for the additonal row
T = table(char(answer))
rnames={'Soak(s)','Soak Rel. Pos (mm)','Soak/Image Laser (V)',['#Fast Images(PostSoak) @ ',num2str(afterSoakPause),'s'],'ZTL (Soak)','Return To Nom(ztl). Wait(s)','#Images(Return To Nom ztl)'}'
T.Properties.RowNames=rnames
T.Properties.VariableNames={'Input'};
% Add new row by creating another table
defaultZTL='-2000';
Tnew = table(defaultZTL,'RowNames',{'ZTL default Pos'}) ;
T = [T ; Tnew]
Incorrect number of arguments.
Error in HTS_TestSoftware/LaserSOAKButtonPushed (line 12564)
Tnew = table(defaultZTL,'RowNames',{'ZTL default Pos'}) ;

请先登录,再进行评论。

采纳的回答

Cris LaPierre
Cris LaPierre 2025-5-9
编辑:Cris LaPierre 2025-5-9
The issue is that char arrays must be padded so that they all have the same length. The value you are assigning does not have the same width as the other values in the table.
The simplest solution is to use strings instead.
answer = {'30','-1','2, 0.18','4','-1833','60','12'}';
T = table(string(answer))
T = 7x1 table
Var1 _________ "30" "-1" "2, 0.18" "4" "-1833" "60" "12"
defaultZTL = '-2000';
T(end+1,1) = {defaultZTL}
T = 8x1 table
Var1 _________ "30" "-1" "2, 0.18" "4" "-1833" "60" "12" "-2000"
  7 个评论
Voss
Voss 2025-5-10
Say this is your table
T = table(["30";"-1"],'RowNames',{'Soak(s)';'Soak Rel.'})
T = 2x1 table
Var1 ____ Soak(s) "30" Soak Rel. "-1"
and you add the new row
T(end+1,1) = {"2, 0.18"}
T = 3x1 table
Var1 _________ Soak(s) "30" Soak Rel. "-1" Row3 "2, 0.18"
then you can set the new row name
T.Properties.RowNames{end} = 'new name'
T = 3x1 table
Var1 _________ Soak(s) "30" Soak Rel. "-1" new name "2, 0.18"
Cris LaPierre
Cris LaPierre 2025-5-10
To add to Voss' response, rownames are stored in the table metadata. They are accessed through the table properties. I don't think you can update rownames when using a cell array to add a table row
Another option that would work is to add the names as a 2nd table column. Then you can update both while using the cell array approach.
input = string({'30','-1','2, 0.18','4','-1833','60','12'})';
rnames=string({'Soak(s)','Soak Rel. Pos (mm)','Soak/Image Laser (V)','#Fast Images(PostSoak) @ 0.5s','ZTL (Soak)','Return To Nom(ztl). Wait(s)','#Images(Return To Nom ztl)'})';
T = table(rnames,input)
T = 7x2 table
rnames input _______________________________ _________ "Soak(s)" "30" "Soak Rel. Pos (mm)" "-1" "Soak/Image Laser (V)" "2, 0.18" "#Fast Images(PostSoak) @ 0.5s" "4" "ZTL (Soak)" "-1833" "Return To Nom(ztl). Wait(s)" "60" "#Images(Return To Nom ztl)" "12"
defaultZTL = '-2000';
T(end+1,:) = {'defaultZTL',defaultZTL}
T = 8x2 table
rnames input _______________________________ _________ "Soak(s)" "30" "Soak Rel. Pos (mm)" "-1" "Soak/Image Laser (V)" "2, 0.18" "#Fast Images(PostSoak) @ 0.5s" "4" "ZTL (Soak)" "-1833" "Return To Nom(ztl). Wait(s)" "60" "#Images(Return To Nom ztl)" "12" "defaultZTL" "-2000"

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2025-5-10
Try the function made for adding columns to tables: addvars

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by