what does the error mean for "Function definitions are not permitted in this context."?

clc; close all; clear all; levels = 5; im=imread('C:\Users\netuser\Desktop\project ssf\ssf.jpg'); function [gaussout] = gauss_pyramid(im, levels) gaussout = cell(1,levels+1); gaussout{1} = im; subsample = im; figure(1); imshow(im); for i = 2 : levels+1 subsample = reduce(subsample); gaussout{i} = subsample; figure{i}; imshow(gaussout{i}); end end

답변 (1개)

Jan
Jan 2018년 1월 23일
편집: Jan 2018년 1월 23일
You can save code in two kind of files: scripts and functions. Functions start with the keyword "function" and if this is missing, the file is a "script".
Functions have the advantage, that they have their own workspace (set of locally used variables), while scripts share the workspace with the caller. This makes the sharing of variables easier, but more dangerous, because a script might modify a variable used in the caller, but this is not visible during reading the source code.
Since Matlab R2016b you can define functions inside script files, but for older versions, functions could be defined in function files only. You cannot define a function in the command window.
The code can be simplified:
function gaussout = gauss_pyramid(im, levels)
gaussout = cell(1, levels+1);
for k = 1 : levels+1
gaussout{k} = im;
figure(k);
imshow(im);
im = reduce(im);
end
end

카테고리

도움말 센터File Exchange에서 Debugging and Improving Code에 대해 자세히 알아보기

질문:

2018년 1월 23일

편집:

Jan
2018년 1월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by