Clear Filters
Clear Filters

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

3 views (last 30 days)
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});

Answers (1)

KSSV
KSSV on 4 Feb 2022
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) ;

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!