转换字符串cellfun为num [英] Convert string to num with cellfun

查看:305
本文介绍了转换字符串cellfun为num的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下一个问题:

A=[2014;20;2012;32155];

然后我想 A 转换在字符串中的字符一个单元阵列,以字符 A prepended每个串。换句话说,我想这样做,如果我是创建一个手动电池阵列:

Then I want to convert A in a cell array with string characters, with the character A prepended to each of the strings. In other words, I wish to do this if I were to create a manual cell array:

A={'A2014';'A20';'A2012';'A32155'};

任何人都可以做这个而不使用循环?谢谢!

Can anybody make this without using a loop? Thanks!

推荐答案

通过您的编辑,你能做到这一点,首先你的数字数组转换成字符串单元阵列就像你说的:

With your edit, you can do this to first convert your numeric array into a cell array of strings like you said:

A = num2cell(A);
A = cellfun(@num2str, A, 'UniformOutput', false);

一旦你这样做,如果我的理解是正确的,希望每个字符串前插入一个 A 字符。因此,你可以这样做:

Once you do this, if my understanding is correct, you want to insert an A character before each string. As such, you can do this:

A = cellfun(@(x) ['A' x], A, 'UniformOutput', false);

您会从而得到:

A = 

 'A2014'
 'A20'
 'A2012'
 'A32144'

这篇关于转换字符串cellfun为num的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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