Matlab convert string & ldquo; Aaa & rdquo; At [& ldquo; A & rdquo;, & ldquo; A & rdquo;, & ldquo; A & rdquo;] array

advertisements

So I'm working in Matlab and having a real problem trying to understand how strings work in Matlab.

Basically I'm trying to load a string into a double array in its binary version.

So basically i want to load "a" and get [0 1 1 0 0 0 0 1] out.

I have the following below that converts a string to its binary representation but this only leads to me having "a" = ["01100001"] when I convert it to an array and [1100001] when converted to a number using str2num

    function loadString(obj, string)
        binaryString = reshape(dec2bin(string, 8), 1, []);

    end

So basically I'm looking for a way to convert a string to an array of characters ie "aaa" -> ["a" "a" "a"]. That way I convert the binaryString to ["0" "1" "1" "0" "0" "0" "0" "1"] then convert that array using str2num.


Is this what you're looking for:

z = dec2bin('abc',8)'
z1 = z(:)'
z2 = arrayfun(@str2double, z1)

z1 =
011000010110001001100011

z2 =
     0     1     1     0     0     0     0     1     0     1     1     0     0     0     1     0     0     1     1     0     0     0     1     1