chnages from silas pc again
This commit is contained in:
@@ -172,9 +172,9 @@ classdef Signal
|
||||
|
||||
hold on;
|
||||
if isempty(options.color)
|
||||
plot(t, sig(1:length(t)), 'DisplayName', dn, 'LineWidth', 0.1, 'Marker', '.', 'LineStyle','none', 'MarkerSize', 0.1);
|
||||
plot(t* 1e6, sig(1:length(t)), 'DisplayName', dn, 'LineWidth', 0.1, 'Marker', '.', 'LineStyle','none', 'MarkerSize', 0.1);
|
||||
else
|
||||
plot(t, sig(1:length(t)), 'DisplayName', dn, 'LineWidth', 0.1, 'Marker', '.', 'LineStyle','none', 'MarkerSize', 0.1,'Color',options.color);
|
||||
plot(t* 1e6, sig(1:length(t)), 'DisplayName', dn, 'LineWidth', 0.1, 'Marker', '.', 'LineStyle','none', 'MarkerSize', 0.1,'Color',options.color);
|
||||
end
|
||||
% 2 c)
|
||||
% - xlabel if not already here: time in readable format (1 ms and not 1e-3 s)
|
||||
@@ -187,10 +187,7 @@ classdef Signal
|
||||
ylabel('Amplitude');
|
||||
end
|
||||
|
||||
% Convert time axis to milliseconds for readability
|
||||
xticks = get(gca, 'XTick');
|
||||
set(gca, 'XTick', xticks, 'XTickLabel', xticks * 1e6);
|
||||
|
||||
|
||||
% Add legend if not already present
|
||||
if isempty(get(gca, 'Legend'))
|
||||
legend;
|
||||
@@ -656,7 +653,7 @@ classdef Signal
|
||||
end
|
||||
|
||||
%%
|
||||
function [obj,S,isFlipped,sequenceFound] = tsynch(obj,options)
|
||||
function [obj,S,inverted,sequenceFound,sequenceStarts] = tsynch(obj,options)
|
||||
% time sync and cut
|
||||
arguments
|
||||
obj Signal
|
||||
@@ -668,9 +665,9 @@ classdef Signal
|
||||
|
||||
|
||||
S = {};
|
||||
isFlipped=0;
|
||||
inverted = -1;
|
||||
sequenceFound = 0;
|
||||
|
||||
sequenceStarts = [];
|
||||
|
||||
|
||||
%normalize the signal
|
||||
@@ -711,19 +708,19 @@ classdef Signal
|
||||
|
||||
|
||||
shifts = lags(pkpos);
|
||||
|
||||
sequenceStarts = shifts;
|
||||
% shifts = shifts(shifts>=0);
|
||||
|
||||
if numel(shifts) > 0
|
||||
|
||||
%Cut occurences of ref signal from signal (only positive shifts)
|
||||
if all(sign(co(pkpos)))
|
||||
isFlipped = 1;
|
||||
if all(sign(co(pkpos))==-1)
|
||||
inverted = 1;
|
||||
end
|
||||
|
||||
for c = shifts
|
||||
sig = obj.delay(-c,'mode','samples');
|
||||
sig.signal = sig.signal(1:length(b)).*-isFlipped;
|
||||
sig.signal = sig.signal(1:length(b)) .* -inverted;
|
||||
S{end+1,1} = sig;
|
||||
end
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ classdef DBHandler < handle
|
||||
tableNames % Cell array containing names of all tables in the database
|
||||
tables = struct(); % Structure containing MATLAB tables for each database table
|
||||
distinctValues
|
||||
type
|
||||
end
|
||||
|
||||
methods
|
||||
@@ -21,6 +22,7 @@ classdef DBHandler < handle
|
||||
|
||||
arguments
|
||||
options.pathToDB = ""; % Default value for pathToDB if not provided
|
||||
options.type = "mysql";
|
||||
end
|
||||
|
||||
% Assign values to class properties based on input arguments
|
||||
@@ -33,7 +35,22 @@ classdef DBHandler < handle
|
||||
|
||||
% Establish a connection to the SQLite database
|
||||
try
|
||||
obj.conn = sqlite(obj.pathToDB);
|
||||
|
||||
if options.type == "sqlite"
|
||||
obj.conn = sqlite(obj.pathToDB);
|
||||
elseif options.type == "mysql"
|
||||
datasource = "jdbc:mysql://134.245.243.254:3306/labor";
|
||||
|
||||
obj.conn = database( ...
|
||||
"labor", ... % Database name
|
||||
"silas", ... % Username
|
||||
"silas", ... % Password (or getSecret)
|
||||
"Vendor", "MySQL", ...
|
||||
"Server", "134.245.243.254", ...
|
||||
"PortNumber", 3306, ...
|
||||
"JDBCDriverLocation", "C:\Users\Silas\Documents\mysql-connector-j-9.3.0\mysql-connector-j-9.3.0.jar");
|
||||
end
|
||||
|
||||
catch e
|
||||
error('Failed to connect to the database: %s', e.message);
|
||||
end
|
||||
@@ -59,8 +76,14 @@ classdef DBHandler < handle
|
||||
function obj = getTableNames(obj)
|
||||
% Get all table names from the database
|
||||
try
|
||||
result = fetch(obj.conn, 'SELECT name FROM sqlite_master WHERE type="table"');
|
||||
obj.tableNames = result.name;
|
||||
if obj.type == "mysql"
|
||||
result = fetch(obj.conn, 'SHOW TABLES;');
|
||||
obj.tableNames = result.Variables;
|
||||
elseif obj.type == "sqlite"
|
||||
result = fetch(obj.conn, 'SELECT name FROM sqlite_master WHERE type="table"');
|
||||
obj.tableNames = result.name;
|
||||
end
|
||||
|
||||
catch e
|
||||
error('Failed to retrieve table names: %s', e.message);
|
||||
end
|
||||
@@ -213,12 +236,16 @@ classdef DBHandler < handle
|
||||
elseif ischar(value)
|
||||
newRow.(colName{1}) = string(value);
|
||||
|
||||
elseif isdatetime(value)
|
||||
|
||||
% newRow.(colName{1}) = string(value);
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
% Parameters for retry logic
|
||||
maxRetries = 5;
|
||||
maxRetries = 50;
|
||||
basePause = 0.05; % seconds
|
||||
|
||||
attempt = 0;
|
||||
@@ -226,6 +253,9 @@ classdef DBHandler < handle
|
||||
|
||||
while ~success && attempt <= maxRetries
|
||||
try
|
||||
if obj.type == "mysql"
|
||||
newRow_ = obj.convertTableToCellStrings(newRow);
|
||||
end
|
||||
sqlwrite(obj.conn, tableName, newRow);
|
||||
success = true; % Write successful
|
||||
catch e
|
||||
@@ -245,7 +275,14 @@ classdef DBHandler < handle
|
||||
end
|
||||
|
||||
% Retrieve the measurement_id of the newly inserted row for linking other tables
|
||||
result = fetch(obj.conn, 'SELECT last_insert_rowid()');
|
||||
|
||||
if obj.type == "mysql"
|
||||
queue = "SELECT LAST_INSERT_ID()";
|
||||
elseif obj.type == "sqlite"
|
||||
queue = "SELECT last_insert_rowid()";
|
||||
end
|
||||
|
||||
result = fetch(obj.conn, queue);
|
||||
lastID = result{1, 1}; % Access the value directly from the table
|
||||
end
|
||||
|
||||
@@ -502,8 +539,87 @@ classdef DBHandler < handle
|
||||
|
||||
% Step 4: Execute the query and handle results
|
||||
result = obj.fetch(query);
|
||||
result = obj.normalizeMySQLTable(result);
|
||||
end
|
||||
|
||||
function cleanedTable = normalizeMySQLTable(~,result)
|
||||
cleanedTable = result;
|
||||
varNames = result.Properties.VariableNames;
|
||||
|
||||
for i = 1:numel(varNames)
|
||||
col = result.(varNames{i});
|
||||
|
||||
% Only process if column is a cell array of strings or chars
|
||||
if iscell(col) && all(cellfun(@(x) ischar(x) || isstring(x), col))
|
||||
% Try converting to numeric if possible
|
||||
numCol = str2double(col);
|
||||
if all(~isnan(numCol) | strcmpi(col, 'NaN'))
|
||||
% It's numeric (with possible NaNs)
|
||||
cleanedTable.(varNames{i}) = numCol;
|
||||
else
|
||||
% Clean double-quoted SQL literals (e.g., ""no_db"")
|
||||
cleanedTable.(varNames{i}) = strrep(string(col), '""', '"');
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function cellTable = convertTableToCellStrings(~,tbl)
|
||||
% Converts all variables in a MATLAB table to cell arrays of strings (like JDBC fetch from MySQL)
|
||||
%
|
||||
% Example:
|
||||
% jdbcFormatted = convertTableToCellStrings(myTable);
|
||||
|
||||
cellTable = tbl;
|
||||
varNames = tbl.Properties.VariableNames;
|
||||
|
||||
for i = 1:numel(varNames)
|
||||
col = tbl.(varNames{i});
|
||||
|
||||
if isnumeric(col)
|
||||
% Convert numeric values to strings
|
||||
cellTable.(varNames{i}) = arrayfun(@(x) num2str(x, '%.15g'), col, 'UniformOutput', false);
|
||||
|
||||
elseif isstring(col) || ischar(col)
|
||||
% Ensure cell array of strings
|
||||
cellTable.(varNames{i}) = cellstr(col);
|
||||
|
||||
elseif iscell(col)
|
||||
% Convert each cell entry to string
|
||||
cellTable.(varNames{i}) = cellfun(@convertToString, col, 'UniformOutput', false);
|
||||
|
||||
elseif islogical(col)
|
||||
% Convert logicals to '0' or '1'
|
||||
cellTable.(varNames{i}) = cellstr(string(double(col)));
|
||||
|
||||
elseif isdatetime(col)
|
||||
% Convert datetimes to formatted string
|
||||
cellTable.(varNames{i}) = cellstr(string(col));
|
||||
|
||||
else
|
||||
warning('Column "%s" has unsupported type. Converting using string().', varNames{i});
|
||||
cellTable.(varNames{i}) = cellstr(string(col));
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function out = convertToString(~,val)
|
||||
if ischar(val)
|
||||
out = val;
|
||||
elseif isstring(val)
|
||||
out = char(val);
|
||||
elseif isnumeric(val)
|
||||
out = num2str(val, '%.15g');
|
||||
elseif islogical(val)
|
||||
out = num2str(double(val));
|
||||
elseif isdatetime(val)
|
||||
out = datestr(val, 'yyyy-mm-dd HH:MM:SS');
|
||||
else
|
||||
out = char(string(val)); % fallback
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function query = constructSQLQuery(obj, filterParams, selectedFields)
|
||||
% constructSQLQuery Constructs the SQL query based on filter parameters and selected fields.
|
||||
%
|
||||
|
||||
Reference in New Issue
Block a user