Help with iterations over large file.

9 ビュー (過去 30 日間)
gero pulido
gero pulido 2023 年 12 月 11 日
コメント済み: Dyuman Joshi 2023 年 12 月 12 日
Hello everyone, I have a huge ascii.txt wind file with the following format.
time = 0 hour
2 3 4 5 6 7
0 9 8 7 6 5
7 6 5 4 3 2
9 8 7 6 5 4
time = 3 hour
2 3 4 5 6 7
0 9 8 7 6 5
7 6 5 4 3 2
9 8 7 6 5 4
The thing is that I need to flip the rows from each block between hours, like this
time = 0 hour
9 8 7 6 5 4
7 6 5 4 3 2
0 9 8 7 6 5
2 3 4 5 6 7
time = 3 hour
9 8 7 6 5 4
7 6 5 4 3 2
0 9 8 7 6 5
2 3 4 5 6 7
I can do this for one block by
X = readlines('xwind.txt');
Y = X(2:5);
Y = flip(Y);
But I couldn’t write the loop for the entire file (4865x1 string).
Any help would be much appreciated.

採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 12 月 11 日
If the format of the data is homogenous through the file, try this -
in = readlines('ascii.txt')
in = 10×1 string array
"time = 0 hour" "2 3 4 5 6 7 " "0 9 8 7 6 5" "7 6 5 4 3 2" "9 8 7 6 5 4" "time = 3 hour" "2 3 4 5 6 7 " "0 9 8 7 6 5" "7 6 5 4 3 2" "9 8 7 6 5 4"
for k=2:5:size(in,1)
in(k:k+3,:) = flipud(in(k:k+3,:));
end
in
in = 10×1 string array
"time = 0 hour" "9 8 7 6 5 4" "7 6 5 4 3 2" "0 9 8 7 6 5" "2 3 4 5 6 7 " "time = 3 hour" "9 8 7 6 5 4" "7 6 5 4 3 2" "0 9 8 7 6 5" "2 3 4 5 6 7 "
  2 件のコメント
gero pulido
gero pulido 2023 年 12 月 12 日
Thank you! It worked perfectly
Dyuman Joshi
Dyuman Joshi 2023 年 12 月 12 日
You're welcome!

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

その他の回答 (1 件)

Mathieu NOE
Mathieu NOE 2023 年 12 月 11 日
hello
try this
I choosed to store each Y array into a cell
D=readlines('data.txt'); % read as string array
[eof] = size(D,1); % last row number (end of file)
ixP1=find(contains(D,'time')); % find the "time" line
ixP1=[ixP1 ; eof]; % add end of file line number
for k=1:numel(ixP1)-1
ind = ixP1(k)+1:ixP1(k+1)-1;
Y = str2num(char(D(ind)));
Y = flip(Y);
out{k} = Y; % store Y flipped in cell array
end

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by