dan12345
dan12345

Reputation: 114

Matlab plot of several digital signals

I'm trying to find a way to nicely plot my measurement data of digital signals.

So I have my data available as csv and mat file, exported from an Agilent Oscilloscope. The reason I'm not just taking a screen shot of the Oscilloscope screen is that I need to be more flexible (make several plots with one set of data, only showing some of the lines). Also I need to be able to change the plot in a month or two so my only option is creating a plot from the data with a computer.

What I'm trying to achieve is something similar to this picture: Goal The only thing missing on that pic is a yaxis with 0 and 1 lines.

My first try was to make a similar plot with Matlab. Here's what I got: What I have so far

What's definitely missing is that the signal names are right next to the actual line and also 0 and 1 ticks on the y-axis.

I'm not even sure if Matlab is the right tool for this and I hope you guys can give me some hints/a solution on how to make my plots :-)

Here's my Matlab code:

clear;
close all;
clc;

MD.RAW = load('Daten/UVLOT1 debounced 0.mat');  % get MeasurementData

MD.N(1) = {'INIT\_DONE'};
MD.N(2) = {'CONF\_DONE'};
MD.N(3) = {'NSDN'};
MD.N(4) = {'NRST'};
MD.N(5) = {'1V2GD'};
MD.N(6) = {'2V5GD'};
MD.N(7) = {'3V3GD'};
MD.N(8) = {'5VGD'};
MD.N(9) = {'NERR'};
MD.N(10) = {'PGD'};
MD.N(11) = {'FGD'};
MD.N(12) = {'IGAGD'};
MD.N(13) = {'GT1'};
MD.N(14) = {'NERRA'};
MD.N(15) = {'GT1D'};
MD.N(16) = {'GB1D'};

% concat vectors into one matrix
MD.D = [MD.RAW.Trace_D0, MD.RAW.Trace_D1(:,2), MD.RAW.Trace_D2(:,2), MD.RAW.Trace_D3(:,2), ...
    MD.RAW.Trace_D4(:,2), MD.RAW.Trace_D5(:,2), MD.RAW.Trace_D6(:,2), MD.RAW.Trace_D7(:,2), ...
     MD.RAW.Trace_D8(:,2), MD.RAW.Trace_D9(:,2), MD.RAW.Trace_D10(:,2), MD.RAW.Trace_D11(:,2), ...
      MD.RAW.Trace_D12(:,2), MD.RAW.Trace_D13(:,2), MD.RAW.Trace_D14(:,2), MD.RAW.Trace_D15(:,2)];

cm = hsv(size(MD.D,2)); % make colormap for plot

figure;
hold on;

% change timebase to ns
MD.D(:,1) = MD.D(:,1) * 1e9;

% plot lines
for i=2:1:size(MD.D,2)
    plot(MD.D(:,1), MD.D(:,i)+(i-2)*1.5, 'color', cm(i-1,:));
end

hold off;

legend(MD.N, 'Location', 'EastOutside');
xlabel('Zeit [ns]');        % x axis label
title('Messwerte');         % title
set(gca, 'ytick', []);      % hide y axis

Thank you guys for your help! Dan

EDIT: Here's a pic what I basically want. I added the signal names via text now the only thing that's missing are the 0, 1 ticks. They are correct for the init done signal. Now I just need them repeated instead of the other numbers on the y axis (sorry, kinda hard to explain :-) Almost what I want

Upvotes: 2

Views: 1747

Answers (3)

dan12345
dan12345

Reputation: 114

This is the end result and all my code, in case anybody else wants to use the good old ctrl-v ;-)

Result

Code:

clear;
close all;
clc;

MD.RAW = load('Daten/UVLOT1 debounced 0.mat');  % get MeasurementData

MD.N(1) = {'INIT\_DONE'};
MD.N(2) = {'CONF\_DONE'};
MD.N(3) = {'NSDN'};
MD.N(4) = {'NRST'};
MD.N(5) = {'1V2GD'};
MD.N(6) = {'2V5GD'};
MD.N(7) = {'3V3GD'};
MD.N(8) = {'5VGD'};
MD.N(9) = {'NERR'};
MD.N(10) = {'PGD'};
MD.N(11) = {'FGD'};
MD.N(12) = {'IGAGD'};
MD.N(13) = {'GT1'};
MD.N(14) = {'NERRA'};
MD.N(15) = {'GT1D'};
MD.N(16) = {'GB1D'};

% concat vectors into one matrix
MD.D = [MD.RAW.Trace_D0, MD.RAW.Trace_D1(:,2), MD.RAW.Trace_D2(:,2), MD.RAW.Trace_D3(:,2), ...
    MD.RAW.Trace_D4(:,2), MD.RAW.Trace_D5(:,2), MD.RAW.Trace_D6(:,2), MD.RAW.Trace_D7(:,2), ...
     MD.RAW.Trace_D8(:,2), MD.RAW.Trace_D9(:,2), MD.RAW.Trace_D10(:,2), MD.RAW.Trace_D11(:,2), ...
      MD.RAW.Trace_D12(:,2), MD.RAW.Trace_D13(:,2), MD.RAW.Trace_D14(:,2), MD.RAW.Trace_D15(:,2)];

cm = hsv(size(MD.D,2)); % make colormap for plot

figure;
hold on;

% change timebase to ns
MD.D(:,1) = MD.D(:,1) * 1e9;

% plot lines
for i=2:1:size(MD.D,2)
    plot(MD.D(:,1), MD.D(:,i)+(i-2)*2, 'color', cm(i-1,:));
    text(MD.D(2,1), (i-2)*2+.5, MD.N(i-1));
end

hold off;

%legend(MD.N, 'Location', 'EastOutside');
xlabel('Zeit [ns]');        % x axis label
title('Messwerte');         % title
% make y axis and grid the way I want it
set(gca, 'ytick', 0:size(MD.D,2)*2-3);   
grid off;
set(gca,'ygrid','on');
set(gca, 'YTickLabel', {'0'; '1'});
ylim([-1,(size(MD.D,2)-1)*2]);

Upvotes: 1

The Minion
The Minion

Reputation: 1164

So as written in my comment to the question. For appending Names to each signal I would recommend searching the documentation of how to append text to graph. There you get many different ways how to do it. You can change the position (above, below) and the exact point of data. As an example you could use:

text(x_data, y_data, Var_Name,'VerticalAlignment','top');

Here (x_data, y_data) is the data point where you want to append the text and Var_Name is the name you want to append.

For the second question of how to get a y-data which contains 0 and 1 values for each signal. I would do it by creating your signal the way, that your first signal has values of 0 and 1. The next signal is drawn about 2 higher. Thus it changes from 2 to 3 and so on. That way when you turn on y-axis (grid on) you get values at each integer (obviously you can change that to other values if you prefer less distance between 2 signals). Then you can relabel the y-axis using the documentation of axes (check the last part, because the documentation is quite long) and the set() function:

set(gca, 'YTick',0:1:last_entry, 'YTickLabel',new_y_label(0:1:last_entry))

Here last_entry is 2*No_Signals-1 and new_y_label is an array which is constructed of 0,1,0,1,0,....

Upvotes: 1

optimist
optimist

Reputation: 1018

For viewing y axis, you can turn the grid('on') option. However, you cannot chage the way the legends appear unless you resize it in the matlab figure. If you really want you can insert separate textboxes below each of the signal plots by using the insert ->Textbox option and then change the property (linestyle) of the textbox to none to get the exact same plot as above.

Upvotes: 1

Related Questions