Display of an ECG in a PDF with millimetre scaling.

3 views (last 30 days)
Hello,
I need help reading in ECG data from a CSV file and then outputting it as a PDF file. The ECG data consists of time values in seconds and voltage values in volts.
Please note the following specific requirements for the PDF presentation:
1. axis scaling:
- A small box should represent 0.02 seconds, while a large box should represent 0.1 seconds.
- A small box should represent 0.01 mV, and a large box should represent 0.5 mV.
2. formatting:
- The X-axis should show time in seconds, and the Y-axis should show voltage in mV.
3. PDF output:
- The PDF document should be in landscape format and contain multiple lines of the ECG
Could you please tell me how I can implement the scaling in the PDF? I would also be grateful for sample code or specific functions that I can use to fulfil these requirements.
%%
clear
addpath(genpath('..')); % add parent directory
% load 1 lead ECG signal
data = readtable('ecg_sample.csv', 'Delimiter', ';');
set(gcf,'PaperUnits','centimeters');
set(gcf,'PaperSize',[29.7 21]);
fig = gcf;
fig.PaperUnits = 'centimeters';
fig.PaperPosition = [0 0 29.7 21];
fig.Units = 'centimeters';
% figure;
% ax1 = subplot(1,1,1);
plot(data.timesincestart(1:8000), data.leadVoltage(1:8000),'LineWidth', 2);
% Set grid and ticks grid on grid minor
xlim([0 5]);
ylim([-0.0004 0.0002]);
hold on;
grid on
grid minor
set(gca, 'GridLineStyle', '-'); % Durchgezogene Linie
set(gca, 'GridAlpha', 0.5); % Transparenz (0 bis 1)
set(gca, 'GridColor', 'k'); % Farbe auf Schwarz setzen
% X-Achse: 1 mm = 0.04 s (25 mm/s)
% 1 small square corresponds to 1/50 of a second, which is 0.02 s.
xticks(0:0.1:5);
xticklabels([]);
ax = gca;
ax.MinorGridLineStyle = '-';
ax.MinorGridAlpha = 0.2;
ax.XAxis.MinorTick = 'on';
ax.XAxis.MinorTickValues = 0:0.02:5;
ax.XAxis.MinorTick = 'on';
% Y-Achse: 1 mm = 0.1 mV (10 mm/mV)
% 10 small squares correspond to 1 mV
% 1 large square corresponds to 0.5 mV
yticks(-0.0004:0.00005:0.0002);
ax.YAxis.MinorTick = 'on';
ax.YAxis.MinorTickValues = -0.0004:0.00001:0.0002;
% %pbaspect([1 1 1])
% ax.PlotBoxAspectRatio = [1 1 1];
ax.DataAspectRatio = [2000 1 1];
set(gca, 'XTickLabel', []);
set(gca, 'YTickLabel', []);
% sace and open pdf
print(fig,'myFigPDF','-dpdf','-r1200');
winopen('myFigPDF.pdf')

Answers (1)

Ronit
Ronit on 20 Sep 2024
Hello Paul,
To implement the scaling and formatting requirements for your ECG data visualization, you can use the following suggestions and get the desired result:
  • Axis Limits: Sets the X-axis to display from 0 to 5 seconds and the Y-axis from -1 to 1 mV, accommodating the simulated data range.
xlim([0 5]);
ylim([-1 1]);
  • Y-Axis Configuration: Sets major ticks every 0.1 mV and minor ticks every 0.01 mV, with the label "Voltage (mV)".
% Configure Y-axis ticks and labels
yticks(-1:0.1:1);
ax.YAxis.MinorTick = 'on';
ax.YAxis.MinorTickValues = -1:0.01:1;
ylabel('Voltage (mV)');
  • It seems like the axis labels are not displaying because the "xticklabels([])" and "yticklabels([])" commands are clearing the labels on the "X" and "Y" axes. To fix this and display the time and voltage labels on the axes, you should remove or comment out those lines.
Please refer to the documentation of "xticklabels" for more understanding: https://www.mathworks.com/help/matlab/ref/xticklabels.html#bvaeadq-1
Now, combining the above suggestions and implementing them gives the following:
(Note: Using a randomly created dataset to simulate results, please use your dataset here.)
clear
addpath(genpath('..')); % add parent directory
% load 1 lead ECG signal
data = readtable('ecg_sample.csv', 'Delimiter', ',');
set(gcf,'PaperUnits','centimeters');
set(gcf,'PaperSize',[29.7 21]);
fig = gcf;
fig.PaperUnits = 'centimeters';
fig.PaperPosition = [0 0 29.7 21];
fig.Units = 'centimeters';
% figure;
% ax1 = subplot(1,1,1);
plot(data.timesincestart(1:8000), data.leadVoltage(1:8000),'LineWidth', 2);
% Set grid and ticks grid on grid minor
xlim([0 5]);
ylim([-1 1]);
grid on
grid minor
set(gca, 'GridLineStyle', '-'); % Durchgezogene Linie
set(gca, 'GridAlpha', 0.2); % Transparenz (0 bis 1)
set(gca, 'GridColor', 'k'); % Farbe auf Schwarz setzen
% X-Achse: 1 mm = 0.04 s (25 mm/s)
% 1 small square corresponds to 1/50 of a second, which is 0.02 s.
xticks(0:0.1:5);
ax = gca;
ax.XAxis.MinorTick = 'on';
ax.XAxis.MinorTickValues = 0:0.02:5;
xlabel('Time (s)');
% Y-Achse: 1 mm = 0.1 mV (10 mm/mV)
% 10 small squares correspond to 1 mV
% 1 large square corresponds to 0.5 mV
yticks(-1:0.1:1);
ax.YAxis.MinorTick = 'on';
ax.YAxis.MinorTickValues = -1:0.01:1;
ylabel('Voltage (mV)');
% sace and open pdf
print(fig,'myFigPDF','-dpdf','-r1200');
% Uncomment the following line to get the pdf output (attached)
% winopen('myFigPDF.pdf')
I hope it helps your query!

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!