MATLAB - Cell array to a cell - matlab

I want to create a {1,2} cell array, that will contain single cell value in {1,1} and several cells in {1,2}{:,1} row:
TEXT = {'-1';'432';'a';'c';'v';'-1';'-1';'14';'b';'n';'-1';'-1';'44';'m';'t';'r';'-1';'-1';'55';'o';'i';'u';'-1'};
Sections{1,1} = TEXT{5};
Sections{1,2}{:,1} = TEXT{3:10,1};
,but I get an ERROR:
The left hand side is initialized and has an empty range of indices.
However, the right hand side returned one or more results.
Error in fff (line 4)
Sections{1,2}{:,1} = TEXT{3:10,1}; % Text in the section
Ideas?

Just use () instead of {} for the sections in (n,2):
TEXT = {'-1';'432';'a';'c';'v';'-1';'-1';'14';'b';'n';'-1';'-1';'44';'m';'t';'r';'-1';'-1';'55';'o';'i';'u';'-1'};
Sections{1,1} = TEXT{5};
Sections{1,2} = TEXT(3:10,1);

Related

Matlab: Conversion from cell to double

I'm trying to store the color values given by the impixel function into a matrix or an array of some sort.
B = cell(301, 51);
for R = 200: 500
for C = 175 : 225
B(R-199,C-174) = impixel(I,R,C);
end
end
I created a cell array to hold the values, but I keep getting the following error:
"Conversion to cell from double is not possible."
Where is my error? Thanks!
Looking at the documentation of impixel, it states that its outputs are all either of class double or single.
In your code, you define B as a cell array. There is no problem storing the output of impixel in B. However, if you index it with the parenthesis (), it expects the value assigned to also be a cell. You want to assign the output of impixel to a particular element of B, and need to use the curly braces {} to refer to the element. I believe changing your code to
B{R-199,C-174} = impixel(I,R,C);
may solve your issue.
The error should be in this line:
B(R-199,C-174) = impixel(I,R,C);
impixel in this case returned a double type, while B is a cell type.

Storing variable names in a struct

I am dealing with a rather large optimization problem (using MOSEK). To remind myself of the decision variables in this problem, I decided to try to store the variable names in a cell array in a struct, as desribed by MOSEK. However, I am unsure of how to do this. The relevant fragment of my code is listed below:
JiSet = 1:6; TiSet = [7 8]; nL = length(TiSet);
P = struct;
P.names.var{1} = 'gamma_i(k+1)'; % my first constraint
P.names.var{2:1+nL,1} = cellstr(strcat('nu_',int2str(TiSet(:)),'(k+1)')); % a series of constraints of varying length
When I run the above code I get the following error:
The right hand side of this assignment has too few values to satisfy the left hand side.
However, if I enter strcat('nu_',int2str(TiSet(:)),'(k+1)') in my command window, it shows an ans variable that is a 2x1 cell (as desired). How do I assign the values in this cell to the 2:1+nL,1 entries in the P.names.var cell?
Try the same line with round brackets ( ), which are used to select sets of cells.
P.names.var(2:1+nL,1) = cellstr(strcat('nu_',int2str(TiSet(:)),'(k+1)'));
MyCell{1:2} refers to the literal contents of the cells.
MyCell = {10,24,-60};
MyCell{1:2}
ans =
10
ans =
24
MyCell(1:2) refers to a collection of cells.
MyCell(1:2)
ans =
[10] [24]
Since cellstr(strcat('nu_',int2str(TiSet(:)),'(k+1)')); is a collection of cells, you need to assign it to a collection of cells, P.names.var(2:1+nL,1);
The problem is in how you structure your assignment in the last line. The right-hand side creates a single value, a 2x1 cell array. However, the left-hand refers to two separate fields. This behavior is likely a result of the nature of cell-arrays, which may except a different data-type in each cell. Cell arrays are set up to assign whatever is on the right-hand side to a single cell on the left-hand side. You can fix this with a simple modification:
P.names.var(2:1+nL,1) = cellstr(strcat('nu_',int2str(TiSet(:)),'(k+1)'));
This utilizes "array indexing" as opposed to "cell indexing".

still stuck trying to update rows name of uitable in matlab gui

I am still trying to update the row names of a uitable in a matlab gui. I previously asked you about this (https://stackoverflow.com/questions/21585453/update-rows-name-of-uitable-in-matlab-gui). However, I am writing you again in order to let you know that I am not able to solve this problem.
This is the part of my code where I am stuck:
function SavePushButton_Callback(~,~)
%%##
data{1,1}= get(h5Out,'string');
data{1,2}= get(h6Out,'string');
data{1,3}= get(h7Out,'string');
data{1,4}= get(h8Out,'string');
===============================
% Update the data %
oldData = get(t,'Data');
newRow = cat(0,data,cell(0,size(data,2)));
newData2 = [oldData; newRow];
set(t,'Data',newData2);
===============================
% this part of the code should Update the name of the rows %
rowname = get(h1Out,'string');
NewRowName = cat(0,rowname,cell(0,size(rowname,1)));
rowname2= [rowname; NewRowName];
set(t,'Rowname',rowname2);
end
It looks like the function to set the Rowname is expecting a cell array of strings, however you're giving it a cell array of cells, which themselves contain strings. This is because the get function (for example data{1,1}= get(h5Out,'string'); ) is already returning a cell array containing a string, and you're storing it within another cell array ( data ). Use () instead of {} on the data matrix in order to concatenate the individual cells into one single cell matrix. In other words, change:
data{1,1}= get(h5Out,'string');
data{1,2}= get(h6Out,'string');
data{1,3}= get(h7Out,'string');
data{1,4}= get(h8Out,'string');
to:
data(1,1)= get(h5Out,'string');
data(1,2)= get(h6Out,'string');
data(1,3)= get(h7Out,'string');
data(1,4)= get(h8Out,'string');

Deleting elements from a cell in Matlab

In a matrix, to remove the columns in which the element of the first line is 0, we can use:
ind2remove = (A(1,:) == 0);
A(:,ind2remove) = [];
How do I do that if A is a cell? I want to remove the columns in which the element of the first row is 0.
I tried:
ind2remove = (A{1,:} == 0);
A{:,ind2remove} = [];
but I got the error message:
??? Error using ==> eq
Too many input arguments.
Error in ==> ind2remove = (A{1,:} == 0);
Indexing using { } gives you the contents of the cell, whereas indexing using ( ) returns the same type as the original object i.e., if A is a cell, A{i,j} will return what it's holding, and A(i,j) will always return a cell. You need the latter.
So in your case, you can do the following to eliminate all columns where the first row has a 0.
A(:, cellfun(#(x)x==0, A(1,:))) = [];
The assumption here is that each cell in the first row holds only a single numeric element, as per your comment.

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.