Matlab combine cells and strings into a cell [duplicate] - matlab

I have created a function which takes vectors for input variables and returns a cell array for each set of inputs. The final output variable (out) seems to consist of a 2x1 cell containing two 1x5 cells. I have provided a screenshot of this below:
I am just trying to figure out how to flatten the cell array (out) to be a 2x5 cell array.

One way to achieve that would be -
vertcat(cell_array1{:})

If your cell has unequal number of elements in each row , maybe this might work better
vector=[cell_array{:}]

Related

Converting cells within cells to single cells

I have a 1X100 cell which contains exclusivly 1X24 cells. I need to extract these 100 cells and join them together to form a 100X24 cell, how can this be done?
I have been playing around with the 'cellfun' function and also using for loops to try an perform the operations required but without success. I understand I could just join these cells one by one but would prefer a more efficient approach. Any help would be appreciated.
The cell is generated from raw data using the following:
for i = 1:100
band{i} = prctile(e-data,i);
end
where e_data is a 62X24 double
The second input to prctile can be an array of percentages so your code can be replaced with
band = prctile(e - data, 1:100).';
This will create a 100 x 24 numeric array which is going to be more performant than a cell array.
In general though, if you need to concatenate the contents of multiple cells together, you can use {:} indexing to yield a comma separated list which can then be passed to cat
result = cat(1, band{:});
If I understood your purpose correctly, you need to use iscell() and retrieve what you want subsequently:
R=cellfun(#iscell, YourCell);
Demanded_Cell=YourCell(R);

How to create column cells, where each cell contains matrix

I have a function in Matlab that requires as the input
column cells, where each cell contains an SPD matrix
To be more precise this function requires 3 input arguments, the first two are column cells, where each cell contains an SPD matrix, But I don't know how to define a column cell in Matlab. I have tried this:
TestData(:,:,12) = T;
TestData is supposed to be my cell column and T is a matrix that should be in this column. for every matrix that I have, I put it in a variable called T and then using above command I add it to a 3D array. So the first matrix is in TestData(:,:,1), The second one is in TestData(:,:,1) and so on. When I run my function with TestData as the input variable i get this error:
Cell contents reference from a non-cell array object.
So I think I didn't define a cell column right.
A 3D array is not a cell. If you want each 3D slice of your 3D array to be a separate cell element, you could use num2cell followed by a call to squeeze to remove all singleton dimensions and make it an N x 1 cell array.
inputs = squeeze(num2cell(TestData, [1 2]));

strcat generates 1x1 cell in matlab

Consider the following snippet:
f=strcat(s,emotions{emotion},int2str(i),'\mean.points');
f1=strcat(s1,speakers(speaker),emotions{emotion},int2str(i),'\mean.points');
Here emotions and speakers are 1x7 and 1x4 arrays. The rest are strings and integers.
The type of f1 comes out to be 1x1 cell while f remains a string. What could be the difference between the two?
Since it comes out to be a 1x1 cell I can't use it for fopen() without using an index.
If any input is a cell array, combinedStr is a cell array of strings. Otherwise, combinedStr is a character array.
In f you concatenate just char-arrays, but in f1 obviously appears a cell array speakers(speaker).
So just use speakers{speaker} also, and it should work.
With () you are indexing the cell array, therefore you get a cell element. With {} you are addressing the content of the specified cell.

creating a new matrix from a cell array after evaluating one column.

I have a cell array of 447*1 Dimensions. The cell array has 2Dimensional arrays of different dimensions of type double. I want to check a particular value in that cell array compare and on that basis store it in a new Matrix.
So for example my my starting cell array is Y{447*1} . My first cell contains an array of
5*10 and second array contains data of 22*10 . I want to evaluate the second column
of this array and then store it in a new Matrix.
I did this for one set of data and the code looks something like this.
A = [y{2,1}(1:20,2),y{4,1}(1:20,2),y{6,1}(1:20,2),y{8,1}(1:20,2),...
y{10,1}(1:20,2),y{12,1}(1:20,2),y{14,1}(1:20,2),y{16,1}(1:20,2),...
y{18,1}(1:20,2),y{20,1}(1:20,2),y{22,1}(1:20,2),y{24,1}(1:20,2),...
y{26,1}(1:20,2),y{28,1}(1:20,2),y{30,1}(1:20,2)];
But I want to automate the thing. Please help how this can be done.
Something along the lines of:
Temp = cellfun(#(x) x(1:20,2),Y(1:2:end,1), 'UniformOutput', false);
A = cat(2,Temp{:});
Should work if I am reading your question right - it should replicate your example anyway.
You can then change the dimensions of the #(x) function x(1:20,2) to take out different values from your cell array, and use different cell indexing for Y(:,1) to pick different parts of Y.

Searching in a cell for a specific value

I have a cell of dimension 100x1 in matlab. I want to search this cell for a particular string so that it returns me the indexes where the value is that particular string. How can I accomplish this?
With strfind().
For example,
strfind({'aa','bb','cc'},'aa')