2015-05-28 161 views
1

我需要将2个细胞阵列转换成一个矩阵。 我有以下代码:MATLAB - 细胞阵列到矩阵

clear all 
close all 
clc 

Data = {'2';'0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00 0.00000E+00';'3';'2.55059E+02 -1.51068E-01 1.98598E+01 3.05054E-02 -3.33973E+00 5.20304E+00';'4';'2.91592E+02 -1.60734E-01 2.65596E+01 1.18310E-02 -8.48549E-01 3.26528E+01';'5';'2.95371E+02 -8.31506E-03 2.73774E+01 -3.12620E-02 3.45210E-01 4.89854E+01';'6';'2.95163E+02 -3.67915E-02 2.73430E+01 5.67954E-03 3.93966E-01 4.91073E+01';'7';'2.91656E+02 3.63959E-02 2.86178E+01 -5.36138E-02 1.01910E+00 3.36354E+01';'8';'2.39894E+02 -5.92872E-02 2.53735E+01 1.04208E-02 2.55075E+00 7.28200E+00';'9';'1.56770E+02 6.15987E+01 3.07648E+01 -1.27722E+01 -6.82190E+00 4.29358E+00';'10';'3.14601E+01 2.74269E+01 -8.55639E+00 -3.92134E+00 -8.17611E+00 -7.48109E-01';'11';'-1.56914E+01 -2.33817E+00 -4.48457E+01 3.01897E+00 3.16196E-01 -6.26759E+00'}; 

A = Data(1:2:end,1); 
B = Data(2:2:end,1); 

A_new = cellfun(@str2num, A); 
B_new = cellfun(@str2num, B); 

M = [A_new B_new] 

,但我得到:

Error using cellfun 
Non-scalar in Uniform output, at index 1, output 1. 
Set 'UniformOutput' to false. 
Error in ddd (line 11) 
B_new = cellfun(@str2num, B); 
+0

的错误是相当明显的,它会告诉你去尝试'cellfun( @ str2num,A,'UniformOutput',false)'。 (我怀疑你会遇到一个新的问题,虽然你已经这样做了) – Dan

+0

这个修改允许我运行代码,而不是M = [10×7]矩阵,它是一个单元阵列:M(1, :) = [2] [1x6双] – Trenera

+0

我最终只需要“cell2mat(M)”。谢谢 – Trenera

回答

2

感谢Dan:

A_new = cellfun(@str2num, A, 'UniformOutput', false); 
B_new = cellfun(@str2num, B, 'UniformOutput', false); 

M = [A_new B_new]; 
Result = cell2mat(M) 
+0

更短的是,如果你做了'A_new = cellfun(....,'uni',0);'。减少打字,它做同样的事情:) – rayryeng