How do I change my code to stop a for loop simply replacing rows of outputs in a uitable?
1 view (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Cris LaPierre
on 4 Jun 2021
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
7 Comments
Cris LaPierre
on 7 Jun 2021
Yup, I see it. Part of the fun of working with cells... Here's an updated and simplified version. I have removed the inputs, for loop, and if statement.
fig = uifigure;
uit = uitable(fig,'columnname','Pipe Length(m)',"RowName",{},"Data",[]);
PRef=1:5;
L=[5 7 9 11 15];
p=1;
uit.Data = [uit.Data;L(p)];
uit.RowName = [uit.RowName;num2str(PRef(p))];
p=2;
uit.Data = [uit.Data;L(p)];
uit.RowName = [uit.RowName;num2str(PRef(p))];
p=3;
uit.Data = [uit.Data;L(p)];
uit.RowName = [uit.RowName;num2str(PRef(p))];
p=4;
uit.Data = [uit.Data;L(p)];
uit.RowName = [uit.RowName;num2str(PRef(p))];
p=5;
uit.Data = [uit.Data;L(p)];
uit.RowName = [uit.RowName;num2str(PRef(p))];
More Answers (1)
Benjamin Kraus
on 4 Jun 2021
The code as it is currently written is creating a brand-new uitable every time the loop runs.
You need to update the code to either:
- Collect all the data, then create a uitable once with all the data.
- Create a uitable once at the beginning, then add data to the existing uitable.
Example of option 1:
ncols = 4;
nrows = 4;
colnames = cell(ncols,1);
rownames = cell(nrows,1);
data = cell(nrows,ncols);
for r = 1:nrows
for c = 1:ncols
rownames{r} = ['Row ' num2str(r)];
colnames{c} = ['Column ' num2str(c)];
data{r,c} = r*c;
end
end
uitable('ColumnName', colnames, 'RowName', rownames, 'Data', data)
Example of option 2:
tbl = uitable;
tbl.RowName = {};
tbl.ColumnName = {};
tbl.Data = {};
ncols = 4;
nrows = 4;
for r = 1:nrows
for c = 1:ncols
tbl.RowName{r} = ['Row ' num2str(r)];
tbl.ColumnName{c} = ['Column ' num2str(c)];
tbl.Data{r,c} = r*c;
end
end
The end result of both will be the same, but the first option is significantly more efficient.
See Also
Categories
Find more on Fluid Dynamics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!