Inserting additional data in an already created table

5 visualizaciones (últimos 30 días)
Jason
Jason el 9 de Mayo de 2025
Respondida: Image Analyst el 10 de Mayo de 2025
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 comentario
Jason
Jason el 9 de Mayo de 2025
Editada: Jason el 9 de Mayo de 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'}) ;

Iniciar sesión para comentar.

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 9 de Mayo de 2025
Editada: Cris LaPierre el 9 de Mayo de 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 comentarios
Voss
Voss el 10 de Mayo de 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 el 10 de Mayo de 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"

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 10 de Mayo de 2025
Try the function made for adding columns to tables: addvars

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by