got error 'Attempt to execute SCRIPT pca as a function'
Mostrar comentarios más antiguos
w = 1./var(jointdata);
[wcoeff,score,latent,tsquared] = pca(jointdata,...
'VariableWeights');
Attempt to execute SCRIPT pca as a function:
C:\Users\Adnan Gujjar\Desktop\MSProject\pca.m
i want to apply PCA in it why i got error can any one help me??
4 comentarios
What do you get when you type this in the command window?:
which pca
Did you write this script/function? What is the first line of code in that Mfile?
MUHAMMAD ADNAN
el 18 de Mayo de 2015
MUHAMMAD ADNAN
el 18 de Mayo de 2015
Editada: Guillaume
el 18 de Mayo de 2015
Jan
el 18 de Mayo de 2015
@Muhammad: Please answer Stephens question: What do you get when to type this command in the command window:
which pca
Or even better:
which pca -all
Respuestas (2)
Walter Roberson
el 18 de Mayo de 2015
Your file C:\Users\Adnan Gujjar\Desktop\MSProject\pca.m does not have its first non-comment line starting with
function SOMETHING = pca(SOMETHING)
It needs to do so if you want to call upon it to return a calculated result.
7 comentarios
MUHAMMAD ADNAN
el 18 de Mayo de 2015
Editada: Walter Roberson
el 18 de Mayo de 2015
Walter Roberson
el 18 de Mayo de 2015
That part is not obviously wrong. The difficulty is that you are calling upon pca(), which requires that pca() be a function. Where is the code for the pca() function?
Is it possible that you stored this code you show here in a file named pca.m ? If so then when it got to the call to pca() it would attempt to use the current file to satisfy the call to pca (a recursive call) rather than calling into the Statistics Toolbox.
If you named your code pca.m then you need to rename it. Don't call it inv.m or zscore.m or plot.m or xlabel.m or ylabel.m either.
MUHAMMAD ADNAN
el 18 de Mayo de 2015
What you currently have may be script which you are trying to use like a function, which causes the error. You will need to show us the complete code for us to know this.
To convert a script into a function simply a line at the start of the M-file:
function [out1,out2,..outN] = pca(inp1, inp2.. inpN)
Walter Roberson
el 18 de Mayo de 2015
Do not put the line Stephen suggests at the top of your file!
Just take your file and rename it from pca.m to runpca.m
MUHAMMAD ADNAN
el 18 de Mayo de 2015
Walter Roberson
el 18 de Mayo de 2015
What filename are you using now? And exactly what error message are you getting now?
Hassan Macanga
el 28 de Abr. de 2020
Editada: Walter Roberson
el 29 de Abr. de 2020
clc
clear
input_dir = 'user/mac/desktop/python/eigenfacematlab/eigenface24/train';
testdir= 'user/mac/desktop/python/eigenfacematlab/eigenface24/test';
image_dims = [200, 180];
classes=[0;1;2;3;4;5;6;7;8;9];
num_images = 170;
images = [];
imageclasses=[];
disp(num_images);
counter = 1;
c = cell(170,1);
c{1}={''};
file=cell(170,1);
file(1)={''};
paths = fullfile(testdir,{'0','1','2','3','4','5','6','7','8','9'});
pathsA = fullfile(testdir,{'0','1','2','3','4','5','6','7','8','9'});
for i = classes(1):classes(10)
filenames = dir(fullfile(input_dir,int2str(i),'\*.jpg'));
num_images = size(filenames,1);
for n = 1:num_images
%filenames = dir(fullfile(input_dir, '*.jpg'));
%disp(filenames)
filename = fullfile(input_dir,int2str(i), filenames(n).name);
disp(filename)
ij=cellstr(filename);
k=cellstr(filenames(n).name);
file{counter}=(k);
disp(filename)
img = imread(filename);
img = rgb2gray(img);
img = im2double(img);
c{counter}=(1i);
images(:, counter) = img(:);
imagesclasses(:,counter)=i;
counter=counter+1;
end
end
num_images = 170;
counter = 170;
mean_face = mean(images, 2);
shifted_images = images - repmat(mean_face, 1, num_images);
[u, s, v] = pca(images');
num_eigenfaces = 20;
u = u(:, 1:num_eigenfaces);
features = u' * shifted_images;
testlabel=[];
cc=1;
testimages=[];
featuresvector=[];
predicition=[];
%testing
for i = classes(1):classes(10)
filenames1 = dir(fullfile(testdir,int2str(i),'\*.jpg'));
for n = 1:3
filename1 = fullfile(testdir,int2str(i),filenames1(n).name);
img = imread(filename1);
img = rgb2gray(img);
img = im2double(img);
figure(1)
imshow(img)
% if n == 1
% images = zeros(prod(image_dims), num_images);
% end
input_image(:, cc) = img(:);
testlabel(:,cc)=i;
feature_vec = u' * (img(:) - mean_face);
similarity = arrayfun(@(n) 1 / (1 + norm(features(:,n) - feature_vec)), 1:num_images);
% find the image with the highest similarity
[match, matchidx] = max(similarity);
A = reshape(images(:,matchidx), image_dims);
Predicition(:,cc)=imagesclasses(matchidx);
% display the result
%figure
%A=cell2struct(file,'name',1)
if 3%n=0
figure
imshow([img reshape(images(:,matchidx), image_dims)]);
title(sprintf('matches %f, score %0.2f', 1-match));
end
cc=cc+1;
end
end
confusionMatrix=confusionmat(testlabel,predicition);
%show mean face
a = reshape(mean_face, image_dims);
figure
imshow(a)
% display the eigenfaces
figure;
for n = 1:num_eigenvalues
subplot(2, ceil(num_eigenvalues/4), n);
ejector = reshape(u(:,n), image_dims);
images(ejector);
colormap(gray);
end
3 comentarios
Hassan Macanga
el 28 de Abr. de 2020
Attempt to execute SCRIPT eigenfaces as a function:
/Users/mac/Desktop/python /eigenfacematlab /eigenfaces.m
i keep in getting this error anyone to help out
This is not an answer, but a question. Please post it as your own question. Feel free to post a link to your question here.
For your reposted question, consider the following changes:
- either attach your m-file or use proper layout to make your question more readable
- post an exact description of what you are doing that produces this error (what is your current directory and how are you calling this script)
Walter Roberson
el 29 de Abr. de 2020
You have a script (not a function) named eigenfaces.m which you are attempting to invoke as if it were a function. However it is not clear to us where you are doing that, as you do not use eigenfaces in your code. Perhaps the file you posted is itself named eigenfaces.m and you are somehow invoking it as if it were a function.
Categorías
Más información sobre Dimensionality Reduction and Feature Extraction en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!