Hi All,
I want to read data from a DAT file. The file has many lines with strings and numbers appearing at known positions. How can I read from a specific line number and column number on the dat file?
Best regards Rehan

回答 (1 件)

dpb
dpb 2016 年 1 月 3 日

0 投票

Sequential files are, well, "sequential". There's not a way to read them as random-access; best one can do(*) is to read records sequentially until get where one wants to go and then parse the lines. Generally, one is better to read the full file content to memory and process there unless it's truly humongous.
(*) Under certain conditions, one can maneuver around by knowing record lengths and such, but similarly to the old saw of "if have to ask the price, you can't afford it" if one has to ask "how?", it's more complicated than you want to tackle.
doc fgetl
doc sscanf
doc textscanf
for the above. As you can see by looking at the number of questions regarding formatted input on the forum, if you attach a section of the particular file, people will generally show you how to write appropriate format strings to parse it. If it's just a mixture, read the examples carefully first; it's probably not too difficult...

2 件のコメント

Rehan Jamshed
Rehan Jamshed 2016 年 1 月 3 日
編集済み: dpb 2016 年 1 月 3 日
Thanks. Here is the actual text to be read.
2 N_MAT = total number of materials
isotr 1 72e9 0.3 2700
ortho 1 141.96D9 9.79D9 9.79D9 0.42 0.42 0.5 6D9 6D9 4.83D9 1.445D3
From the above, all information is to be read except the text "N_MAT = total number of materials"
dpb
dpb 2016 年 1 月 3 日
OK, that's not very complicated but -- what is known a priori about the rest of the structure or is this it? That is, if the first record is '2' materials, are the following to records it or is there then some other set of "stuffs" yet to come? Does one know if parse the string for each of the N materials the size of the subsequent number of elements in the record or is that also unknown? Is the structure of the file always as shown or can the M numeric values/record be on more than a single record? "So few answers, so many questions..." :)

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

カテゴリ

ヘルプ センター および File ExchangeData Import and Analysis についてさらに検索

質問済み:

2016 年 1 月 2 日

コメント済み:

dpb
2016 年 1 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by