Stephen White
Stephen White

Reputation: 1

MATLAB loading data from multiple .mat files

My data is x,y co-ordinates in multiple files

a=dir('*.mat')
b={a(:).name}

to load the filenames in a cell array

How do I use a loop to sequentially load one column of data from each file into consecutive rows of a new/separate array......?

I've been doing it individually using e.g.

Load(example1.mat)
A(:,1)=AB(:,1)
Load(example2.mat)
A(:,2)=AB(:,1)
Load(example3.mat)
A(:,3)=AB(:,1)

Obviously very primitive and time consuming!!

My Matlab skills are weak so any advice gratefully received

Cheers

Many thanks again, I'm still figuring out how to read the code but I used it like this; a=dir('*.mat'); b={a(:).name}; test1=zeros(numel(b),1765); for k=1:numel(b) S=load(b{k}); I then used the following code to create a PCA cluster plot test1(k,:)=S.AB(:,2); end [wcoeff,score,latent,tsquared,explained] = pca(test1,... 'VariableWeights','variance'); c3 = wcoeff(:,1:3) coefforth = inv(diag(std(test1)))*wcoeff; I = c3'*c3 cscores = zscore(test1)*coefforth; figure() plot(score(:,1),score(:,2),'+') xlabel('1st Principal Component') ylabel('2nd Principal Component') –

I was using 'gname' to label the points on the cluster plot but found that the point were simply labelled from 1 to the number of rows in the array.....I was going to ask you about this but I found out simply through trial and error if I used 'gname(b)' this labels the points with the .names listed in b.....

However the clusterplot starts to look very busy/messy once I have labelled quite a few points so now I am wondering is is possible to extract the filenames into a list by dragging round or selecting a few points, I think it is possible as I have read a few related topics.....but any tips/advice around gname or labelled/extracting labels from clusterplots would be greatly appreciated. Apologies again for my formatting I'm still getting used to this website!!!

Upvotes: 0

Views: 719

Answers (1)

Benoit_11
Benoit_11

Reputation: 13945

Here is a way to do it. Hopefully I got what you wanted correctly :)

The code is commented but please ask any questions if something is unclear.

a=dir('*.mat');
b={a(:).name};

%// Initialize the output array. Here SomeNumber depends on the size of your data in AB.
A = zeros(numel(b),SomeNumber);

%// Loop through each 'example.mat' file
for k = 1:numel(b)

%// ===========  
%// Here you could do either of the following:

1)
   %// Create a name to load with sprintf. It does not require a or b.
   NameToLoad = sprintf('example%i.mat',k);

   %// Load the data
   S = load(NameToLoad);


2) 
   %// Load directly from b:
   S = load(b{k});
   %// ===========

%// Now S is a structure containing every variable from the exampleX.mat file. 
%// You can access the data using dot notation.

 %// Store the data into rows of A
   A(k,:) = S.AB(:,1);

end

Hope that is what you meant!

Upvotes: 0

Related Questions