Matlab获取char的值 [英] matlab get the value of char

查看:958
本文介绍了Matlab获取char的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MATLAB命令行中,当我输入变量a时,它会为我提供预期的值:

from MATLAB command line , when I type my variable a , it gives me values as expected :

a =


            value_1
            value_2

并且我想访问a的每个值,我尝试了a(1),但这给了我空白 a的类型为1x49char. 我如何获得value_1和value_2?

and I would like to access to each value of a, I tried a(1) but this gives me empty the type of a is 1x49char. how could I get value_1 and value_2 ?

 whos('a')
  Name      Size            Bytes  Class    Attributes

  a         1x49               98  char 

我从xml文件中获取了一个

I get the a from xml file :

<flag ="value">
    <flow>toto</flow>
     <flow>titi</flow>
 </flag>

a + 0:

ans =    
    10  32  32   32  32  32  32  32  32  32  32  32  32  98,...
   111 111 108  101  97 110  95  84  10  32  32  32  32  32,...
   32   32  32   32  32  32  32  66  79  79  76  10  32  32,...
   32   32  32   32  32  32  32

推荐答案

您似乎有一些不方便的字符数组.您可以通过执行类似@Richante所说的操作来以更易于管理的形式转换此数组:

You seem to have an somewhat inconvenient character array. You can convert this array in a more manageable form by doing something like what @Richante said:

strings = strread(a, '%s', 'delimiter', sprintf('\n'));

然后您可以通过

>> b = strings{2}
b = 
toto

>> c = strings{3}
c = 
titi

请注意,由于a以换行符开头,因此strings{1}为空.还要注意,您不需要strtrim了-已经由strread照顾了.您可以这样做来规避初始换行符

Note that strings{1} is empty, since a starts with a newline character. Note also that you don't need a strtrim -- that is taken care of by strread already. You can circumvent the initial newlines by doing

strings = strread(a(2:end), '%s', 'delimiter', sprintf('\n'));

但是我只会在 all 情况下第一个换行符始终存在的情况下执行此操作.我宁愿这样做

but I'd only do that if the first newline is consistently there for all cases. I'd much rather do

strings = strread(a, '%s', 'delimiter', sprintf('\n'));
strings = strings(~cellfun('isempty', strings))

最后,如果您更愿意使用textscan而不是strread,则需要多做1个步骤:

Finally, if you'd rather use textscan instead of strread, you need to do 1 extra step:

strings = textscan(a, '%s', 'delimiter', sprintf('\n'));
strings = [strings{1}(2:end)];

这篇关于Matlab获取char的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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