Delete selected rows in uitable - Matlab
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I built in GUI (using GUIDE) a uitable (4x5) that last row is logical so I can select lines to delete.
d = 'L1',1,10,true;'L2',2,20,true;'L3',3,30,false;'L4',4,40,true;'L4',5,50,false;
set(handles.outputTable,'Data',d)
I created a button to delete the selectd rows but I does not work:
function deleteButton_Callback(hObject, eventdata, handles)
% hObject handle to deleteButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
dataTable = (get(handles.outputTable,'data'));
[m n] = size(dataTable);
disp(dataTable);
for i = 1:m
if num2str(cell2mat(dataTable(i,4))) =='1'
dataTable(i,:)=;
end
end
disp('Modifed table')
disp(dataTable);
How can I fix it so I get set the table again in the GUI?
matlab
add a comment |
I built in GUI (using GUIDE) a uitable (4x5) that last row is logical so I can select lines to delete.
d = 'L1',1,10,true;'L2',2,20,true;'L3',3,30,false;'L4',4,40,true;'L4',5,50,false;
set(handles.outputTable,'Data',d)
I created a button to delete the selectd rows but I does not work:
function deleteButton_Callback(hObject, eventdata, handles)
% hObject handle to deleteButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
dataTable = (get(handles.outputTable,'data'));
[m n] = size(dataTable);
disp(dataTable);
for i = 1:m
if num2str(cell2mat(dataTable(i,4))) =='1'
dataTable(i,:)=;
end
end
disp('Modifed table')
disp(dataTable);
How can I fix it so I get set the table again in the GUI?
matlab
(1) You first get thedata
from theuitable
in your variabledataTable
=>ok. Then (2) you modify thedataTable
according to your needs => ok. Now you just need to complete the process: (3) push yourdataTable
back into theuitable
, something likeset(handles.outputTable,'data',dataTable)
.
– Hoki
Nov 16 '18 at 14:18
I get an error with the loop, the rows are not deleted. Any idea why?
– wrek
Nov 16 '18 at 14:28
you don't neednum2str
andcell2mat
to just check for a boolean value in a cell array. You could try just checking forif dataTablei,4 ; dataTable(i,:)=; end
. Note the use of the curly braces:...
instead of the parenthesis, when you want to retrieve the content of a givencell
.
– Hoki
Nov 16 '18 at 14:42
add a comment |
I built in GUI (using GUIDE) a uitable (4x5) that last row is logical so I can select lines to delete.
d = 'L1',1,10,true;'L2',2,20,true;'L3',3,30,false;'L4',4,40,true;'L4',5,50,false;
set(handles.outputTable,'Data',d)
I created a button to delete the selectd rows but I does not work:
function deleteButton_Callback(hObject, eventdata, handles)
% hObject handle to deleteButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
dataTable = (get(handles.outputTable,'data'));
[m n] = size(dataTable);
disp(dataTable);
for i = 1:m
if num2str(cell2mat(dataTable(i,4))) =='1'
dataTable(i,:)=;
end
end
disp('Modifed table')
disp(dataTable);
How can I fix it so I get set the table again in the GUI?
matlab
I built in GUI (using GUIDE) a uitable (4x5) that last row is logical so I can select lines to delete.
d = 'L1',1,10,true;'L2',2,20,true;'L3',3,30,false;'L4',4,40,true;'L4',5,50,false;
set(handles.outputTable,'Data',d)
I created a button to delete the selectd rows but I does not work:
function deleteButton_Callback(hObject, eventdata, handles)
% hObject handle to deleteButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
dataTable = (get(handles.outputTable,'data'));
[m n] = size(dataTable);
disp(dataTable);
for i = 1:m
if num2str(cell2mat(dataTable(i,4))) =='1'
dataTable(i,:)=;
end
end
disp('Modifed table')
disp(dataTable);
How can I fix it so I get set the table again in the GUI?
matlab
matlab
asked Nov 16 '18 at 13:45
wrekwrek
1148
1148
(1) You first get thedata
from theuitable
in your variabledataTable
=>ok. Then (2) you modify thedataTable
according to your needs => ok. Now you just need to complete the process: (3) push yourdataTable
back into theuitable
, something likeset(handles.outputTable,'data',dataTable)
.
– Hoki
Nov 16 '18 at 14:18
I get an error with the loop, the rows are not deleted. Any idea why?
– wrek
Nov 16 '18 at 14:28
you don't neednum2str
andcell2mat
to just check for a boolean value in a cell array. You could try just checking forif dataTablei,4 ; dataTable(i,:)=; end
. Note the use of the curly braces:...
instead of the parenthesis, when you want to retrieve the content of a givencell
.
– Hoki
Nov 16 '18 at 14:42
add a comment |
(1) You first get thedata
from theuitable
in your variabledataTable
=>ok. Then (2) you modify thedataTable
according to your needs => ok. Now you just need to complete the process: (3) push yourdataTable
back into theuitable
, something likeset(handles.outputTable,'data',dataTable)
.
– Hoki
Nov 16 '18 at 14:18
I get an error with the loop, the rows are not deleted. Any idea why?
– wrek
Nov 16 '18 at 14:28
you don't neednum2str
andcell2mat
to just check for a boolean value in a cell array. You could try just checking forif dataTablei,4 ; dataTable(i,:)=; end
. Note the use of the curly braces:...
instead of the parenthesis, when you want to retrieve the content of a givencell
.
– Hoki
Nov 16 '18 at 14:42
(1) You first get the
data
from the uitable
in your variable dataTable
=>ok. Then (2) you modify the dataTable
according to your needs => ok. Now you just need to complete the process: (3) push your dataTable
back into the uitable
, something like set(handles.outputTable,'data',dataTable)
.– Hoki
Nov 16 '18 at 14:18
(1) You first get the
data
from the uitable
in your variable dataTable
=>ok. Then (2) you modify the dataTable
according to your needs => ok. Now you just need to complete the process: (3) push your dataTable
back into the uitable
, something like set(handles.outputTable,'data',dataTable)
.– Hoki
Nov 16 '18 at 14:18
I get an error with the loop, the rows are not deleted. Any idea why?
– wrek
Nov 16 '18 at 14:28
I get an error with the loop, the rows are not deleted. Any idea why?
– wrek
Nov 16 '18 at 14:28
you don't need
num2str
and cell2mat
to just check for a boolean value in a cell array. You could try just checking for if dataTablei,4 ; dataTable(i,:)=; end
. Note the use of the curly braces: ...
instead of the parenthesis, when you want to retrieve the content of a given cell
.– Hoki
Nov 16 '18 at 14:42
you don't need
num2str
and cell2mat
to just check for a boolean value in a cell array. You could try just checking for if dataTablei,4 ; dataTable(i,:)=; end
. Note the use of the curly braces: ...
instead of the parenthesis, when you want to retrieve the content of a given cell
.– Hoki
Nov 16 '18 at 14:42
add a comment |
1 Answer
1
active
oldest
votes
This is wrong:
for i = 1:m
if num2str(cell2mat(dataTable(i,4))) =='1'
dataTable(i,:)=;
end
end
First of all, if num2str(cell2mat(dataTable(i,4))) =='1'
is a convoluted equivalent of if dataTablei,4==1
. You should learn to use curly braces to access the content of a cell array.
Then, it will work only if the counter is decreased.
See what happens:
Test if row n should be deleted
Delete line n; the content of row (n+1) have now moved to row n
Increment counter i from value n to n+1
The row now at position n has never been tested for deletion !
What was at row (n+1) is never tested, since the delete operation moves it backwards first, then the counter is incremented without testing again. The solution is to decrement the counter.
for i = m:-1:1
if dataTablei,4
dataTable(i,:)=;
end
end
The rows moved by the deletion operation have already been tested, so in the end it is certain that all lines will have been tested.
Now, the same can be obtained in a vectorized form with a single line:
dataTable = dataTable(cell2mat(dataTable(:,4))==0,:);
The whole function boils down to:
function deleteButton_Callback(hObject, eventdata, handles)
dataTable = get(handles.outputTable,'data');
% Do some checks to make sure that the values input by are correct %
assert(all(cellfun(@isscalar,dataTable(:,4))), 'Last colum should contain scalars!');
set(handles.outputTable,'data' , dataTable(cell2mat(dataTable(:,4))==0,:));
end
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339097%2fdelete-selected-rows-in-uitable-matlab%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This is wrong:
for i = 1:m
if num2str(cell2mat(dataTable(i,4))) =='1'
dataTable(i,:)=;
end
end
First of all, if num2str(cell2mat(dataTable(i,4))) =='1'
is a convoluted equivalent of if dataTablei,4==1
. You should learn to use curly braces to access the content of a cell array.
Then, it will work only if the counter is decreased.
See what happens:
Test if row n should be deleted
Delete line n; the content of row (n+1) have now moved to row n
Increment counter i from value n to n+1
The row now at position n has never been tested for deletion !
What was at row (n+1) is never tested, since the delete operation moves it backwards first, then the counter is incremented without testing again. The solution is to decrement the counter.
for i = m:-1:1
if dataTablei,4
dataTable(i,:)=;
end
end
The rows moved by the deletion operation have already been tested, so in the end it is certain that all lines will have been tested.
Now, the same can be obtained in a vectorized form with a single line:
dataTable = dataTable(cell2mat(dataTable(:,4))==0,:);
The whole function boils down to:
function deleteButton_Callback(hObject, eventdata, handles)
dataTable = get(handles.outputTable,'data');
% Do some checks to make sure that the values input by are correct %
assert(all(cellfun(@isscalar,dataTable(:,4))), 'Last colum should contain scalars!');
set(handles.outputTable,'data' , dataTable(cell2mat(dataTable(:,4))==0,:));
end
add a comment |
This is wrong:
for i = 1:m
if num2str(cell2mat(dataTable(i,4))) =='1'
dataTable(i,:)=;
end
end
First of all, if num2str(cell2mat(dataTable(i,4))) =='1'
is a convoluted equivalent of if dataTablei,4==1
. You should learn to use curly braces to access the content of a cell array.
Then, it will work only if the counter is decreased.
See what happens:
Test if row n should be deleted
Delete line n; the content of row (n+1) have now moved to row n
Increment counter i from value n to n+1
The row now at position n has never been tested for deletion !
What was at row (n+1) is never tested, since the delete operation moves it backwards first, then the counter is incremented without testing again. The solution is to decrement the counter.
for i = m:-1:1
if dataTablei,4
dataTable(i,:)=;
end
end
The rows moved by the deletion operation have already been tested, so in the end it is certain that all lines will have been tested.
Now, the same can be obtained in a vectorized form with a single line:
dataTable = dataTable(cell2mat(dataTable(:,4))==0,:);
The whole function boils down to:
function deleteButton_Callback(hObject, eventdata, handles)
dataTable = get(handles.outputTable,'data');
% Do some checks to make sure that the values input by are correct %
assert(all(cellfun(@isscalar,dataTable(:,4))), 'Last colum should contain scalars!');
set(handles.outputTable,'data' , dataTable(cell2mat(dataTable(:,4))==0,:));
end
add a comment |
This is wrong:
for i = 1:m
if num2str(cell2mat(dataTable(i,4))) =='1'
dataTable(i,:)=;
end
end
First of all, if num2str(cell2mat(dataTable(i,4))) =='1'
is a convoluted equivalent of if dataTablei,4==1
. You should learn to use curly braces to access the content of a cell array.
Then, it will work only if the counter is decreased.
See what happens:
Test if row n should be deleted
Delete line n; the content of row (n+1) have now moved to row n
Increment counter i from value n to n+1
The row now at position n has never been tested for deletion !
What was at row (n+1) is never tested, since the delete operation moves it backwards first, then the counter is incremented without testing again. The solution is to decrement the counter.
for i = m:-1:1
if dataTablei,4
dataTable(i,:)=;
end
end
The rows moved by the deletion operation have already been tested, so in the end it is certain that all lines will have been tested.
Now, the same can be obtained in a vectorized form with a single line:
dataTable = dataTable(cell2mat(dataTable(:,4))==0,:);
The whole function boils down to:
function deleteButton_Callback(hObject, eventdata, handles)
dataTable = get(handles.outputTable,'data');
% Do some checks to make sure that the values input by are correct %
assert(all(cellfun(@isscalar,dataTable(:,4))), 'Last colum should contain scalars!');
set(handles.outputTable,'data' , dataTable(cell2mat(dataTable(:,4))==0,:));
end
This is wrong:
for i = 1:m
if num2str(cell2mat(dataTable(i,4))) =='1'
dataTable(i,:)=;
end
end
First of all, if num2str(cell2mat(dataTable(i,4))) =='1'
is a convoluted equivalent of if dataTablei,4==1
. You should learn to use curly braces to access the content of a cell array.
Then, it will work only if the counter is decreased.
See what happens:
Test if row n should be deleted
Delete line n; the content of row (n+1) have now moved to row n
Increment counter i from value n to n+1
The row now at position n has never been tested for deletion !
What was at row (n+1) is never tested, since the delete operation moves it backwards first, then the counter is incremented without testing again. The solution is to decrement the counter.
for i = m:-1:1
if dataTablei,4
dataTable(i,:)=;
end
end
The rows moved by the deletion operation have already been tested, so in the end it is certain that all lines will have been tested.
Now, the same can be obtained in a vectorized form with a single line:
dataTable = dataTable(cell2mat(dataTable(:,4))==0,:);
The whole function boils down to:
function deleteButton_Callback(hObject, eventdata, handles)
dataTable = get(handles.outputTable,'data');
% Do some checks to make sure that the values input by are correct %
assert(all(cellfun(@isscalar,dataTable(:,4))), 'Last colum should contain scalars!');
set(handles.outputTable,'data' , dataTable(cell2mat(dataTable(:,4))==0,:));
end
answered Nov 16 '18 at 15:25
BriceBrice
1,416110
1,416110
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339097%2fdelete-selected-rows-in-uitable-matlab%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
(1) You first get the
data
from theuitable
in your variabledataTable
=>ok. Then (2) you modify thedataTable
according to your needs => ok. Now you just need to complete the process: (3) push yourdataTable
back into theuitable
, something likeset(handles.outputTable,'data',dataTable)
.– Hoki
Nov 16 '18 at 14:18
I get an error with the loop, the rows are not deleted. Any idea why?
– wrek
Nov 16 '18 at 14:28
you don't need
num2str
andcell2mat
to just check for a boolean value in a cell array. You could try just checking forif dataTablei,4 ; dataTable(i,:)=; end
. Note the use of the curly braces:...
instead of the parenthesis, when you want to retrieve the content of a givencell
.– Hoki
Nov 16 '18 at 14:42