Function definitions are not permitted in this context.

2 次查看(过去 30 天)
Meu codigo diz que a funçao não é permitida
close all;
load lawdata
rng default
a = imread('Fig5.jpg');
b = imread('Fig4.jpg');
a_red = a(:,:,1);
b_red = b(:,:,1);
%imagem completa, i.e., a media de todas as linhas
for i=1:length(a_red(1,:)),
aux(i) = mean (a_red(:,i));
aux_b(i) = mean(b_red(:,i));
pixel (i) = i;
end
figure, plot (aux,'r');
hold on;
plot (aux_b,'b');
hold off;
title('Curva SPR')
legend('Figura4','Figura5');
%bootstrap
for i = 1:length(a_red(1,:)),
[bootstat,bootsam] = bootstrp_f(2000,a_red(:,1));
[bootstat,bootsam] = sort(m, 'ascend');
bootstat(1,:)
sup(i) = m(1950);
inf(i) = m(90);
end
% 2000, sup = 1950 e inf = 50
% 1000, sup=975, inf = 25
% 100, sup=97, inf = 3
% 500, sup = 487, inf = 13
% 700, sup= 682, inf=18
function[mm] = bootstrp_f(B, dados)
mm = zeros(B,1);
for i=1:B,
mm(i) = mean(datasample(dados, length(dados)));
end
end
[ma, ia] = min(aux(80:160));
[mb, ib] = min(sup(80:160));
abs(ia-ib); % valor absoluto
figure, histogram(bootstat)
se = std(bootstat);
se = 0.1285;
hold on;
hold off;
mm = bootstrp_f(2000,@mean,y);
figure;
[ia,ib] = ksdensity(mm);
figure, plot(ia,ib);
hold on;
hold off;
figure , plot(min(sup));
hold on;
hold off;

回答(2 个)

Cris LaPierre
Cris LaPierre 2019-1-14
编辑:Cris LaPierre 2019-1-14
Functions must be placed at the very bottom of your script or in a separate file. Try this:
close all;
load lawdata
rng default
a = imread('Fig5.jpg');
b = imread('Fig4.jpg');
a_red = a(:,:,1);
b_red = b(:,:,1);
%imagem completa, i.e., a media de todas as linhas
for i=1:length(a_red(1,:))
aux(i) = mean(a_red(:,i));
aux_b(i) = mean(b_red(:,i));
pixel (i) = i;
end
figure; plot(aux,'r')
hold on
plot(aux_b,'b')
hold off
title('Curva SPR')
legend('Figura4','Figura5')
%bootstrap
for i = 1:length(a_red(1,:))
[bootstat,bootsam] = bootstrp_f(2000,a_red(:,1));
[bootstat,bootsam] = sort(m, 'ascend');
bootstat(1,:)
sup(i) = m(1950);
inf(i) = m(90);
end
[ma, ia] = min(aux(80:160));
[mb, ib] = min(sup(80:160));
% #### This value is not captured in a variable ####
abs(ia-ib); % valor absoluto
figure; histogram(bootstat)
se = std(bootstat);
se = 0.1285;
hold on
hold off
mm = bootstrp_f(2000,@mean,y);
figure
[ia,ib] = ksdensity(mm);
figure; plot(ia,ib)
hold on
hold off
figure; plot(min(sup))
hold on
hold off
%%
function[mm] = bootstrp_f(B, dados)
mm = zeros(B,1);
for i=1:B
mm(i) = mean(datasample(dados, length(dados)));
end
end
  2 个评论
Cris LaPierre
Cris LaPierre 2019-1-14
I didn't look close enough. This function call syntax is incorrect
mm = bootstrp_f(2000,@mean,y);
There are too many inputs (bootstrp_f has 2 inputs), the variable y doesn't exist, and you are trying to pass in a function, something your function doesn't support.
Better to try
mm = bootstrp_f(2000,mean(<varname>));
Walter Roberson
Walter Roberson 2019-1-14
note how Cris added an extra end statement . When you define a function inside a script then every function must have a corresponding end statement .
however in the case where the function is in the correct location in a script but is missing the end statement then it is a different error message you would get.
The error message observed occurs in two circumstances:
  1. you tried to define a function at the command prompt .That is not permitted in any MATLAB release and is not likely to be implemented any time soon.
  2. you tried to define a function inside a script in a release before r2016b . Before that function statements could never appear in scripts I suspect this is cause of your message .

请先登录,再进行评论。


madhan ravi
madhan ravi 2019-1-14

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by