Function definitions are not permitted in this context.

조회 수: 2 (최근 30일)
gleice de Aguiar
gleice de Aguiar 2019년 1월 14일
댓글: Walter Roberson 2019년 1월 14일
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