Difference between accessing cell elements using curly braces and parentheses - matlab

What is the difference between accessing elements in a cell array using parentheses () and curly braces {}?
For example, I tried to use cell{4} = [] and cell(4) = []. In the first case it sets the 4th element to [], but in the second case it wiped out the cell element, that is, reduced the cell element count by 1.

Think of cell array as a regular homogenic array, whose elements are all cells. Parentheses (()) simply access the cell wrapper object, while accessing elements using curly bracers ({}) gives the actual object contained within the cell.
For example,
A={ [5,6], 0 , 0 ,0 };
Will look like this:
The syntax of making an element equal to [] with parentheses is actually a request to delete that element, so when you ask to do foo(i) = [] you remove the i-th cell. It is not an assignment operation, but rather a RemoveElement operation, which uses similar syntax to assignment.
However, when you do foo{i} = [] you are assigning to the i-th cell a new value (which is an empty array), thus clearing the contents of that cell.

See the help in this link. As you'll see, accessing with parentheses (), gives you a subset of a cell (i.e. a sub-cell), while curly braces {} gives you the content of the cell you are trying to access.

Related

How to empty a cell

I have a cell array that contains a lot of NaN. But for whatever reason the isnan function can't detect them (hence this doesn't work cellfun(#(Iarray) any(isnan(Iarray)),Iarray);) so I figured it was actually strings that contains NaN.
I perform two things on this array : cleaning empty rows and columns and removing NaN (well trying to).
So I want to replace all the NaN by empty cells and then perform to clean all empty cells with the isempty function. I'll use a loop and if char(x(i,j))=='NaN'.
So here comes my problem I want to empty a cell and then detect that cell with the isempty function but I have no idea how. I have tried x(1,2)= [], x(1,2)= {[]}, x(1,2)='' but none of those gives a 1 for isempty(x(1,2)) for example.
Does anyone know how to solve my problem?
Thanks in advance!
If you want to empty the content of a cell you can use :
x{1,2} = [];
There is a difference between indexing using parentheses () and brackets {}. You can think a cell array as an array of cells that each cell contains a value such as 1 ,2 , []. When a cell is indexed with parentheses it returns the result as cell (or more precisely as an array of type cell) but when it is indexed with brackets it returns the content of the cell (or more precisely as a comma separated list containing the contents of the indexed cells). So when you write such an expression:
x(1,2) = [];
It removes the second element from the array of cells and behaves like indexing other array types. For example when you want to remove the second element of a = [1 2 3] you can use a(2)=[].
But when you write x{1,2} = []; it accesses the content of the cell and sets it to a null array [0 x 0] of type double that is empty.
Likewise a={} is a [0 x 0] null array of cells and b={[]} is an [1 x 1] array of cells that its first element contains a null array [0 x 0] of type double. When you use isempty(b) it returns false because it contains an element and when you use isempty(b(1)) it returns false because b(1) returns an array of cells that contains an element but when you use isempty(b{1}) it returns true because the {} operator extracts the contents of the first cell that is a null array.
In short, cells can be accessed using both () and {}, and based on the situation [] has different functionalities: a) removing element b) null array.

Iterating Over Unique Values in Matlab

I've been trying to follow this answer in order to obtain unique strings from a given cell array. However, I'm running into trouble when iterating over these values. I have tried for loops as follows:
[unique_words, ~, occurrences] = unique(words);
unique_counts = hist(occurrences, 1:max(occurrences));
for a=1:numel(unique_words)
word = unique_words{a}
count = unique_counts{a}
result = result + a_struct.(unique_words{a}) + unique_counts{a}
end
When trying to reference the items like this, I receive the error:
Cell contents reference from a non-cell array object.
Changing the curly brackets to round brackets for unique_couts yields the error:
Reference to non-existent field 'N1'.
Changing both unique_words and unique_counts to round brackets yields:
Argument to dynamic structure reference must evaluate to a valid field name.
How am I to iterate over the results of unique?
unique_words is a cell array. unique_counts is a vector. So unique_words should be accessed using curly brackets and unique_counts using round ones. The error that you are getting in this case is related to the a_struct (which is not defined in the question) not having the corresponding field, not the access method.

How to remove a particular set of cells from a cell array?

Suppose I have an array with 10 cells: C{1},C{2},...,C{10}, and let b=[1 2 8], then I want to empty C{1}, C{2} and C{8}, so I tried C{b}=[], but it does't work. Is there any easy way to do it?
C(b) = [] will do it. The {} notation is for addressing the contents of a cell rather than the cell as a member of the array. If you get more than one cell back from the {} notation it returns as a comma separated list which you can't use for indexing. The () notation will get you an index to the actual cells themselves and is thus the correct way to do this.

Undefined function 'eq' for input arguments of type 'cell'

I tried to make a function that generates a number of strings.
function [p] = GetPattern (v)
load('code128B.mat')
for a=1:length(code128B)
if v == code128B(a,1)
p=code128B{a,3};
end
end
code128B.mat contains data, first column are numbers while third column are strings. I want to input numbers and produce a string.
why this function produce an error: Undefined function 'eq' for input arguments of type 'cell'.? I don't get it.
Thanks for any help.
For cell arrays, curly braces ({}) are used to extract the contents of the cells, while parentheses (()) are used to extract a subset of the cells (that is, the result is also a cell array).
Use code128B{a,1} instead of code128B(a,1) to get the number instead of a cell containing the number. However, if v is also a cell then you have to use isequal to compare their contents.

If a MATLAB function returns a variable number of values, how can I get all of them as a cell array?

I am writing a function to remove some values from a cell array, like so:
function left = remove(cells, item);
left = cells{cellfun(#(i) ~isequal(item, i), cells)};
But when I run this, left has only the first value, as the call to cells{} with a logical array returns all of the matching cells as separate values. How do I group these separate return values into a single cell array?
Also, perhaps there is already a way to remove a given item from a cell array? I could not find it in the documentation.
You have to use () instead of {} to index the cells:
function left = remove(cells, item)
left = cells(cellfun(#(i) ~isequal(item, i), cells));
Using () for indexing will give you a subset of cells, while using {} will return the contents of a subset of cells as a comma-separated list, and only the first entry of that list will get placed in left in your example.
You can check out this MATLAB documentation for more information on using cell arrays.
EDIT: Response to comment...
If you have an operation that ends up giving you a comma-separated list, you can place the individual elements of the list into cells of a cell array by surrounding the operation with curly braces. For your example, you could do:
left = {cells{cellfun(#(i) ~isequal(item, i), cells)}};
The inner set of curly braces creates a comma-separated list of the contents of cells that are not equal to item, and the outer set then collects this list into a cell array. This will, of course, give the same result as just using parentheses for the indexing, which is the more sensible approach in this case.
If you have a function that returns multiple output arguments, and you want to collect these multiple values into a cell array, then it's a bit more complicated. You first have to decide how many output arguments you will get, or you can use the function NARGOUT to get all possible outputs:
nOut = 3; %# Get the first three output arguments
%# Or...
nOut = nargout(#some_fcn); %# Get all the output arguments from some_fcn
Then you can collect the outputs into a 1-by-nOut cell array outArgs by doing the following:
[outArgs{1:nOut}] = some_fcn(...);
It should be noted that NARGOUT will return a negative value if the function has a variable number of output arguments, so you will have to choose the value for nOut yourself in such a case.