Effacer les filtres
Effacer les filtres

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

3 vues (au cours des 30 derniers jours)
Eli Zatulovski
Eli Zatulovski le 4 Fév 2022
Réponse apportée : KSSV le 4 Fév 2022
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});

Réponses (1)

KSSV
KSSV le 4 Fév 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) ;

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by