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

34 次查看(过去 30 天)
Sinem Tola
Sinem Tola2021-10-16
评论: Sinem Tola ,2021-10-21
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 个评论
Rik
Rik 2021-10-17
Can you reply to my answer? I don't see what exactly you have edited in your question.

请先登录,再进行评论。

采纳的回答

Rik
Rik 2021-10-16
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 个评论
Sinem Tola
Sinem Tola 2021-10-21
I have realized another mistake and corrected but now it looks fine.
Thanks again for your help. Here is the plot:

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by