I am trying to populate a 3D cell array. Here is the code:
D = cell(M,N,1);
for i = 1:M
for j=1:N
for k = 1:L
D{i}{j}(1+length(D{i}{j})) = 1; % error here
end
end
end
I get the error Cell contents reference from a non-cell array object even though the following within the command window works fine:
D{i}{j}(1+length(D{i}{j})) = 1;
I believe the problem is how you are indexing your cell array D. The syntax is
D{i,j,k}
not
D{i}{j}{k}
The line giving an error should therefore be written
D{i,j,1 + length(D{i,j})} = 1;
For more information see Access Data in a Cell Array.
Related
I have simple code as below and try to insert the values into cell array.
a = cell(14,1);
for i = 1:14
a(i:1)=sin(i)
end
However error came out as:
Conversion to cell from double is not possible.
What is the problem for this code?
Either expand the cell, or wrap the result of the sin function in a cell.
a = cell(14,1);
b = cell(14,1);
for ii = 1:14
a{ii} = sin(ii);
b(ii) = {sin(ii)};
end
isequal(a,b)
ans =
logical
1
Your Syntax is wrong. a(i:1) can not work inside a loop over i. Simply using a(i) will give you the desired result.
I have a Cell Array 1*42 .
I want to save this cell array into 311029*42 array size in .mat file.enter image description here
How to do it ?
You can use cell2mat function to do this. You can see the mechanism of this function in this link (see the following image).
You can just horizontally concatenate a comma-separated list generated from the cell array, then save your new variable like so:
newData = [data{:}];
svae('your_file.mat', 'newData');
Let C be a cell array of 1x42 size. Then, run the following code to get the output array Y.
N = length(C);
L = size(C{1});
Y = size(L(1),L(2)*N);
for n = 1:N
Y(:,1+(n-1)*L(2):n*L(2)) = C{n};
end
rate_arr_cst_1 = #(t) 2*sin(t)+10;
rate_arr_cst_2 = #(t) 3*sin(2*t)+8;
rate_arr_cst_h = {rate_arr_cst_1, rate_arr_cst_2};
I defined a cell array in such way and try to access in the following way:
i=1;
h = rate_arr_cst_h(i);
but what I get here is still a cell array, meaning i can't use h to evaluate t=0.1.
Your help is much appreciated!
When you do h = rate_arr_cst_h(i);, you are accessing the i^th element of the cell array, which is still a cell. If you want to access the contents of i^th cell in the cell array, you need to do: h = rate_arr_cst_h{i};. Note the use of curly brackets.
Either use a for loop:
for ii = 1:numel(rate_arr_cst_h)
hh(ii) = rate_arr_cst_h{ii}(i);
end
or you can use cellfun:
hh = cellfun(#(f) f(i), rate_arr_cst_h);
I'm trying to import multiple data files in MATLAB by using importdata so that the data are available outside the loop:
for i = 1:5
filename = sprintf('data-%d.txt', i);
data{i} = importdata(filename);
end
But the script returns the following error:
Cell contents assignment to a non-cell array object.
Error in process (line 12)
data{i} = importdata(filename);
How can I fix this?
This error typically appears when you are trying to make a cell assignment to a variable that is already instantiated as a non-cell type.
Most likely, somewhere earlier in your code you initialized data as a matrix and you are now attempting to address it as though it is a cell type.
To quickly test this theory out, try out this slightly modified code which ensures that data will be initialized as cell type when you try to address it.
data = cell(1);
for i = 1:5
filename = sprintf('data-%d.txt', i);
data{i} = importdata(filename);
end
I want to create array of MSERRegions class.Basically i have one array of same type returned by function detectMSERFeatures.See code snippet below
regions = detectMSERFeatures(gray_input)
%gray_input is any image in gray scall form
for (i =2:length(regions))
if(length(regions(i).PixelList)>100)
% Here i want to copy all such regions in new object array say of name regions_new
j=j+1;
end
end
How can this be done?
You cannot have an array of MSERRegions objects. You should use a cell array instead.
You can declare an MSER Array in MatLab like this:
regions = MSERRegions();
And add them into the array like this:
regions(1,1) = mserRegions(i,1);
So:
regions_new = MSERRegions();
j = 1;
for (i = 1:length(regions))
if(length(regions(i).PixelList)>100)
regions_new(j,1) = regions(i,1)
j = j + 1;
end
end