I'm trying to plot 4 different graphs in respect to 4 different function handles for 10 revolutions using for loop. I've created a function file and a m-file but the error Operator '.*' is not supported for operands of type 'cell' just keeps popping out.
%% Function file
function [x,y] = my_polar(fhandle, N)
th = 0:0.01:2*pi;
for i =0:N-1
x = fhandle.*cos(th);
y = fhandle.*sin(th);
th = th+2*pi;
end
end
%% m-file
f1 = @(th) exp(0.01*th).*sin(5*th);
f2 = @(th) sin(10*th) + 0.15;
f3 = @(th) sin(0.5*th).*(0.85 + sin(th));
f4 = @(th) 10 + sin(2*pi.*th);
fset = {f1;f2;f3;f4};
for i=1:4
subplot(2,2,i);
[x,y] = my_polar(fset(i), 10);
plot(x,y);
title(char(fset(i)));
end

 채택된 답변

Cris LaPierre
Cris LaPierre 2021년 4월 22일

0 개 추천

You have defined your function to have an input - th. Therefore, when you use your function handle, you have to supply it an input. Perhaps you meant to do the following?
x = fhandle(th).*cos(th);
The error message about 'cell' is because you add your function handles to a cell array, fset. When you use parentheses to index a cell array, it returns a cell array. Use braces to pass the function handle into the function.
[x,y] = my_polar(fset{i}, 10);
Your final error will be about trying to create a title from the function handle. You might find this answer insightful. You probably want to use func2str. Just be aware that it also does not work with cells.

댓글 수: 1

Stanley Ka
Stanley Ka 2021년 4월 22일
It worked! Thank you so much. I'm new to programming so I'm still learning a lot of things, but now I know that even different brackets can have so much impact on the entire code. Thanks again for replying so fast!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2021년 4월 22일

편집:

2021년 4월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by