-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertResultsToExcel.m
More file actions
65 lines (53 loc) · 2.04 KB
/
convertResultsToExcel.m
File metadata and controls
65 lines (53 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
function convertResultsToExcel(matFilePath)
% If matFilePath is not provided, open a GUI to select the file
if nargin < 1 || isempty(matFilePath)
[fileName, filePath] = uigetfile('*.mat', 'Select the .mat file');
if fileName == 0
error('No file selected. Exiting function.');
end
matFilePath = fullfile(filePath, fileName);
end
% Generate the Excel file path
[filePath, fileName, ~] = fileparts(matFilePath);
excelFilePath = strcat(filePath,'\', fileName, '.xlsx');
% Check if the .mat file exists
if ~isfile(matFilePath)
error('The specified .mat file does not exist.');
end
% Load the all_results.mat file
data = load(matFilePath);
allResults = data.allResults;
% Get all the field names (dates)
dateFields = fieldnames(allResults);
% Initialize a cell array to store the data
allData = {};
% Iterate over each date field
for i = 1:numel(dateFields)
date = dateFields{i};
resultStruct = allResults.(date);
% Get all subfield names for the current date
try
subfields = fieldnames(resultStruct);
catch
continue
end
subfields = subfields(3:end);
% Extract subfield values
subfieldValues = struct2cell(resultStruct)';
subfieldValues = subfieldValues(3:end);
% Create a cell array row with the date and subfield values
row = subfieldValues;
% Append the row to the allData cell array
if isempty(allData)
allData = row;
else
allData = [allData; row];
end
end
% Create a table from the cell array
varNames = subfields';
allDataTable = cell2table(allData, 'VariableNames', varNames);
% Write the table to an Excel file
writetable(allDataTable, excelFilePath);
fprintf('Data successfully written to %s\n', excelFilePath);
end