How to access function handles in a cell array? - matlab

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);

Related

Add value to cell in a loop

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.

Save Cell Array into 2D-Array in Matlab

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

looping technique with the data using variable name?

This is my code program which is x is my data, and i have another data name such as af4,f7 and f8.. How can I do looping technique on my program, so that the x will be automatically change into af4, then f7 and last f8 in Matlab?
x=af3;
d = fdesign.lowpass('Fp,Fst,Ap,Ast',4,5,1,40,128);
Hd = design(d,'butter');
fvtool(Hd);
y_delta = filter(Hd,x);
How do you generate these variables af4, af7 and af8? If you can create them as cells in a cell array or as fields in struct - your life would be much easier.
If you have no control over the variables, you can use eval:
varNames = {'af3', 'af4', 'af7', 'af8' }; % as strings
for vi=1:numel(varNames)
x = eval( varNames{vi} ); % here''s the trick
% continue here with x...
end
Note however that it is extremely unrecomanded to use eval.
I think this is what you could use:
xCell = {af3, af4, af7, af8};
for xi = 1:nnumel(xCell)
x = xCell{xi};
% do what you want to do with x
end

Create multiple variables of same size in Matlab

Say, I want to create 3 variables of same size in MATLAB:
a = zeros(3,3);
b = zeros(3,3);
c = zeros(3,3);
Is there any fast way to do this, I know this is not working but I think of something like
a,b,c = zeros(3,3);
Any suggestions?
To use deal there is no need to wrap it in a cell as NKN suggested:
[a,b,c]=deal(zeros(3,3))
Although you can do this:
a = zeros(3); % a 3x3 zero matrix
b = a;
c = a;
If you define the values that you want to assign, in a cell, the other way (faster) is:
c={zeros(3)};
[a1,a2,a3,a4]=deal(c{1})
it means that you put your assigning value in a cell and then use deal function. Notice that the a1,a2,a3,a4 does not have cell formats, but double formats and actually this is a very fast method.
If you use cell foramt you can assign more values at the same time, for example:
C = {rand(3) ones(3,1) eye(3) zeros(3,1)};
[a,b,c,d] = deal(C{:})
otherwise you can just get rid of the cell and use:
c=zeros(3);
[a1,a2,a3,a4]=deal(c);
as Daniel Suggested.

Problems populating 3D cell array in MATLAB

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.