Trenera
Trenera

Reputation: 1505

MATLAB - Cell arrays to a matrix

I need to transform 2 cell arrays into one matrix. I have the following code:

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]

,but I get:

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); 

Upvotes: 1

Views: 65

Answers (1)

Trenera
Trenera

Reputation: 1505

Thanks to Dan:

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

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

Upvotes: 2

Related Questions