From bafadead8329f955bc8dfd676a996315d9b9b000 Mon Sep 17 00:00:00 2001
From: Thomas Lambert <dev@tlambert.be>
Date: Tue, 22 Nov 2022 23:06:20 +0100
Subject: [PATCH] feat(scripts): matlab importer

---
 CHANGELOG.md      |  4 ++-
 scripts/csv2mat.m | 74 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 77 insertions(+), 1 deletion(-)
 create mode 100644 scripts/csv2mat.m

diff --git a/CHANGELOG.md b/CHANGELOG.md
index b495d62..3b46a60 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
 The format is based on [Keep a Changelog][keep_chglog], and this project adheres
 to [Semantic Versioning][sem_ver]. In the context of this repo, the semantic
 versioning is based mainly on the CSV files. Major releases indicate a breaking
-change in the formatting of the CSV files (column rorder, renaming, etc). Minor
+change in the formatting of the CSV files (column reorder, renaming, etc). Minor
 versions indicate addition of new data.
 
 ## [Unreleased]
@@ -19,6 +19,8 @@ format to use for the geometry files.
 - Caradonna1981: Geometry and (partial) data
 - Landgrebe1971: Geometry and data
 
+- csv2mat: Script to load csv files into Matlab tables
+
 ### Changed
 
 ### Deprecated
diff --git a/scripts/csv2mat.m b/scripts/csv2mat.m
new file mode 100644
index 0000000..3d45f6d
--- /dev/null
+++ b/scripts/csv2mat.m
@@ -0,0 +1,74 @@
+function RawTable = csv2mat(datafile, autosave)
+    % csv2mat Import a CSV file into a Matlab Table (and save it as a MAT-file).
+    %   This function loads CSV data files into a Matlab Table and add some useful metadata whenever
+    %   possible to add more context.
+    %   The resulting table is then returned and saved as a MAT-file automatically (if the users
+    %   choses to).
+    % -----
+    %
+    %   Notes:
+    %     The input CSV file <strong>must</strong> be formatted as follow:
+    %         # Title
+    %         # Tags: tags
+    %         heading1, heading2, heading3, ...
+    %         unit1   , unit2   , unit3, ...
+    %         data    , data    , data    , ...
+    %         data    , data    , data    , ...
+    %         ...     , ...     , ...     , ...
+    % -----
+    %
+    % Syntax:
+    %   RawTable = csv2mat(datafile) loads the file `datafile` into the table `RawTable`.
+    %   RawTable = csv2mat(datafile, 'save') loads the file `datafile` into the table `RawTable` and
+    %   save the table in a MAT-file automatically.
+    %
+    % Inputs:
+    %   datafile : Name of the csv or txt file containing the data (ex: 'mydata.csv')
+    %   autosave : (optional) If false (default), table will not be saved. If anything else than
+    %   'false', the table will be saved to a MAT-file.
+    %
+    % Outputs:
+    %   RawTable : A Table corresponding to the CSV data provided in input.
+    %
+    % Examples:
+    %   ExpDataTable = csv2mat('experiment1.csv')
+    %   ExpDataTable = csv2mat('experiment1.csv', true)
+    %   ExpDataTable = csv2mat('experiment1.csv', 'save')
+
+    % -----------------------------------------
+    % (c) Copyright 2022 University of Liege
+    % Author: Thomas Lambert <t.lambert@uliege.be>
+    % ULiege - Aeroelasticity and Experimental Aerodynamics
+    % MIT License
+    % Repo: https://gitlab.uliege.be/thlamb/rotare
+    % Docs: https://gitlab.uliege.be/thlamb/rotare-doc
+    % Issues: https://gitlab.uliege.be/thlamb/rotare/-/issues
+    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+
+    % Input checks
+    if ~exist(datafile, 'file')
+        error('csv2mat:FileNotFound', 'The file ''%s'' was not found in Matlab''s PATH.', datafile);
+    end
+    if nargin < 2
+        autosave = false;
+    end
+
+    % Keep file path and name for MAT-file
+    [filepath, filename, ~] = fileparts(datafile);
+    savefile = fullfile(filepath, filename);
+
+    % -----------------------------------------
+    % Load CSV table
+    opts = detectImportOptions(datafile);
+    opts.VariableNamesLine = 3;
+    opts.VariableUnitsLine = 4;
+    opts.DataLines = [5 Inf];
+    RawTable = readtable(datafile, opts);
+
+    % -----------------------------------------
+    % Save data
+    if autosave ~= false
+        save(savefile, 'RawTable');
+    end
+
+end
-- 
GitLab