Files
imdd_silas/Functions/showCurrentMeasurement.m
2024-10-24 19:41:59 +02:00

137 lines
4.7 KiB
Matlab

function showCurrentMeasurement(varargin)
% showCurrentMeasurement displays measurement data in a figure with variable names
% as column headers and values listed below. Calling the function multiple times
% with the same variable names but different values adds more data points.
%
% Usage:
% showCurrentMeasurement('VariableName1', VariableValue1, 'VariableName2', VariableValue2, ...)
%
% Example:
% % First measurement
% voltage = 5.12;
% ber = 3.86e-5;
% power = voltage * 0.85;
% showCurrentMeasurement('Voltage', voltage, 'ber', ber, 'Power', power);
%
% % Second measurement
% voltage = 5.15;
% ber = 2.54e-5;
% power = voltage * 0.90;
% showCurrentMeasurement('Voltage', voltage, 'ber', ber, 'Power', power);
% Validate that inputs are in name-value pairs
if mod(nargin, 2) ~= 0
error('Inputs must be provided as name-value pairs.');
end
% Extract variable names and values
numPairs = nargin / 2;
names = varargin(1:2:end);
values = varargin(2:2:end);
% Ensure variable names are strings
for i = 1:length(names)
if ~ischar(names{i}) && ~isstring(names{i})
error('Variable names must be strings.');
end
names{i} = char(names{i});
end
% Convert values to strings for display
for i = 1:length(values)
varNameLower = names{i}; % Convert variable name to lowercase for case-insensitive comparison
if isnumeric(values{i})
if any(strcmpi(varNameLower, {'ber','mlse','ffe','db'}))
% Format 'ber' values in exponential notation with two decimal places
values{i} = sprintf('%.2e', values{i});
else
values{i} = num2str(values{i});
end
else
values{i} = char(values{i});
end
end
% Define a unique tag for the figure to locate it later
figTag = 'CurrentMeasurementsFigure';
% Try to find an existing figure with the specified tag
hFig = findobj('Type', 'figure', 'Tag', figTag);
if isempty(hFig)
% Create a new figure and table
hFig = figure('Name', 'Current Measurements', 'NumberTitle', 'off', ...
'MenuBar', 'none', 'ToolBar', 'none', 'Resize', 'on', ...
'Tag', figTag);
% Initialize data and column names
data = values;
columnNames = names;
% Create the uitable
hTable = uitable('Parent', hFig, 'Data', data, ...
'ColumnName', columnNames, ...
'FontSize', 14, ...
'RowName', [], ...
'Units', 'normalized', ...
'Position', [0, 0, 1, 1]);
% Adjust column widths
setColumnWidths(hTable);
% Store the table handle for future use
setappdata(hFig, 'DataTable', hTable);
else
% Retrieve the existing table handle
hTable = getappdata(hFig, 'DataTable');
% Get current data and column names
currentData = get(hTable, 'Data');
columnNames = get(hTable, 'ColumnName');
% Ensure that variable names are consistent
if ~isequal(columnNames, names')
error('Variable names must be consistent with previous calls.');
end
% Append new data to the existing data
updatedData = [currentData; values];
% Update the table data
set(hTable, 'Data', updatedData);
% Adjust column widths
setColumnWidths(hTable);
end
% Adjust the figure size to fit the table content without changing its position
drawnow;
tableExtent = get(hTable, 'Extent');
% Get the current figure position
figPosition = get(hFig, 'Position');
% Update the figure size while preserving the position
figPosition(3) = max(figPosition(3), tableExtent(3) + 20); % Width
figPosition(4) = max(figPosition(4), tableExtent(4) + 20); % Height
set(hFig, 'Position', figPosition);
end
function setColumnWidths(hTable)
% Helper function to adjust column widths based on content
data = get(hTable, 'Data');
columnNames = get(hTable, 'ColumnName');
numColumns = length(columnNames);
columnWidths = cell(1, numColumns);
% Calculate the maximum width needed for each column
for col = 1:numColumns
maxContentLength = max(cellfun(@length, data(:, col)));
headerLength = length(columnNames{col});
maxLength = max(maxContentLength, headerLength);
% Estimate pixel width (approximate, adjust as needed)
pixelWidth = maxLength * 14; % 8 pixels per character as an estimate
columnWidths{col} = pixelWidth;
end
set(hTable, 'ColumnWidth', columnWidths);
end