How to properly delete cell element ?

조회 수: 787 (최근 30일)
RuiQi
RuiQi 2016년 6월 27일
댓글: Josep Llobet 2022년 8월 3일
I have a 1x12 cell. How do i remove the last element in the cell ? I tried cell{end} = [] but it did not work. It only emptied the last cell. I would like the result to be a 1x11 cell

채택된 답변

goerk
goerk 2016년 6월 27일
use normal brackets
cell(end) = [];
  댓글 수: 1
Guillaume
Guillaume 2016년 6월 27일
The reason being:
  • curly brackets act on the content of a cell(s). It does not affect the container (the cell array) itself, so c{end} refers to what's in the last cell, and therefore c{end} = [] puts an empty matrix in that last cell.
  • round brackets act on the cell array itself. So c(end) refers to the last cell, and c(end) = [] deletes it.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Ceethal Kottakali Piyus
Ceethal Kottakali Piyus 2022년 1월 20일
You have to use round parentheses instead of curly braces (which act on the inner cell values and not on the cells themselves):
cell(end) = [];
  댓글 수: 1
Tong Zhao
Tong Zhao 2022년 6월 15일
편집: Tong Zhao 2022년 6월 15일
Hi Ceethal,
What if I want to remove the second row of a 2x2 cell array?
cell_arr = {1,2;3,4}
cell_arr = 2×2 cell array
{[1]} {[2]} {[3]} {[4]}
cell_arr(2,:) = []
cell_arr = 1×2 cell array
{[1]} {[2]}
Edit: I just found the expression myself. But I'd like to keep this thread for anyone who's running into the same problem.

댓글을 달려면 로그인하십시오.


Amir Hosein Asaadi
Amir Hosein Asaadi 2021년 9월 27일
I sove it by creating variable again like this:
cell_var = {'var1','var2'};
cell_var = {'var1'};
I hope this work for you.

Josep Llobet
Josep Llobet 2022년 8월 2일
% Cell you want to copy
branques_pixels_new = {[55178] [55593] [62271] [62686] [66858] [67273 123123 12123]}
% element you want to eliminate
n_elm_borrar = 3;
% New cell with the non- element
branques_pixels_new_2 = {};
% Operation
for n_elm = 1:length(branques_pixels_new)
if n_elm ~= n_elm_borrar
branques_pixels_new_2{end+1} = branques_pixels_new{n_elm};
end
end
% New cell
branques_pixels_new_2
  댓글 수: 2
Stephen23
Stephen23 2022년 8월 2일
The simple and efficient MATLAB approach:
branques_pixels_new_2 = branques_pixels_new;
branques_pixels_new_2(n_elm_borrar) = [];
Josep Llobet
Josep Llobet 2022년 8월 3일
That's true, it's better that one

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by