Unzipping MDF4 files and combining them into 1
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
endI 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 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 mdf4, matlab, import, mdfimport MATLAB Answers — New Questions