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

34 visualizaciones (últimos 30 días)
Sinem Tola
Sinem Tola el 16 de Oct. de 2021
Comentada: Sinem Tola el 21 de Oct. de 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 comentario
Rik
Rik el 17 de Oct. de 2021
Can you reply to my answer? I don't see what exactly you have edited in your question.

Iniciar sesión para comentar.

Respuesta aceptada

Rik
Rik el 16 de Oct. de 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 comentarios
Sinem Tola
Sinem Tola el 20 de Oct. de 2021
Hello,
Sorry I saw your reply late, wasn't expecting such a fast answer :) thak you very much. Rearranging the line as you described got rid of the error.
cellfun(@plot, X1, r_4);
Since it is a long code, it's still busy wtih running now. When i finally obtain the plot, i will put it here, hopefully without any trouble.
Sinem Tola
Sinem Tola el 21 de Oct. de 2021
I have realized another mistake and corrected but now it looks fine.
Thanks again for your help. Here is the plot:

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by