How can I import a vector of vectors from txt file into a matrix

3 次查看(过去 30 天)
Hello,
I have a data.txt file that looks like this, for example:
[1,2,3,[1,2,3,4,5,6,7,8,9,10]]
[1,2,4,[2,3,4,5,6,7,8,9,10,11]]
[1,2,5,[3,4,5,6,7,8,9,10,11,12]]
the length of each line and each inner-vector, the forth element in each line, is always the same.
how can i import that data into a matrix that will contain the forth element as a vector of its own?
i have tried to import the data using 'importdata', which imported it as a (numOfLines*1) cell and using 'cell2mat' but i got that error:
Error using cat
Dimensions of arrays being concatenated are not consistent.
Error in cell2mat (line 83)
m{n} = cat(1,c{:,n});

回答(1 个)

KSSV
KSSV 2022-2-4
fid = fopen('data.txt') ; % your data file
S = textscan(fid,'[%f,%f,%f,[%f,%f,%f,%f,%f,%f,%f,%f,%f,%f]]');
fclose(fid) ;
S = cell2mat(S) ;
A = S(:,1:3) ;
B = S(:,4:end) ;

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

产品


版本

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by