Inserting additional data in an already created table

2 views (last 30 days)
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 Comment
Jason
Jason on 9 May 2025
Edited: Jason on 9 May 2025
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'}) ;

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 9 May 2025
Edited: Cris LaPierre on 9 May 2025
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 Comments
Voss
Voss on 10 May 2025
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 on 10 May 2025
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"

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 10 May 2025
Try the function made for adding columns to tables: addvars

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!