フィルターのクリア

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

3 ビュー (過去 30 日間)
Eli Zatulovski
Eli Zatulovski 2022 年 2 月 4 日
回答済み: KSSV 2022 年 2 月 4 日
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 Center および File ExchangeData Type Conversion についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by