Matlab字符到字符串的转换问题。使用什么功能?

发布于 2024-11-28 22:45:23 字数 259 浏览 8 评论 0原文

x = 1234     56789     7654

x(1) 是 1,x(2) 是 2 等等...中间有 5 个空格.. 尺寸(x) = 1 23 1行23列 我尝试过使用 num2str、strcat 但我无法合并数字。 y = num2str(x), y = strcat(x)

我希望它是.. x(1) = 1234, x(2) = 56789, x(3) = 7654

我应该使用什么函数来完成上述操作?

x = 1234     56789     7654

x(1) is 1, x(2) is 2 and so on... there are 5 spaces in between..
size(x) = 1 23
One row with 23 columns
I've tried using num2str, strcat but I cannot club the numbers.
y = num2str(x), y = strcat(x)

I want it to be.. x(1) = 1234, x(2) = 56789, x(3) = 7654

What function should I use to accomplish the above?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

假装不在乎 2024-12-05 22:45:23

简单的解决方案是使用 sscanf:

x =' 1234     56789     7654'

sscanf(x, '%d')

ans =

    1234
   56789
    7654

Simple solution is to use sscanf:

x =' 1234     56789     7654'

sscanf(x, '%d')

ans =

    1234
   56789
    7654
顾忌 2024-12-05 22:45:23

有多种方法可以执行您想要的操作。其中之一是 strtok。

x = '1234     56789     7654';
[fst rest] = strtok(x,' ');

There are several ways of performing what you want. One of them is strtok.

x = '1234     56789     7654';
[fst rest] = strtok(x,' ');
再可℃爱ぅ一点好了 2024-12-05 22:45:23

STR2NUM 非常适合此任务:

>> x = '1234     56789     7654';
>> x = str2num(x)'

x =

        1234
       56789
        7654

STR2NUM works well for this task:

>> x = '1234     56789     7654';
>> x = str2num(x)'

x =

        1234
       56789
        7654
栖迟 2024-12-05 22:45:23

只是为了添加另一个答案......

y = textscan(x, '%d     %d     %d')

Just to add another answer to the mix...

y = textscan(x, '%d     %d     %d')
旧时浪漫 2024-12-05 22:45:23

下面创建一个字符串元胞数组,然后使用 sscanf 应用程序。

b = regexp(x,'\d+','match');
y = cellfun(@(a) (sscanf(a,'%d')),b); 

The following creates a cell array of strings then follows it up with an application of sscanf.

b = regexp(x,'\d+','match');
y = cellfun(@(a) (sscanf(a,'%d')),b); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文