How to solve the error?

1 view (last 30 days)
Shaila parvin
Shaila parvin on 17 Jun 2013
close all;
clear all;
clc;
function [Instances T_target]=create_learning_set()
%%create data set for each texture class
%for cloud class
srcFiles = dir('colored_textures\LearningSet\cloud\*.jpg'); % the folder in which ur images exists
Iin=[]; %this hold the data as input to the neural network
Target=[]; %this holds the target or the required result for each class
%we put it manully.
%read the folder containing the coloud images
for i = 1 : length(srcFiles)
filename = strcat('colored_textures\LearningSet\cloud\',srcFiles(i).name);
I = imread(filename); %read the image
F=get_image_features(I); %create feature vector for the image
Iin=[Iin F']; %concatenate the feature vector to the store of features
Target=[Target, 100];%concatenate the target value for each feature vector
end
%for gravel class
srcFiles = dir('colored_textures\LearningSet\gravel\*.jpg'); % the folder in which ur images exists
for i = 1 : length(srcFiles)
filename = strcat('colored_textures\LearningSet\gravel\',srcFiles(i).name);
I = imread(filename);
F=get_image_features(I); %create feature vector for the image
Iin=[Iin F']; %concatenate the feature vector to the store of features
Target=[Target, 200];%concatenate the target value for each feature vector
end
%for snake skin class
srcFiles = dir('colored_textures\LearningSet\wood\*.jpg'); % the folder in which ur images exists
for i = 1 : length(srcFiles)
filename = strcat('colored_textures\LearningSet\wood\',srcFiles(i).name);
I = imread(filename);
F=get_image_features(I); %create feature vector for the image
Iin=[Iin F']; %concatenate the feature vector to the store of features
Target=[Target, 300];%concatenate the target value for each feature vector
end
T_target=Target;
When I run the code, the error arises
"??? Error: File: create_learning_set.m Line: 5 Column: 1
Function definitions are not permitted in this context.
Error in ==> new_test at 6
[Instances T_target]=create_learning_set();"
How can I solve this?

Accepted Answer

Jan
Jan on 17 Jun 2013
Edited: Jan on 17 Jun 2013
Functions cannot be defined inside scripts. An M-file, which does not start with the keyword "function" is a script. So simply omit the brute clearing header: close all; clear all; clc;
Btw, I frequently post warnings about this ugly piece of code here, perhaps too frequently. "close all" deletes all other windows without asking the user. This might be useful if a former application has crashed, but with this argument restarting the computer each time would also be "useful". "clc" removes all former warning and error messages from the command window, but this information could be useful. Finally "clear all" causes the most troubles: On one hand it removes all loaded functions from the memory and reloading them from the disk is a time-consuming job. On the other hand it deletes all breaks points also, and everything, which impedes debugging seriously, is a bad programming pattern.
In opposite to the drawbacks, advantages of this brute clearing are very rare. There are much better ways to keep the code clean, e.g. using functions instead of scripts, such that the local workspace is clear automatically. In scripts, "celar" without "all" clears only the variables. If formerly created figures of this program should be closed, do this explicitly:
...
existingFig = findobj(allchild(0), 'flat', 'tag', 'myTagName');
if any(existingFig)
delete(existingFig)
end
...
figure('Tag', 'myTagName');
As usualy let me ask who has suggested the brute clearing header to you. It is time to contact the sources of this method instead of trying to teach the end-users.
Please note: You find the solution of this problem much faster if you use the search function in this forum. 49 people have asked the same question already: http://www.mathworks.com/matlabcentral/answers/?term=Function+definitions+are+not+permitted+in+this+context
  1 Comment
Shaila parvin
Shaila parvin on 17 Jun 2013
Okay. Now another error arises. that is
??? Undefined function or method 'get_image_features' for input arguments of type 'uint8'.
Error in ==> create_learning_set at 13 F=get_image_features(I); %create feature vector for the image
What to do?

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 17 Jun 2013
Get rid of those first three lines. They are not doing you any good and they are preventing the code from working.

Tags

Community Treasure Hunt

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

Start Hunting!