matlabplotbinarymagnitude

Matlab Binary Plot for Magnitude Representing the Whole 8 Bits


I am trying to achieve a magnitude binary plot, where y shows the binary magnitude for the 8 bits. Others have addressed this question through plotting individual bits, but the goal to show the whole 8 bits, represented as one number (i.e. 00000001 = 1), magnitude in binary form on the plot.

Here is the code:

     % Magnitude
     binMag = [00000001; 00000010; 00000100] 
% binary string representation of decimal 1, 2, 3
    % Tried to convert bin to a double
    binTodouble = str2double(binMag);
    figure;
    stairs(binTodouble)

Overall, my goal is to have a plot like the one below showing magnitude in binary. enter image description here


Solution

  • Taking the idea from Cris Luengo into account, you'll get the following short script. I just wanted to add how to specifically set the xticklabel and yticklabel as you wanted it to have in your plot.

    % Binary values
    bValues = { '00000000', 
                '00000001', 
                '00000010', 
                '00000011', 
                '00000100', 
                '00000101' };
    
    % Double values
    dValues = bin2dec(bValues);
    
    % Output
    figure(1);
    stairs(dValues, dValues);
    set(gca, 'xtick', dValues, 'xticklabel', dValues);
    set(gca, 'ytick', dValues, 'yticklabel', bValues);
    

    Stairs plot