PCA Graph of Data Comes Up Blank

I am running an example code from the Climate Data Toolbox and I am having issues plotting the graph of the principle component of my data. When I run the code, it is resulting in a blank graph. The data is the file attached. The following is my code.
clc
close all
clear all
%% TS
filename = 'data';
S = dir(fullfile(filename,'*.txt'));
for k = 1:numel(S)
fnm = fullfile(filename,S(k).name);
mtx1 = load(fnm);
mtx1 = normalize(mtx1);
mtx = reshape(mtx1,72,144);
TS{k} = mtx;
TS{k}(TS{k} == -1000) = NaN;
end
TS_cat = cat(3,TS{:});
lat = (-90:2.5:90);
lon = (-180:2.5:180);
[Lon,Lat] = meshgrid(lon,lat);
figure
TS_cat = fillmissing(TS_cat,'linear');
imagescn(lon,lat,mean(TS_cat,3))
axis xy
cmocean thermal
xlabel 'longitude'
ylabel 'latitude'
%%
[eof_maps,pc,expv] = eof(TS_cat);
% Plot the first mode:
figure
imagesc(lon,lat,eof_maps(:,:,1))
axis xy image
cmocean('curl','pivot')
title 'The first EOF mode!'
t = length(pc(1,:));
%
figure
% subsubplot(3,1,1)
plot(t,pc(1,:))
box off
axis tight
ylabel 'pc1'
title 'The first principal components'
How would I fix this?
Thanks in advance.

Answers (1)

I think you're mixing and matching different ways of defining filenames. As a result, dir is unable to find the files you're looking for.
When I ran your code, this came up empty:
S = dir(fullfile(filename,'*.txt'));
The easiest solution is to navigate directly to the folder where all of your .txt files are, and do
S = dir('*.txt');
I was able to get around all of this by manually writing the filename in this line:
mtx1 = load('data.txt');
But that of course only loads the single example file you uploaded. As a result, pc only contains one entry, becauese there's only one timestep.

6 Comments

In the portion for setting the filename, I have it set to the directory path.
filename = 'C:\Users\mike\Desktop\New folder\TS';
S = dir(fullfile(filename,'*.txt'));
for k = 1:numel(S)
fnm = fullfile(filename,S(k).name);
mtx1 = load(fnm);
mtx1 = normalize(mtx1);
mtx = reshape(mtx1,72,144);
TS{k} = mtx;
TS{k}(TS{k} == -1000) = NaN;
end
I checked what S gives and it gives this:
Is the portion "isdir" being 0 what you meant by S coming up as empty?
Ah, okay, if the data's loading for ya, then that's not the issue.
After the loop, do you see any data in TS_cat? Check to make sure there's data in the first couple of slices by
imagesc(TS_cat(:,:,1))
and
imagesc(TS_cat(:,:,2))
Yes, when I run
imagesc(TS_cat(:,:,1))
and
imagesc(TS_cat(:,:,2))
it gives me the following, respectively.
I noticed both maps are the same.
They look similar, but they don't look exactly the same. Okay, so there's evidence that you do indeed have some data here. Are you sure there aren't any NaNs anywhere in the dataset? Check by
imagesc(sum(isnan(TS_cat),3))
colorbar
And make sure everything is zero.
This is the image that I get when I run
imagesc(sum(isnan(TS_cat),3))
colorbar
I tried to see if changing
TS{k}(TS{k} == -1000) = NaN;
from NaN to 0 in the loop, makes any difference but it gives the same figure.
I changed
TS{k}(TS{k} == -1000) = NaN;
to
TA{k}(TA{k} <= 0) = NaN;
in the loop and this is the figure that is produced:

Sign in to comment.

Categories

Asked:

on 2 May 2021

Commented:

on 2 May 2021

Community Treasure Hunt

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

Start Hunting!