How to read txt file and put the words on a vector and the numbers in a matrix?

1 回表示 (過去 30 日間)
Alexandre
Alexandre 2023 年 10 月 10 日
コメント済み: Star Strider 2023 年 10 月 10 日
Hi
how do i extract the words and the numbers and put them in a vector(for words) and matrix(for numbers) with this txt file in this format:
Salmorrete, Vila Ló, Pião, Tortolindo, Chão de Frades
0 104 97 33 205
0 0 46 57 410
0 0 0 154 73
0 0 0 0 322
0 0 0 0 0
  1 件のコメント
dpb
dpb 2023 年 10 月 10 日
See general use of the new(ish) string class example <word frequency example> for ideas on how to get started.
Post back when/if you get stuck somewhere...

サインインしてコメントする。

採用された回答

Star Strider
Star Strider 2023 年 10 月 10 日
If that is the exact file, readtable is going to have problems with it.
Using fgetl and textscan
type('Alexandre_2023_10_10.txt')
Salmorrete, Vila Ló, Pião, Tortolindo, Chão de Frades 0 104 97 33 205 0 0 46 57 410 0 0 0 154 73 0 0 0 0 322 0 0 0 0 0
fidi = fopen('Alexandre_2023_10_10.txt','rt');
VN = fgetl(fidi)
VN = 'Salmorrete, Vila Ló, Pião, Tortolindo, Chão de Frades'
C = textscan(fidi, '%f%f%f%f%f', 'CollectOutput',1);
Matrix = cell2mat(C)
Matrix = 5×5
0 104 97 33 205 0 0 46 57 410 0 0 0 154 73 0 0 0 0 322 0 0 0 0 0
fclose(fidi);
.
  2 件のコメント
Alexandre
Alexandre 2023 年 10 月 10 日
Thank you!
Star Strider
Star Strider 2023 年 10 月 10 日
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeText Data Preparation についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by