MATLAB: How to change the code to stop a for loop simply replacing rows of outputs in a uitable

datafor loopMATLABuitable

function drainage_system_design_rough
n=input('How many pipes are in the system? ');
PRef=[1:1:n];
for PRef=1:n
disp('Pipe reference:')
disp(PRef)
L=input('Pipe Length:');
schedule=input('Do you want to add pipe to drainage schedule? (Yes=1, No=0)?: ');
if schedule==1
col={'Pipe Length(m)'};
row={PRef};
dat={L};
uitable('columnname',col,'rowname',row,'data',dat);
end
end
I am really new to MATLAB but want to create a simple table to store drainage system information. I want the rows of the table to be labelled by the pipe references (the number of which depend on the number of pipes in the system). However, each time the for loop is executed only the information for the last pipe is stored in the table and it replaces the informaton for the previous pipe. If any could help me code this better so I had a table that added rows each time the for loop was executed but also stored the previous rows, that would be much appreciated.

Best Answer

Uitables do not have an append mode. When you specify data, you either need to specify the specific row, or reload all the data. Given the functionality you want, my approach would be to first extract the existing values/rownames, then append the new values to the end, and pass the combined values back in as the table data.
fig = uifigure;
uit = uitable(fig,'columnname','Pipe Length(m)',"RowName",{},"Data",[]);
n=input('How many pipes are in the system? ');
PRef=[1:1:n];
for PRef=1:n
disp('Pipe reference:')
disp(PRef)
L=input('Pipe Length:');
schedule=input('Do you want to add pipe to drainage schedule? (Yes=1, No=0)?: ');
if schedule==1
uit.Data = [uit.Data;L];
uit.RowName = {uit.RowName{:};PRef};
end
end