首页 > 解决方案 > 将元胞数组转换为单个数组

问题描述

开始使用 Matlab 和 mySQL,作为我的查询的结果,我得到了这种类型的数组:

my_result =

  5×1 cell array

    {'a'}
    {'b'}
    {'c'}
    {'d'}
    {'e'}

我想得到一个像这样的简单数组:

[a, b, c, d, e]

这是我的代码:

mysql( 'open', 'my_database', 'usr','passw' )

query = fileread('query1.sql');           
query = sprintf(query)

my_result = mysql(query);

我尝试获得一个简单的数组:

my_array = []
for i=1:length(my_result)
   my_array = [my_array, my_result{i}];
end


>> my_array

my_array =

    'abcde'

>> cell2mat(my_result)
Error using cat
Dimensions of arrays being concatenated are not consistent.

Error in cell2mat (line 83)
            m{n} = cat(1,c{:,n});
 

有没有办法首先获得正确的格式,或者轻松正确地转换它?谢谢

标签: mysqlsqlmatlab

解决方案


只需使用 cell2mat 内置函数

a{1} =  'a' 
a{2} =  'b' 
a{3} =  'c' 
a{4} =  'd' 
a{5} =  'e' 


a =

  5×1 cell array

    {'a'}
    {'b'}
    {'c'}
    {'d'}
    {'e'}


cell2mat(a)

ans =

  5×1 char array

    'a'
    'b'
    'c'
    'd'
    'e'

推荐阅读