got error 'Attempt to execute SCRIPT pca as a function'

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

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?
dear sir i got Attempt to execute SCRIPT pca as a function: C:\Users\Adnan Gujjar\Desktop\MSProject\pca.m
Error in classifyexample (line 8) [wcoeff,score,latent,tsquared,explained] = pca(jointdata,...
this is whole ccode that i run in to matlab i have 8 variable numerial data.
load jointdata
figure()
boxplot(jointdata,'orientation','horizontal');
C = corr(jointdata,jointdata);
w = 1./var(jointdata);
[wcoeff,score,latent,tsquared,explained] = pca(jointdata,...
'VariableWeights',w);
c3 = wcoeff(:,1:3);
coefforth = inv(diag(std(jointdata)))*wcoeff;
I = c3'*c3;
cscores = zscore(jointdata)*coefforth;
figure()
plot(score(:,1),score(:,2),'+');
xlabel('1st Principal Component');
ylabel('2nd Principal Component');
@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

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2015년 5월 18일
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

dear sir i can not understand please here is my all code can u correct it. i ma very new in Matlab
load jointdata
figure()
boxplot(jointdata,'orientation','horizontal');
C = corr(jointdata,jointdata);
w = 1./var(jointdata);
[wcoeff,score,latent,tsquared,explained] = pca(jointdata,...
'VariableWeights',w);
c3 = wcoeff(:,1:3);
coefforth = inv(diag(std(jointdata)))*wcoeff;
I = c3'*c3;
cscores = zscore(jointdata)*coefforth;
figure()
plot(score(:,1),score(:,2),'+');
xlabel('1st Principal Component');
ylabel('2nd Principal Component');
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.
can u make some change in code where it is necssary because i can not understand what u say?. i show my whole code here make changing please. then paste here i run what u send me.
MATLAB has two kinds of M-files: scripts and functions, which are very different to use.
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)
You can read lots of examples in the function documentation.
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
Dear i change filename i still face same problem. please look it and correct me code.
What filename are you using now? And exactly what error message are you getting now?

댓글을 달려면 로그인하십시오.

Hassan Macanga
Hassan Macanga 2020년 4월 28일
편집: Walter Roberson 2020년 4월 29일
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

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
Rik
Rik 2020년 4월 28일
편집: Rik 2020년 4월 29일
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)
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Dimensionality Reduction and Feature Extraction에 대해 자세히 알아보기

질문:

2015년 5월 18일

편집:

Rik
2020년 4월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by