Inserting additional data in an already created table

조회 수: 5 (최근 30일)
Jason
Jason 2025년 5월 9일
답변: Image Analyst 2025년 5월 10일
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