function reorganize_data() basePath = '../'; rawDataPath = fullfile(basePath, 'raw_data'); % Get all .mat files in the rawDataPath files = dir(fullfile(rawDataPath, '**/*.mat')); for i = 1:length(files) filePath = fullfile(files(i).folder, files(i).name); data = load(filePath); if isfield(data, 'tireid') && isfield(data, 'testid') tireid = data.tireid; testType = formatTestType(data.testid); newDir = fullfile('../sorted_data', tireid); if ~exist(newDir, 'dir') mkdir(newDir); end newFilePath = fullfile(newDir, sprintf('%s.mat', testType)); if exist(newFilePath, 'file') mergeData(newFilePath, filePath); else copyfile(filePath, newFilePath); end else warning('Required fields not found in %s', filePath); end end end function testType = formatTestType(testid) switch lower(testid) case 'cornering' testType = 'cornering'; case 'drive/brake/combined' testType = 'drivebrake'; otherwise error('Unknown testid: %s', testid); end end function mergeData(mergedFilePath, newFilePath) % Load existing and new data existingData = load(mergedFilePath); newData = load(newFilePath); % Merge logic with error checking for singular values fieldNames = fieldnames(newData); for i = 1:length(fieldNames) fieldName = fieldNames{i}; if isnumeric(newData.(fieldName)) || islogical(newData.(fieldName)) if numel(newData.(fieldName)) == 1 % Singular value if isfield(existingData, fieldName) && existingData.(fieldName) ~= newData.(fieldName) error('Mismatch in singular value for field "%s" between existing and new data.', fieldName); end elseif isfield(existingData, fieldName) existingData.(fieldName) = [existingData.(fieldName); newData.(fieldName)]; else existingData.(fieldName) = newData.(fieldName); end elseif ~strcmp(fieldName, 'tireid') && ~strcmp(fieldName, 'testid') if isfield(existingData, fieldName) && ~isequal(existingData.(fieldName), newData.(fieldName)) error('Mismatch in non-array field "%s" between existing and new data.', fieldName); end existingData.(fieldName) = newData.(fieldName); end end % Save the merged data save(mergedFilePath, '-struct', 'existingData'); end