フィルターのクリア

How to read a specially structured data file

13 ビュー (過去 30 日間)
Jake
Jake 2023 年 2 月 27 日
編集済み: Jake 2023 年 2 月 27 日
I have a *.5P file (file is an output from the software WAMIT, and can be read by Matlab and/or any text editor) that I want to read through Matlab. Since I cannot upload a *.5P file here in the forum, I changed it to a *.txt file and attached the sample file here.
Now, one line of this data file would include 19 columns (each separated by a tab or space). However, the specific structure of the output file "wraps" each data line to include only 15 columns, and the next 4 lines go into a new line. I'm trying the following code to read the data.
fid = fopen('test.5p');
C = cell2mat(textscan(fid, '%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f'));
fclose(fid);
But this doesn't give me the structure I want, and instead, I get something similar to the following.
How can I read these wrapped lines to be one single line?
TIA!
** EDIT: I realized that I made a mistake with the question and the actual structure of the data file is a bit more complex than what I thought and had attached here. I posted a separate question detailing that.

採用された回答

Stephen23
Stephen23 2023 年 2 月 27 日
fid = fopen('test.txt','rt');
mat = fscanf(fid,'%f',[19,Inf]).';
fclose(fid);
display(mat)
mat = 300×19
-1.0000 1.0000 1.0000 0.0303 0 -0.0446 0 0.1337 0 -0.0016 0 0.0647 0 0.0461 0 -0.0446 0 0.0154 0 -1.0000 1.0000 2.0000 0.0318 0 -0.0505 0 0.1375 0 -0.0017 0 0.0679 0 0.0521 0 -0.0505 0 0.0190 0 -1.0000 1.0000 3.0000 0.0305 0 -0.0402 0 0.1327 0 -0.0013 0 0.0649 0 0.0418 0 -0.0402 0 0.0153 0 -1.0000 1.0000 4.0000 0.0301 0 -0.0314 0 0.1318 0 -0.0010 0 0.0634 0 0.0327 0 -0.0314 0 0.0115 0 -1.0000 1.0000 5.0000 0.0308 0 -0.0316 0 0.1329 0 -0.0009 0 0.0646 0 0.0330 0 -0.0316 0 0.0127 0 -1.0000 1.0000 6.0000 0.0316 0 -0.0416 0 0.1357 0 -0.0012 0 0.0672 0 0.0433 0 -0.0416 0 0.0173 0 -1.0000 1.0000 7.0000 0.0326 0 -0.0533 0 0.1392 0 -0.0017 0 0.0700 0 0.0550 0 -0.0533 0 0.0217 0 -1.0000 1.0000 8.0000 0.0324 0 -0.0444 0 0.1379 0 -0.0012 0 0.0692 0 0.0461 0 -0.0444 0 0.0196 0 -1.0000 1.0000 9.0000 0.0333 0 -0.0559 0 0.1413 0 -0.0016 0 0.0722 0 0.0575 0 -0.0559 0 0.0245 0 -1.0000 1.0000 10.0000 0.0349 0 -0.0650 0 0.1443 0 -0.0021 0 0.0741 0 0.0663 0 -0.0650 0 0.0277 0
  1 件のコメント
Jake
Jake 2023 年 2 月 27 日
Thank you @Stephen23, I accepted your answer because it aligned with what I had in mind. However, I'm so sorry that I had posted my question with a mistake. I posted a separate question detailing that. I'm sorry again.

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

その他の回答 (2 件)

Jan
Jan 2023 年 2 月 27 日
編集済み: Jan 2023 年 2 月 27 日
[fid, msg] = fopen('test.5p');
assert(fid > 0, '%s', msg);
C = fscanf(fid, '%g', inf); % Read all in one block
fclose(fid);
C = reshape(C, 19, []).'; % Reshape afterwards
  1 件のコメント
Jake
Jake 2023 年 2 月 27 日
Thank you @Jan! However, I'm so sorry that I had posted my question with a mistake. I was in a hurry, and overlooked the structure of my file, only to realize that the actual structure is a bit more complex than what I had stated. I posted a separate question detailing that. I'm sorry again.
Further, I had accepted Stephen23's answer since it aligned a bit more with what I had in mind :)

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


Askic V
Askic V 2023 年 2 月 27 日
編集済み: Askic V 2023 年 2 月 27 日
I would also like to suggest my naive and inefficient approach:
A = dlmread('testfile5p.txt');
nr_rows = size(A,1);
A2 = zeros(ceil(size(A,1)/2),19);
for i = 1:nr_rows-1
A2(i,:) = [A(i,:), A(i+1,1:4)];
end
B = A2(1:2:end,:);
The solution with using Inf plus transponse is really mind blowing. I would never expect that.

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by