Cell Array: show content and print name cell UITABLE - matlab

It is a basic problem but I am not so much experienced in Matlab(Guide).
What I have now is a cell array called Z with 21x2 elements: 21 rows 2 columns.
What I would like to do is to get only the first column (to show only 21x1).
Then, in this column there is a list of names. Inside the 21 rows of this cell there are repeated names. I would like to run through each row of this 21x1 column, detect which are repeated. The repeated ones should be printed in the uitable in a white colour.
Any ideas?

I believe this should deal with the core of your question:
A={'abc' 6;'de' 7;'abc' 8};
[C, ia] = unique(A(:,1));
idx = setdiff(1:size(A,1),ia);
A(idx,1)
This code will list all duplicates.

Related

Using cellfun for logical indexing

I have a cell data containing one row and four column. What i am trying to do is to go through elements in first column and see if an year '2018' is seen in the first column of each cell. When i perform this code, it will only give me the indices containing '2018' in the fourth cell. Not all the cells. Any help will be appreciated.
time = cellfun(#x, contains(x,'2018'),data{ii}(:,1))
I have created a small data sample as part of an example which I hope will help you solve the problem.
%Create cell array of strings.
data = {["2018","date","2018";"stack","cell2018","fun"],["data","stackoverflow","2018";"array","variable","data2018"]}
%Search for substring 2018 in the first column of every cell.
time = cellfun(#(x) contains(x(:,1),'2018'),data,'UniformOutput',0)
The output of time is a logical cell array:
>>time
{2×1 logical} {2×1 logical}
>>time{1}
1
0
>>time{2}
0
0
For the first cell, the column contains 2018 and stack, therefore 1 and 0 are returned.
For the second cell, the column contains data and array, therefore 0 and 0 are returned.
If you wish to find the indexes from the logical array, you can use the find function with outputs [row,col].

Combine elements of a cell into a matrix

I have a cell (called AA ) that contains 1 row * 36 columns (as shown in the attached image)
As we could see that each column in that cell is a matrix ( inside each column there are 1*3 data points )
I need to have an array that has 36 Rows * 3 columns in MATLAB
For example,
the first column in the Cell will be converted into 1 Row and three columns, the second column in the Cell will be converted in the same way and finally put them all of them together in order to generate an array that contains 36 Rows and 3 Columns
as shown in this example
-1.48247427405830e-15 0.185513882360673 -0.185513882360676
-9.59200039657764e-16 0.211729497802758 -0.211729497802760
3.69087930153418e-16 0.224791092084074 -0.224791092084073
You can just use cat to concatenate the rows and use {:} indexing to create a comma separated list
output = cat(1, AA{:})
In addition to the answer of the gentleman Suever these are 2 methods as well that can be used to solve the same question
output =cell2mat(AA(:))
or, more simply,
output = vertcat(AA{:})

Nested cells matlab

I have a 73 x 1 cell, each of those cells contains a 16 x 1 cell and each of those cells is an image. Is there an easy way I can convert this into one big array of cells containing only the images? Many thanks.
If C is your cell, use B = [C{:}] to create a 16×73 cell B with every column one of your original 16×1 cell elements. This works, because C{:} accesses every element in cell C and the brackets ([ ]) group all these elements into one array again. This is possible, because every element in C is of the same type and size.
Use B = B(:) to get a 1168×1 cell (73*16=1168), if you want. Either way, B{n} accesses the n-th image.

Concatenate and pull out data from cell array

I have a cell array called d_O3 of size 3x15.
Here's what the first row looks like:
10x29 cell
31x29 cell
40x29 cell
...
I want to turn column one inside each cell into a column array and vertically concatenate them all together.
Here's what I've done so far:
years = 1999:2013; % The 15 columns
m = 1; % Row 1. I'd like to be able to run a loop for the other rows to do the same thing, but I haven't figured out how.
for y = 1:numel(years)
data{y,1} = d_O3{m.y}(:,1);
end
This creates a 15x1 cell that looks like this inside:
31x1 cell
40x1 cell
42x1 cell
...
Inside each cell (i.e. inside 31x1), there's a column array of strings. But I want it to all concatenate together so it looks like:
06-029-0001-88101
06-073-0010-88101
...
In other words, I want to make vertically concatenate all the cells above.
I can do it by doing the following:
vertcat(data{1,1},data{2,1},...)
But that would mean typing out data{i,1} 15 times. What's an easier way?
vertcat(data{1:15,1})
or
vertcat(data{:,1})
It creates a comma separated list which is passed to vertcat.

find top n numeric cells in a cell array

Hi I have a cell array 2 x 1000. the first column holds numeric (double) values, the second holds a string. i would like the find all cells in the first column that are above a certain value, and bring back the corresponding cells in the second column. I have tried strcamp and various others but obviously they are for strings. I also tried doing
sortrows(mycell(1,:));
so i could pick the first 50 rows off or whateever, but this didn't seem to order the cell array. but really i would like to specifiy a threshold on the first column of the cell array.
How do I do this?
thanks.
If C is your cell array:
nums = [C{:,1}];
{:} converts C into a comma separated list (so {:,1} only converts the first column) and then [] collects the results into a normal array. After that it's simple:
index = nums > Threshold;
C(index,:)
OR in a one liner:
C([C{:,1}] > Threshold, :) %// Or C([C{:,1}] > Threshold, 2) as Luis said