convert a .mat to excel file

Hello, I have so many .mat files that I want to convert them to excel.
the first one (If I want to do one by one) name is A_Q1S1.mat.
What is the correct code for that?

4 Kommentare

Dyuman Joshi
Dyuman Joshi am 2 Apr. 2024
Verschoben: Dyuman Joshi am 4 Apr. 2024
(Assuming single data set in each file)
Read the data from the files and use writematrix to save the data in excel files accordingly.
Refer to this link to see how to read data from files in a sequence - https://in.mathworks.com/help/matlab/import_export/process-a-sequence-of-files.html
hello
t would be nice to have a few files to know how the names are structured and what is the data inside (and what we need to retrieve)
a general approach could look like :
fileDir = pwd; % current directory (or specify which one is the working directory)
S = dir(fullfile(fileDir,'Data*.mat')); % get list of data files in directory
% optionnal robust file name sorting (if needed)
S = natsortfiles(S); % sort file names into natural order , see :
%(https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
for k = 1:length(S)
filename = S(k).name % to actually show filenames are sorted (see command window)
data = load( fullfile(fileDir, filename)); % output structure
% store out_data in excel file
out_data = [data.Field. ....]; % here extract data from output structure
outfile = [filename(1:length(filename)-4) '.xlsx']; % output file name
writematrix(out_data,fullfile(fileDir,outfile));
end
mehra
mehra am 2 Apr. 2024
My .mat data names are like :
A_Q1S1
A_Q1S2
A_Q1S3
A_Q2S1
A_Q2S2
A_Q2S3
A_Q3S1
A_Q3S2
A_Q3S3
the same is for B and C.
Should I do something more for the whole files?
Mathieu NOE
Mathieu NOE am 3 Apr. 2024
ok , we don't need any special approach for loading your different mat files
but what we miss here is :
  • what data you have stored in the mat files ,
  • which elements you want to export to excel
if you could share a couple of mat files ,that would be great

Melden Sie sich an, um zu kommentieren.

Antworten (1)

KSSV
KSSV am 4 Apr. 2024

0 Stimmen

matFiles = dir('*.mat') ;
N = length(matFiles) ;
T = table;
for i =1:N
load(matFiles(i).name) ;
T.(i) = val ; % I assume each mat file has a variable "val" which is single column of same size
end
writetable(T,'Test.xlsx')

Gefragt:

am 2 Apr. 2024

Beantwortet:

am 4 Apr. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by