Unable to use a value of type 'cell' as an index

34 views (last 30 days)
Sinem Tola
Sinem Tola on 16 Oct 2021
Commented: Sinem Tola on 21 Oct 2021
Hello,
In my code, I receive the error "Unable to use a value of type 'cell' as an index". Looks like it has a problem iregarding the plot line so, I tried to rename r and x but it did not work..
I'll be glad if you could help.
Ny=length(xd); % Total number of frequency increments
ks=0.25; o_c=0.8245; % coefficients
A=[121.07, 101.81, 68.16, 53.76, 20.95, 3.39]; % mm2rad/m, coefficients from line grade 1 to 6
Lrmax= 50; Lrmin =0.5; % Wavelength limits by Lei & Noda
omega_max=2*pi./Lrmin; omega_min = 2*pi./Lrmax; % Spatial frequency limits
delta_omega=(omega_max-omega_min)/Ny; % Frequency increment
n=1:Ny; ph_an=(2*pi).*rand(1,1);
x1 = linspace(dx,Le,Le/dx);
part1 = @(x1) cos((omega_min + (n-0.5).* delta_omega).*x1+ph_an);
part2 = sqrt((ks.*A(1,6).*o_c.^2./(((omega_min + (n-0.5) .* delta_omega).^2+o_c.^2).*(omega_min + (n-0.5) .* delta_omega).^2)).*delta_omega);
r_4 =arrayfun(@(x1)part2.*part1(x1),x1,'un',0);
%Rail profile plot
figure; hold on; title('Rail profiles');
X1 = num2cell(x1);
cellfun(@(X1) plot(r_4(X1), 'o-'), {X1}); & Line 405
xlabel('distance'); ylabel('vertical profile');
Error in QuarterCarModelr15>@(X1)plot(r_4(X1),'o-')
Error in QuarterCarModelr15 (line 405)
cellfun(@(X1) plot(r_4(X1), 'o-'), {X1});
  1 Comment
Rik
Rik on 17 Oct 2021
Can you reply to my answer? I don't see what exactly you have edited in your question.

Sign in to comment.

Accepted Answer

Rik
Rik on 16 Oct 2021
You need to unwind the call to see what's going on.
When you create r_4, you have set UniformOutput to false. That means it is a cell array.
Then in your cellfun call, you use normal indexing. You do that with a variable that you have first wrapped in a cell, which is unwrapped by cellfun. Unfortunately, the variable was already a cell.
cellfun(@(X1) r_4(X1), {X1});
%this will return a cell
Because it returns a cell, you are calling plot with an argument of type cell. Removing the braces in your cellfun call might be enough to solve the issue.
  2 Comments
Sinem Tola
Sinem Tola on 21 Oct 2021
I have realized another mistake and corrected but now it looks fine.
Thanks again for your help. Here is the plot:

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!