Matlab转换字符串"aaa"到["a","a","a"]数组 [英] Matlab convert string "aaa" to ["a", "a", "a"] array

查看:144
本文介绍了Matlab转换字符串"aaa"到["a","a","a"]数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆