Files
nappMATLAB-toolbox/import/importLabViewIssfile.m
T

108 lines
3.5 KiB
Matlab

function TableLabViewIss = importLabViewIssfile(filenameOrFolder, dataLines)
%IMPORTFILE Import data from a text file or folder
% TableLabViewIss = IMPORTFILE(FILENAME) reads data from text
% file FILENAME and returns the data as a table.
%
% TableLabViewIss = IMPORTFILE(FOLDER) reads all .txt files from
% FOLDER, merges them into a single table (sorted by datetime).
%
% TableLabViewIss = IMPORTFILE(FILE, DATALINES) reads data
% for the specified row interval(s) of text file FILENAME.
%
% Example (single file):
% TableLabViewIss = importLabViewIssfile("/Users/bartels-rausch/Desktop/17-12-2025_09-14datafile.txt", [2, Inf]);
%
% Example (folder):
% TableLabViewIss = importLabViewIssfile("/Users/bartels-rausch/Desktop/DataFolder");
%
% See also READTABLE.
%% Input handling
% If dataLines is not specified, define defaults
if nargin < 2
dataLines = [2, Inf];
end
%% Check if input is a file or folder
if isfolder(filenameOrFolder)
% Process all .txt files in the folder
folderPath = filenameOrFolder;
fileList = dir(fullfile(folderPath, '*.txt'));
if isempty(fileList)
error('No .txt files found in folder: %s', folderPath);
end
fprintf('Found %d .txt files in folder. Processing...\n', length(fileList));
% Initialize empty table
TableLabViewIss = table();
% Loop through each file
for i = 1:length(fileList)
filepath = fullfile(folderPath, fileList(i).name);
fprintf(' [%d/%d] Processing: %s\n', i, length(fileList), fileList(i).name);
% Import this file
tbl_temp = importSingleFile(filepath, dataLines);
% Append to main table
if isempty(TableLabViewIss)
TableLabViewIss = tbl_temp;
else
TableLabViewIss = [TableLabViewIss; tbl_temp];
end
end
% Sort by datetime
[~, sortIdx] = sort(TableLabViewIss.datetime);
TableLabViewIss = TableLabViewIss(sortIdx, :);
fprintf('Merged %d files into single table with %d rows.\n', length(fileList), height(TableLabViewIss));
else
% Process single file
TableLabViewIss = importSingleFile(filenameOrFolder, dataLines);
end
end
%% Helper function to import a single file
function tbl = importSingleFile(filename, dataLines)
% Import options setup
opts = delimitedTextImportOptions("NumVariables", 10);
% Specify range and delimiter
opts.DataLines = dataLines;
opts.Delimiter = "\t";
% Specify column names and types
opts.VariableNames = ["date_DD_MM_JJJJ_", "time_hh_mm_ss_", "pressure_cell_hPa_", "temperature__C_", "x_mm_", "y_mm_", "z_mm_", "Tsealing__C_", "flowCryo_l_min_", "pOut_hPa_"];
opts.VariableTypes = ["datetime", "duration", "double", "double", "double", "double", "double", "double", "double", "double"];
% Specify file level properties
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% Specify variable properties
opts = setvaropts(opts, "date_DD_MM_JJJJ_", "InputFormat", "dd.MM.yyyy");
opts = setvaropts(opts, "time_hh_mm_ss_", "InputFormat", "hh:mm:ss.S");
% Import the data as regular table
tbl = readtable(filename, opts);
% Merge date + time into a single datetime column
tbl.datetime = tbl.date_DD_MM_JJJJ_ + tbl.time_hh_mm_ss_;
% Set the datetime display format to show date and time
tbl.datetime.Format = 'dd.MM.yyyy HH:mm:ss.SSS';
% Remove the original date and time columns
tbl(:, ["date_DD_MM_JJJJ_", "time_hh_mm_ss_"]) = [];
% Move datetime to be the first column
tbl = movevars(tbl, "datetime", "Before", 1);
end