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.
Related
I'd like to understand why the following code works:
close all
clear all
t=[0:0.1:10];
x=figure(1);
plot(t,t.^2)
a=getframe(gcf);
b{1}=frame2im(a);
instead the following code does not work:
close all
clear all
t=[0:0.1:10];
x=figure(1);
plot(t,t.^2)
a=getframe(gcf);
b(1)=frame2im(a);
If I use "b(1)=x;" it works.
Thank you very much.
In an array, you can store only one 1x1 value of any class at a single index but the class of all elements of an array must be same. In a cell array, there is no such restriction.
frame2im(a) is [525x700x3 uint8] and hence you can store it in a cell and not in a simple array if you want to store it at a single index.
b(1)=x; works because x is 1x1 matlab.ui.Figure. You can also store x in a cell array.
To my understanding, you need to know what cells are meant for in MATLAB. If you happen to know Python, you probably will think in a "list"-type way. MATLAB cell can store numbers, strings, etc. However its array is meant to store numbers.
That's why your structure from fram2im can't work.
In MATLAB, the i-th element of an array is accessed by a(i), while the i-th element of a cell is accessed by a{i}. So my code has to do different things in terms of whether a is a cell or not.
Is there a better way to do it? so we can access the i-th element in a 'same'? way.
You can access the ith element of any array in the same way, a(i), and the behaviour is completely uniform: you get back an object of the same class as a. So if a is a 5x5 double array, a(i) is a 1x1 double. Similarly, if a is a cell array, then a(i) is a 1x1 cell. So far, so logical and consistent.
Now, if you want to look inside a cell, you use a{i} but that is a fundamentally different operation. Your question seems to imply that it would be "better" if these two fundamentally different operations had the same syntax. It wouldn't: if this were the case, then the ability to slice a cell array into arbitrary shapes (including 1x1) would be overshadowed.
But you can always write a custom function. Among the many Matlab workarounds I carry with me everywhere, I have the following pair of functions:
function a = ascell(a)
if ~iscell(a), a = {a}; end
and
function a = uncell(a)
if iscell(a) & numel(a) == 1, a = a{1}; end
With uncell.m on your path, b = uncell(a(i)); would give you the ith element of a with the cell wrapping, if any, stripped off.
It is good to have the call to uncell visible in the code because it alerts you (or another maintainer) to the possibility that a might legally be a cell or a non-cell—this is by no means necessarily true in everybody's coding strategy. Nor will my code necessarily follow the same convention as yours when it comes to interpreting the meaning and correct treatment of a cell array where a non-cell was expected, or a non-cell where a cell was expected (and this is another way of explaining why there's no common syntax). This leads me to the question: if the design of your application is such that a can by its nature contain elements with mismatched shapes or types, then why not simply decree that it is always a cell, never a non-cell, and always access the ith element as a{i}?
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]));
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{:}]
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.