Unable to use a value of type cell as an index
Show older comments
I am working on a cell merge, the following is my idea:
1. Set the logical value of all rows to 1.
2. Start from the first row with a logical value of 1, and traverse all subsequent rows with a logical value of 1.
3. If the first cell of the traversed row is exactly the same as the first cell of this row, merge all the columns of the two rows.
4. After the merge is complete, set the logical value of the merged row to 0.
5. Continue to traverse the lines after merging until the end of the traversal.
6. Finally, jump to the second step to continue to find the next row with a logical value of 1 and traverse all the rows with a logical value of 1 after this row and merge them.
7. When all traversals are over, extract all rows with a logical value of 1 and combine them into a new mxn cell.
The following is my code, but the following problem occurs when setting the logical value to the row of the cell: Unable to use a value of type cell as an index.
Please tell me how to modify the code to achieve the effect of encoding the logical value of the row of the cell, thanks
f = true(1, length(bbox_cell));
for i=1:length(bbox_cell)
if f(bbox_cell(i))
a = bbox_cell{i,1};
for j=i+1:length(bbox_cell)
if f(bbox_cell(j))
strcmp(a, bbox_cell{j,1});
if 1
bbox_cell{i,1} = [bbox_cell{i,1}];
bbox_cell{i,2} = [bbox_cell{i,2}; bbox_cell{j,2}];
bbox_cell{i,3} = [bbox_cell{i,3}; bbox_cell{j,3}];
f(bbox_cell(j)) = false;
end
end
end
end
end
bbox_cell = bbox_cell(f)
And here is my cell for reference:

5 Comments
langrg
on 10 May 2022
Hi,
Not sure to understand what you are trying to do.
Could you show an exemple of what you would like to have after you run your code?
GL.
dpb
on 10 May 2022
Well, w/o the full error message, we don't even know which line of code is the offending one...
You could optimize the above logic quite a lot by using logical indexing -- you can find all matching cell content in one call.
You could also help a bunch in the search if you were to sortrows() on the basis of the first cell; then all rows until there's a difference between two cells are a match without any searching. Turning that cell into a categorical variable would aid in that distinction.
Jiongyi Meng
on 10 May 2022
Fangjun Jiang
on 10 May 2022
Hard to understand your question, but it should be easy to debug this problem.
Assign i=1, show the value of bbox_cell(i), then show the value of f(bbox_cell(i))
f is a logical array, f(1) and f(2) etc. are all true. But your data show that bbox_cell(i) is nothing like 1, 2, 3,etc. You can't use bbox_cell(i) to index a value in the array of f.
That is what the error message says. You are trying to use a value of type cell as an index, which you can't do.
dpb
on 10 May 2022
Well, yes, that's the problem statement you gave before -- all the comments on "how" I made above are still valid and would simplify the code significantly if you were to use them.
As for the latter, you need
if f(bbox_cell{i})
probably, but as the other poster says, the code is convoluted enough I didn't really even try to read it...if you have this cell dereferencing issue there, you'll probably find you've got more of the same elsewhere.
Accepted Answer
More Answers (0)
Categories
Find more on Language Support 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!