Deyan Levski
Deyan Levski

Reputation: 67

Matlab 2D plot — display axis in binary

I have a positive natural number vector which I am plotting in Matlab using the typical plot() function. Here is a sample plot:

Plotting a natural number vector, axis in radix-10, need to display them in radix-2

However, I need to see the vector (y axis) displayed in binary. Is there a way to change the axis display in binary (radix-2) ? I tried using dec2bin, but it only converts the integers to strings which can not be plotted.

Upvotes: 1

Views: 1083

Answers (1)

Florian
Florian

Reputation: 1824

How about this:

L = get(gca,'YTickLabel');
set(gca,'YTickLabel',cellfun(@(x) dec2bin(str2num(x)),L,'UniformOutput',false));

edit: Since you wanted the ability to zoom, here is a way to make the axis zoomable:

zh = zoom(gcf);
set(zh,'ActionPreCallBack',@(source,event,s) set(gca,'YTickLabelMode','auto'))
set(zh,'ActionPostCallBack',@(source,event,s) set(gca,'YTickLabel',cellfun(@(x) dec2bin(str2num(x)),get(gca,'YTickLabel'),'UniformOutput',false)));

It resets the axis to decimal before the zoom and then converts back to binary after the zoom.

Upvotes: 3

Related Questions