Function definitions are not permitted in this context.

3 views (last 30 days)
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;

Answers (2)

Cris LaPierre
Cris LaPierre on 14 Jan 2019
Edited: Cris LaPierre on 14 Jan 2019
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 Comments
Cris LaPierre
Cris LaPierre on 14 Jan 2019
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 on 14 Jan 2019
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 .

Sign in to comment.


madhan ravi
madhan ravi on 14 Jan 2019

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!