Undefined function or variable 'trainset'. Eror in svm (line 33) SVMModel = fitcsvm(trainset,class)
Matlab R2016b
조회 수: 5 (최근 30일)
이전 댓글 표시
clc;
clear;
close all;
warning off all;
image_folder = 'pengolahan\data latih';
filenames = diri(fullfile(image_folder,'*.jpg'));
total_images = numel(filenames);
area = zeros(1,total_images);
perimeter = zeros(1,total_images);
metric = zeros(1,total_images);
eccentricity = zeros(1,total_images);
for n = 1 : total_images
full_name = fullfile(image_folder, filenames(n).name);
our_images = logical(imread(full_name));
our_images = bwconfull(our_images,'objects');
stats = regionprops(our_images,'Area','Perimeter','Eccentricity');
area(n) = stats.Area;
perimeter(n) = stats.Perimeter;
metric(n) = 4*pi*area(n)/(perimeter(n)^2);
eccentricity(n) = stats.Eccentricity;
trainset = [metric;eccentricity]';
end
%prepare class label for first run of SVM
class = cell(75,1);
class(1:35,1) = {'Sub Tropis'};
class(36:75,1)= {'Tropis'};
%perform run of svm
figure
SVMModel = fitcsvm(trainset,class);
sv = SVMModel.SupportVectors;
gscatter(trainset(:,1), trainset(:,2),class)
grid on
hold on
plot(sv(:,1),sv(:,2),'ko','MarkerSize',10)
legend('Sub Tropis','Tropis','Support Vector')
hold off
xlabel ('Metric')
ylabel ('Eccentricity')
save SVMModel.mat SVMModel
[SL: formatted code as code and added line breaks for readability. Also fixed one missing ' in the line where filenames is first defined.]
댓글 수: 2
Image Analyst
2020년 5월 27일
And why do you think that it somehow knows what trainset is if you never defined it? It can't just make stuff up for you. You need to define it. Where did you do that?
답변 (1개)
Steven Lord
2020년 5월 27일
My suspicion is that your images folder contains no JPG files. If it doesn't, your for loop does not execute its loop body and the variable trainset is never created.
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!