Matlab转换字符串"aaa"到["a","a","a"]数组 [英] Matlab convert string "aaa" to ["a", "a", "a"] array
问题描述
所以我正在Matlab中工作,试图理解字符串在Matlab中的工作方式时遇到了一个真正的问题.
So I'm working in Matlab and having a real problem trying to understand how strings work in Matlab.
基本上,我正在尝试将字符串以二进制版本加载到double数组中.
Basically I'm trying to load a string into a double array in its binary version.
所以基本上我想加载"a"并得到[0 1 1 0 0 0 0 0 1].
So basically i want to load "a" and get [0 1 1 0 0 0 0 1] out.
下面我将字符串转换为二进制表示形式,但这仅导致我将其转换为数组时将"a" = ["01100001"]转换为字符串,使用str2num将其转换为数字时将显示为[1100001]
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
所以基本上我正在寻找一种将字符串转换为字符数组的方法,即"aaa"-> ["a""a""a"].这样,我将binaryString转换为["0""1""1""0""0""0""0""1"],然后使用str2num转换该数组.
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
这篇关于Matlab转换字符串"aaa"到["a","a","a"]数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!