How can I load .mat files?

Hello, I have 3 .mat files and I want to measure feature importance. Following code works fine with .txt files but I am getting an error message with .mat files. I want to load all .mat files one by one and seperate predictors (X) and response variable (Y, the last column) for "fscchi2" function but I can't. How can I fix it?
matFiles = dir('Datasets/*.mat');
nFiles = length(matFiles);
for i = 1:nFiles
DataSet = load(matFiles(i).name);
X = DataSet(:,1:end-1);
Y = DataSet(:,end);
[~,Scores] = fscchi2(X,Y);
end

4 Comments

What error message are you getting?
Stephen23
Stephen23 on 18 Oct 2021
Edited: Stephen23 on 18 Oct 2021
"Following code works fine with .txt files but I am getting an error message with .mat files."
Because .mat files get loaded into a scalar structure (not a numeric array), exactly as the LOAD documentation states:
You will need to access the appropriate fields of that structure:
I strongly recommend that you use a more appropriate tool for importing text files, e.g. READMATRIX, DLMREAD, etc.
MB
MB on 18 Oct 2021
Edited: MB on 18 Oct 2021
As you mentined .mat files were loaded into a scalar structure and I couldn't find a way to seperate response column like I did with the .txt files. Anyway, this not a good way of programming but I solved the problem like this:
for i = 1:3
switch i
case 1
load('Datasets/a.mat')
X = a(:,1:end-1);
Y = a(:,end);
case 2
load('Datasets/b.mat')
X = b(:,1:end-1);
Y = b(:,end);
case 3
load('Datasets/c.mat')
X = c(:,1:end-1);
Y = c(:,end);
end
[~,nCols] = size(X);
[Idx,Scores] = fscchi2(X,Y);
...
end
Stephen23
Stephen23 on 18 Oct 2021
Edited: Stephen23 on 18 Oct 2021
"I couldn't find a way to seperate response column like I did with the .txt files"
The columns are in the structure field, not the structure itself. So you just need to access its field/s. If the fieldnames change (i.e. the data is very badly designed by someone who wants to make your life harder) then you can use dynamic fieldnames:
Rik showed this below.
Avoid loading directly into the workspace (as you show) unless you want to include latent bugs in your code.
Much simpler and more robust code would result if the data had been better designed (i.e. the same variable/field name).

Sign in to comment.

 Accepted Answer

Or:
matfilenames={'Datasets/a.mat','Datasets/b.mat','Datasets/c.mat'};
for k = 1:3
S=load(matfilename{k});
fields=fieldnames(S);
data=s.(fields{1});
X = data(:,1:end-1);
Y = data(:,end);
[~,nCols] = size(X);
[Idx,Scores] = fscchi2(X,Y);
...
end
Next time, try to give the variables in your mat files the same name, so you can load to a struct array.

6 Comments

MB
MB on 18 Oct 2021
Actually "star strider" suggested the way you mentioned but deleted his/her answer. I hope he/she didn't get offended when I said I didn't solve my problem. I don't want to be rude. I wanted to load all the files in a loop without typing the names one by one. What if I have 10 files? Thanks to all of you for the help. Problem solved.
My suggested code relies on using a cell array, so you will have to create it some way. You can also use the dir call:
matFiles = dir('Datasets/*.mat');
matFiles=arrayfun(@(x)fullfile(x.folder,x.name),matFiles,'UniformOutput',false);
Or just compose the file name inside the loop since you're already using one.
MB
MB on 18 Oct 2021
Thank you both, Rik and Stephen. Much obliged.
I provided something substantially similar to that, however never received any feedback on what did or didn’t work, why it didn’t work, or anything else. I deleted my answer when it became obvious that my efforts were apparently futile.
MB
MB on 19 Oct 2021
I am a victim of misunderstanding. We had a communication problem. I am not a native speaker like you but I'm trying to do my best. My problem was reading .mat files sequentially and separating the last column and the rest into two different variables. I shared my code and a .mat file but they were deleted. Anyway seems like I had trouble explaining my problem. Your efforts were not futile and important for me.

Sign in to comment.

More Answers (0)

Products

Release

R2021b

Asked:

MB
on 14 Oct 2021

Commented:

MB
on 19 Oct 2021

Community Treasure Hunt

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

Start Hunting!