Manipulating cells and nested cells - matlab

I have P being a 17x1 cell, each cell representing a subject. Each of the 17 cells within P is a 3x1 cell, representing contrast images for each subject. So basically there are 17 subjects, 3 contrast images per subject. So to index the 3rd contrast images of the 14th subject, I would do P{14,1}{3,1}. However, I would like to turn P into 3*17 x 1 cell (or 51x1 cell) instead. This means there are no nested cells within each cell in P. So P would be something like this:
Subject1/contrast1.img
Subject1/contrast2.img
Subject1/contrast3.img
Subject2/contrast1.img
Subject2/contrast2.img
Subject3/contrast3.img
...
Subject17/contrast3.img
Could anyone tell me how this may be accomplished?

How about a oneliner?
a = {{1;2};{3;4};{5;6};{7;8}}
b = vertcat(a{:})

Related

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.

subindex into a cell array of strings

I have a 6 x 3 cell (called strat) where the first two columns contain text, the last column has either 1 or 2.
I want to take a subset of this cell array. Basically select only the rows where the last column has a 1 in it.
I tried the following,
ff = strat(strat(:, 3), 1:2) == 1;
The error message is,
Function 'subsindex' is not defined for values of class 'cell'.
How can I index into a cell array?
Cell arrays are accessed through braces {} instead of parentheses (). Then, as a 2nd subtlety, when pulling values out of a cell arrays, you need to gather them...for numerics you gather them into regular arrays using [] and for strings you gather them into a new cell array using {}. Confusing, eh?
ff = { strat{ [strat{:,3}]==1 , 1:2 } };
Gathering into cell arrays this way can often give the wrong shape when you're done. So, you might try something like this
ind = find([strat{:,3}]==1); %find the relevant indices
ff = {{strat{ind,1}; strat{ind,2}}'; %this will probably give you the right shape

Cell Array: show content and print name cell UITABLE

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.

Accessing the contents of a 1x1 matlab cell

I'm not sure about the terminology, but I have read data from a text file into a 1x1 cell array P. When examining P, it lists "<142x2 cell>" in the (1,1) position. From there I can double-click this and it opens up into the 142x2 cell that I actually want. The issue is, I don't get how to manipulate this data via code to convert from the 1x1 cell array to the 142x2 cell array.
Also, I cannot find anywhere what the curly brackets denote.
I don't get how to manipulate this data via code to convert from the 1x1 cell array to the 142x2 cell array.
The cell array P is actually a 1x1 cell array, which in turn contains another cell array 142x2. This type of output is very common when using textscan. To access the inner cell array, you can simply use curly braces ({}), like so:
Q = P{1}; // or P{:} if you're certain that P holds only one cell
The resulting Q should hold your 142x2 cell array. I usually "flatten" P by doing P = P{:}, without using an intermediate variable.
Also, I cannot find anywhere what the curly brackets denote.
Have you read MATLAB's documentation about special characters? Here's what it says:
Curly braces are used in cell array assignment statements. For example, A(2,1) = {[1 2 3; 4 5 6]}, or A{2,2} = ('str'). See help paren for more information about { }.
I would also urge you to read the following (very) related question: Difference between accessing cell elements using {} and () (curly braces vs. parentheses)
Short answer: You can assign the content of the first cell in P to P.
Example:
P = {cell(142,2)}; %Create a 142x2 cell inside a cell
P = P{1}; %Solution: Now P is a 142x2 cell
If you try help cell it will lead you to help paren that explains the use of curly brackets.