How can I export fields from a series of .mat struct files into seperate .csv files in a loop while maintaining the field names as the new file names?

3 ビュー (過去 30 日間)
Vítor
Vítor 2023 年 4 月 11 日
編集済み: Arjun 2024 年 9 月 30 日
I have a bunch of struct files inside a folder. The only files present in the folder are the .mat struct files. Each .mat file is a 1x1 struct with 8 fields, and each field is a M x 5 double (M is constant inside the same struct, varying only between struct files). Both the .mat files and the fields are named according to a code for future reference (for simplicity and confidentiality, lets say the .mat files are named like APA1470fG; APA2470fG; APA3480fG (...) and the fields are named like APA4701hJ; APA4702hJ; APA480kJ (...). I need to export each field as a separate .csv file for analysis, but I wanted to create a loop so that I wouldn't have to export each and every single one manually, as there are simply too many fields in total that need to be exported, but I also need to be careful about the file names. Just to note that the actual names for both the struct and fields are very complex, so I don't see a way to load or save files using a logical naming approach in order to keep the existing names.
So, what I wanted to make was a loop that goes as follows:
  • Load each struct from the main folder at a time;
  • Create a folder inside the main folder with the same name as the .mat struct file;
  • Save each field as a .csv file with the same name as the field and save it on the created folder.
I would deeply appreciate if anyone could help me with my problem.

回答 (1 件)

Arjun
Arjun 2024 年 9 月 30 日
編集済み: Arjun 2024 年 9 月 30 日
As per my understanding, you want to perform file manipulation involving .mat file. Specifically, you want to read .mat files that contain a 1x1 struct and then export each field of this struct as a separate .csv file. Each .csv file should be placed inside a dedicated folder corresponding to the .mat file from which it originated.
We can follow the following steps for automating this process:
  • Set the path to the folder containing .mat files
  • Use the “dir” function to list all the .mat files in the folder
  • Load the data from each .mat file
  • Use the “fieldnames” function to obtain the name of the struct’s fields
  • Create new folder named after each .mat file
  • Loop through each field in the struct and save it as a .csv file using “writematrix” function.
You can refer to the following code for better understanding:
% Address of the folder contain gthe .mat file
mainFolder = 'path_to_your_folder containing .mat files';
% Get a list of all .mat files in the folder
matFiles = dir(fullfile(mainFolder, '*.mat'));
% Scan each .mat file, extract the struct and fields
for k = 1:length(matFiles)
% Load the kth .mat file after fetching the name
matFileName = matFiles(k).name;
matFilePath = fullfile(mainFolder, matFileName);
dataStruct = load(matFilePath);
% Extract the name of the fields of the struct
structFieldNames = fieldnames(dataStruct);
mainStruct = dataStruct.(structFieldNames{1});
% Create a new folder with the same name as the .mat file
[~, folderName, ~] = fileparts(matFileName);
newFolderPath = fullfile(mainFolder, folderName);
if ~exist(newFolderPath, 'dir')
mkdir(newFolderPath);
end
% Get field names from the struct
fieldNames = fieldnames(mainStruct);
% Loop through each field in the struct
for fieldIdx = 1:length(fieldNames)
fieldName = fieldNames{fieldIdx};
fieldData = mainStruct.(fieldName);
% Create a CSV file for each field
csvFileName = [fieldName '.csv'];
csvFilePath = fullfile(newFolderPath, csvFileName);
% Write the field data to a CSV file
writematrix(fieldData, csvFilePath);
end
end
Please go through the documentation of “dir”, “fieldnames” and “writematrix” functions for better understanding.
I hope this will help!

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by