Error in Principal Component Analysis (PCA) matlab

Error in Principal Component Analysis (PCA) matlab. I applied PCA on matlab using my variable fextracted. It is a 22 x 16 array . I only have fextracted as the information. Please help me with the steps to do PCA. Attached are my data (fextracted) and my PCA code. I dont understand what should i do first before using the PCA() function on MATLAB. Ive seen tutorials and read from matlab PCA docs but i did not manage to figure it out.

 Accepted Answer

You are getting this error because you have overwritten MATLAB's pca function with your pca.m file. Rename your file, and your call to pca should work.
See here for execution precedence.

3 Comments

Hi Cris, thanks for replying. Really appreciate that.
But, apparently, I did not have any file that named pca.m. I want to ask in order to apply this line, do i need to find coeff, score, latent etc seperately first? Because I saw examples on Matlab doc, they only execute this line after loading the data. Not sure am I understood correclty or not. But, even if i used coeff = pca(fextracted) it gives the same error as well. The coedd,score,latent,tsquared,explained have red lines saying "values assigned are unused, recommed to use ~". So i do not now why this function does not work.
[coeff,score,latent,tsquared,explained] = pca(fextracted);
The error message suggests otherwise. Run the following code in the command window
which pca
The result should be something like this: C:\Program Files\MATLAB\R2021a\toolbox\stats\stats\pca.m
Your syntax is correct.
fextracted = rand(22,16);
[coeff,score,latent,tsquared,explained] = pca(fextracted)
coeff = 16×16
-0.0414 0.4172 0.1205 0.2871 0.6193 -0.0735 0.1129 0.1205 0.1097 0.2141 0.0162 0.3444 0.0827 -0.1396 -0.2588 0.2025 -0.2079 0.0292 0.1624 0.5370 -0.2892 0.0452 0.1042 -0.0634 -0.2043 0.2314 0.3456 0.2450 0.0994 -0.1214 0.4364 -0.2254 -0.1059 -0.1846 0.2349 -0.0497 -0.1560 -0.0855 0.4983 0.3462 -0.2047 0.3313 0.0175 -0.3634 -0.0127 -0.0893 -0.1031 0.4410 0.4788 0.0664 -0.2710 0.1164 -0.3339 -0.0306 -0.0870 0.1993 0.0728 0.0753 -0.1985 0.0602 0.4072 -0.5384 -0.0948 0.0333 0.1765 0.3922 0.0863 -0.2889 0.0492 -0.3856 0.0676 -0.0057 0.1444 -0.2174 0.5058 -0.2350 0.2481 -0.0124 0.3428 0.1003 0.3360 -0.2255 0.0197 0.2778 -0.0181 0.0308 0.1925 -0.3264 0.6195 0.1211 -0.0611 -0.0089 -0.1684 0.1507 0.2419 0.3193 -0.4205 0.0812 0.1869 0.0098 -0.1779 -0.2231 0.0484 0.0416 0.5401 0.1567 -0.2317 -0.1885 0.3183 0.0822 -0.1787 -0.3881 0.1167 -0.0461 0.3125 -0.4395 -0.0251 0.4876 -0.1637 -0.1195 0.0395 0.4076 0.1500 0.2140 0.3878 0.1460 -0.0056 0.0791 -0.2565 -0.0794 0.1934 0.1726 0.0759 0.2987 -0.4755 0.4428 0.2267 -0.2773 -0.0164 -0.1648 0.0334 -0.1443 0.2661 0.3083 -0.0051 -0.3974 0.0119 -0.2117 -0.1039 -0.4173 -0.1098 0.3844 0.2014 0.0472 0.2511 0.5553 -0.1852 0.0164 -0.0597 -0.0034
score = 22×16
0.5916 0.3959 -0.0283 0.3969 -0.3703 -0.2739 0.1248 -0.1741 -0.2166 -0.3241 0.2682 0.0876 0.2363 0.0172 -0.0428 -0.0409 -0.5745 0.3889 0.7649 0.1216 0.0371 0.1386 -0.2615 -0.2539 0.1048 0.4315 -0.0601 -0.0760 0.1336 -0.0508 -0.0344 -0.0456 -0.5343 0.0964 -0.3965 0.1531 0.5716 -0.0529 0.1151 0.1186 -0.0198 -0.1760 -0.2102 -0.0179 0.1872 0.2434 0.0357 0.0464 0.3658 0.1188 -0.0745 -0.0959 0.3265 0.1423 -0.1210 0.3631 0.6778 -0.0864 0.0085 0.0573 -0.0693 -0.0345 -0.0540 -0.0340 -1.1158 0.1176 0.0473 0.0949 -0.3773 -0.3859 0.1984 -0.1899 0.1312 -0.1197 0.0049 0.0914 -0.1122 -0.0657 0.0826 0.0206 0.2315 -0.2773 0.5269 -0.2486 0.6571 -0.0102 0.3536 -0.1562 -0.0260 0.0068 0.4174 -0.0323 -0.0993 -0.0246 0.0208 0.0606 -0.3856 0.3658 -0.3582 -0.3215 -0.0401 0.6392 0.1356 -0.4788 0.2512 -0.0378 0.2407 0.1402 0.0733 0.0661 0.0123 -0.0255 0.6539 -0.0443 0.4341 -0.0771 0.0258 -0.2478 -0.2313 -0.1191 0.1442 -0.2703 0.0118 -0.2130 0.0784 -0.0916 -0.0221 0.0730 -0.2626 0.5914 0.4794 0.0879 -0.0430 -0.3025 -0.0907 0.3458 0.1498 -0.2271 -0.1490 0.0244 -0.0961 0.0940 -0.0467 -0.0369 -0.2414 -1.2151 0.3395 -0.1321 -0.2833 -0.1413 0.4139 -0.2173 0.0041 0.0395 -0.1649 0.0482 -0.0423 0.1210 -0.1394 -0.0196
latent = 16×1
0.2674 0.2029 0.1497 0.1367 0.1084 0.0874 0.0819 0.0726 0.0523 0.0413
tsquared = 22×1
16.7472 15.6788 16.8153 14.5799 12.7046 16.2672 15.7190 12.3457 11.2392 18.7158
explained = 16×1
20.6980 15.7046 11.5873 10.5788 8.3876 6.7649 6.3364 5.6231 4.0458 3.1998
Hi Chris!!
Thnak you so much!! It works really well. I mislook, there is a file name pca.m . I did not notice it earlier.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!