How to read comma separated .txt file in MATLAB ?

156 ビュー (過去 30 日間)
M
M 2022 年 7 月 1 日
回答済み: Cris LaPierre 2022 年 7 月 1 日
How to read comma separated .txt file in matlab ?
The rows is like this :
#A-R=-6.00,20.00,304.00
#A-R=-6.00,22.00,311.00
#A-R=-3.00,22.00,315.00
.
.
.
and so on
I want to store the columns after the "#A-R=" in 2D matrix, (I mean only the number values)

採用された回答

Cris LaPierre
Cris LaPierre 2022 年 7 月 1 日
I would use the settings available in readmatrix to make the '=' a delimiter. That will treat the '#A-R' as another column of data. I would then use the 'Range' input to skip the first column.
data = readmatrix("sampleData.txt",'Delimiter',{',','='},'Range','B1')
data = 3×3
-6 20 304 -6 22 311 -3 22 315

その他の回答 (1 件)

Abhishek Tiwari
Abhishek Tiwari 2022 年 7 月 1 日
編集済み: Abhishek Tiwari 2022 年 7 月 1 日
Hi @M
Try strsplit() or split() on rows and string array '#A-R' as delimiter. It divides each element of input at the specified delimiters. If you wish to separate number values, use strsplit() or split() with '#A-R=' and ',' as delimiters. Here are several examples:
temp = "#A-R=-6.00,20.00,304.00";
columns = strsplit(temp, "#A-R=")
columns = 1×2 string array
"" "-6.00,20.00,304.00"
sepColumns = strsplit(temp, ["#A-R=", ","])
sepColumns = 1×4 string array
"" "-6.00" "20.00" "304.00"
Before you begin, look into them.
  1. Split string or character vector at specified delimiter - MATLAB strsplit (mathworks.com)
  2. Split strings at delimiters - MATLAB split (mathworks.com)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by