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

32 Ansichten (letzte 30 Tage)
Sinem Tola
Sinem Tola am 16 Okt. 2021
Kommentiert: Sinem Tola am 21 Okt. 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 Kommentar
Rik
Rik am 17 Okt. 2021
Can you reply to my answer? I don't see what exactly you have edited in your question.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by