Files
nappMATLAB-toolbox/utils/plotExportedData.m
T

152 lines
4.8 KiB
Matlab

function plotExportedData(dataDirectory)
%plotExportedData Plot all exported ASCII data files in one figure
%
% This function reads all .txt files from the exported data (created by
% exportFigureData) and plots them in a single figure for visual comparison.
% File names are parsed to extract axis labels and legend entries.
%
% SYNTAX:
% plotExportedData() % Uses current working directory
% plotExportedData(dataDirectory) % Uses specified directory path
%
% INPUTS:
% dataDirectory : char or string (optional)
% Path to directory containing exported .txt files.
% If omitted, uses current working directory (pwd).
%
% OUTPUT:
% A new figure with all data plotted as lines. Axis labels and legend
% entries are reconstructed from the file names.
%
% NOTES:
% - Reads all .txt files in the specified directory
% - File name format expected: <FigureTitle>_[<TileTitle>_]<YLabel>_vs_<XLabel>_<LineLabel>.txt
% - Plots are created with underscores converted back to spaces for display
% - X and Y axis labels are set based on parsed filename
% - Legend shows line labels from filenames
% - All data is plotted in a single axes for direct visual comparison
%
% EXAMPLE:
% % After running exportFigureData():
% plotExportedData()
% % or
% plotExportedData('../exported_data/')
% Handle input: use current directory if none provided
if nargin == 0 || isempty(dataDirectory)
dataDirectory = pwd;
end
% Get all .txt files in the directory
fileStruct = dir(fullfile(dataDirectory, '*.txt'));
if isempty(fileStruct)
warning('plotExportedData:NoFiles', 'No .txt files found in %s', dataDirectory);
return;
end
% Create a new figure
fig = figure('Name', 'Exported Data Comparison', 'NumberTitle', 'off');
ax = axes(fig);
hold(ax, 'on');
grid(ax, 'on');
% Storage for axis labels (to ensure consistency)
xLabelStr = '';
yLabelStr = '';
legendEntries = {};
% Loop over all .txt files
for ii = 1:length(fileStruct)
fileName = fileStruct(ii).name;
filePath = fullfile(dataDirectory, fileName);
% Read the data
try
data = readmatrix(filePath);
catch ME
warning('plotExportedData:ReadFailed', 'Could not read file: %s\n%s', fileName, ME.message);
continue;
end
% Check if data has at least 2 columns
if size(data, 2) < 2 || size(data, 1) < 1
warning('plotExportedData:InvalidData', 'File %s does not have valid X,Y columns', fileName);
continue;
end
xData = data(:, 1);
yData = data(:, 2);
% Parse filename to extract labels
% Expected format: <FigureTitle>_[<TileTitle>_]<YLabel>_vs_<XLabel>_<LineLabel>.txt
% Remove .txt extension
nameNoExt = fileName(1:end-4);
% Split by '_vs_' to separate y-axis label from the rest
parts = strsplit(nameNoExt, '_vs_');
if length(parts) >= 2
% Extract line label (after _vs_<XLabel>_)
rightPart = parts{2}; % <XLabel>_<LineLabel>
% Find the last underscore to separate XLabel from LineLabel
underscores = find(rightPart == '_');
if ~isempty(underscores)
lastUnderscore = underscores(end);
xLabelExtracted = rightPart(1:lastUnderscore-1);
lineLabel = rightPart(lastUnderscore+1:end);
else
xLabelExtracted = rightPart;
lineLabel = 'data';
end
% Extract y-axis label (before _vs_)
yLabelExtracted = parts{1};
% Remove figure title (first part before first underscore)
% This is tricky, so we'll keep what we have and note it's approximate
% Convert underscores back to spaces for display
xLabelDisplay = strrep(xLabelExtracted, '_', ' ');
yLabelDisplay = strrep(yLabelExtracted, '_', ' ');
lineLabel = strrep(lineLabel, '_', ' ');
% Store axis labels (use the first valid ones found)
if isempty(xLabelStr)
xLabelStr = xLabelDisplay;
end
if isempty(yLabelStr)
yLabelStr = yLabelDisplay;
end
else
% Fallback if filename parsing fails
lineLabel = nameNoExt;
end
% Plot the data
plot(ax, xData, yData, 'DisplayName', lineLabel, 'LineWidth', 1.5, 'Marker', 'o', 'MarkerSize', 4);
% Store legend entry
legendEntries{end+1} = lineLabel;
end
% Set axis labels
xlabel(ax, xLabelStr, 'Interpreter', 'none');
ylabel(ax, yLabelStr, 'Interpreter', 'none');
title(ax, 'Exported Data Comparison', 'Interpreter', 'none');
% Add legend
if ~isempty(legendEntries)
legend(ax, legendEntries, 'Interpreter', 'none', 'Location', 'best');
end
% Optimize figure appearance
grid(ax, 'on');
hold(ax, 'off');
fprintf('Plotted %d data files from: %s\n', length(fileStruct), dataDirectory);
end