Clear Filters
Clear Filters

Unzipping MDF4 files and combining them into 1

13 views (last 30 days)
I am trying unzip a collection of mdf4 files, combine them into 1 mdf4 file and save it. I am getting the following error (attached):
'Time_10Hz' does exist in the dataset as shown (attached)
Any help would be appreciated! Code is attached below
% Open file selection dialog to choose MDF files
[fileNames, folderPath] = uigetfile('*.mdf;*.zip', 'Select MDF or ZIP files', 'MultiSelect', 'on');
if ~ischar(fileNames) % Check if any file is selected
combinedData = struct(); % Initialize an empty structure to store combined data
for i = 1:numel(fileNames)
filePath = fullfile(folderPath, fileNames{i});
% Check if the file is a ZIP archive
if endsWith(filePath, '.zip')
% Unzip the archive to a temporary folder
tempFolder = tempname;
unzip(filePath, tempFolder);
% Find the MDF file(s) in the temporary folder
mdfFiles = dir(fullfile(tempFolder, '*.mdf'));
for j = 1:numel(mdfFiles)
mdfFilePath = fullfile(tempFolder, mdfFiles(j).name);
data = mdfimport(mdfFilePath);
% Extract desired channel groups and signals
desiredChannelGroups = {'32'}; % Replace with your desired channel groups
desiredSignals = {'YawRate', 'EPBMode'}; % Replace with your desired signals
for k = 1:numel(desiredChannelGroups)
channelGroup = desiredChannelGroups{k};
if isfield(data, channelGroup)
for l = 1:numel(desiredSignals)
signal = desiredSignals{l};
if isfield(data.(channelGroup), signal)
if isfield(combinedData, [channelGroup '.' signal])
combinedData.([channelGroup '.' signal]) = [combinedData.([channelGroup '.' signal]), data.(channelGroup).(signal)];
else
combinedData.([channelGroup '.' signal]) = data.(channelGroup).(signal);
end
end
end
end
end
end
% Clean up the temporary folder
rmdir(tempFolder, 's');
else
% Import the MDF file directly
data = mdfimport(filePath);
% Extract desired channel groups and signals
desiredChannelGroups = {'32'}; % Replace with your desired channel groups
desiredSignals = {'YawRate', 'EPBMode'}; % Replace with your desired signals
for k = 1:numel(desiredChannelGroups)
channelGroup = desiredChannelGroups{k};
if isfield(data, channelGroup)
for l = 1:numel(desiredSignals)
signal = desiredSignals{l};
if isfield(data.(channelGroup), signal)
if isfield(combinedData, [channelGroup '.' signal])
combinedData.([channelGroup '.' signal]) = [combinedData.([channelGroup '.' signal]), data.(channelGroup).(signal)];
else
combinedData.([channelGroup '.' signal]) = data.(channelGroup).(signal);
end
end
end
end
end
end
end
% Create a timetable from the combined data structure
timeField = 'Time_10Hz'; % Assuming 'Time' is the field containing time information
combinedTimetable = timetable(combinedData.(timeField), combinedData);
% Save the combined data to a new MDF file
[fileName, folderPath] = uiputfile('*.mdf', 'Save Combined MDF File');
if fileName ~= 0 % Check if a file name is provided
outputFilePath = fullfile(folderPath, fileName);
mdfWrite(outputFilePath, combinedTimetable);
msgbox('MDF files combined successfully!', 'Success');
end
end

Answers (1)

Samay Sagar
Samay Sagar on 24 Apr 2024
In my understanding., the problem lies in the handling of the time field and combining the MDF files. Instead, you can use the “mdfRead” function to read MDF files as timetables and then append these timetables and store the resultant table as MDF file using “mdfWrite”. In this case, you will not need to manually specify the time fields as the entire tables will be concatenated.
Here is a revised approach to your task:
[fileNames, folderPath] = uigetfile('*.mf4;*.zip', 'Select MDF or ZIP files', 'MultiSelect', 'on');
if ~ischar(fileNames) && ~isempty(fileNames) % Check if any file is selected
combinedTimetable = timetable(); % Initialize an empty timetable to store combined data
for i = 1:numel(fileNames)
filePath = fullfile(folderPath, fileNames{i});
% Check if the file is a ZIP archive
if endsWith(filePath, '.zip')
% Unzip the archive to a temporary folder
tempFolder = tempname;
unzip(filePath, tempFolder);
% Find the MDF file(s) in the temporary folder
mdfFiles = dir(fullfile(tempFolder, '*.mf4'));
for j = 1:numel(mdfFiles)
mdfFilePath = fullfile(tempFolder, mdfFiles(j).name);
tt = mdfRead(mdfFilePath);
% Append the data
if size(combinedTimetable, 1) == 0
combinedTimetable = tt{:};
else
combinedTimetable = [combinedTimetable; tt{:}];
end
end
% Clean up the temporary folder
rmdir(tempFolder, 's');
else
% Import the MDF file directly as a timetable
tt = mdfRead(filePath);
% Append the data
if size(combinedTimetable, 1) == 0
combinedTimetable = tt{:};
else
combinedTimetable = [combinedTimetable; tt{:}];
end
end
end
combinedTimetable = sortrows(combinedTimetable);
% Save the combined data to a new MDF file
[fileName, folderPath] = uiputfile('*.mf4', 'Save Combined MDF File');
if fileName ~= 0 % Check if a file name is provided
outputFilePath = fullfile(folderPath, fileName);
mdfWrite(outputFilePath, combinedTimetable);
msgbox('MDF files combined successfully!', 'Success');
end
end
For more information about “mdfRead” and “mdfWrite”, you can refer the following documentation:
Hope this resolves your query.

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!