A function should exist in an m file. If you want to put it in a script file, it needs to have a closing end keyword, and no other code (except for other function) may exist after it. The error you posted indicates you have not met these requirements.
How to make the modfunc function work ?
2 次查看(过去 30 天)
显示 更早的评论
I have problem with the modfunc function. I get this erroe message: function out = modfunc(Population, Year)
↑
Error: Function definition are not supported in this context. Functions can only be created as local or nested functions in code files.
Also note that I am writing in live script and I wrote the modfunc function in a different live script. Additionally, the plots work, but the the line does not appear.
My code is as follows:
% Fit an exponential function to the data
function out = modfunc(Year,Population)
out = Population(1)*exp(Population(2)*Year);
% Plot the data and the fitted function on a semi-log scale
ain=[0.3 0.3];
figure
subplot(1,2,1)
plot(Population.Year,Population.Population,'o')
hold on
plot(Population,modfunc(Population,ain),'k','LineWidth',3)
subplot(1,2,2)
semilogy(Population.Year,Population.Population,'o')
hold on
semilogy(Population,modfunc(Population,ain),'k','LineWidth',3)
xlabel('Year');
ylabel('Population');
title('World Population with Exponential Fit');
legend('Data', 'Exponential Fit');
0 个评论
回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Subplots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!