Separate one column into 3 when finding a character

I have one column line but huge data >400,000 points separated by a character /& like this
1
2
3
4
/&
5
6
7
8
/&
9
10
11
12
/&
I want to split this one column into three columns stops at the separator/& like :
1 5 9
2 6 10
3 7 11
4 8 12

 採用された回答

OCDER
OCDER 2017 年 10 月 11 日
編集済み: OCDER 2017 年 10 月 12 日

0 投票

New Answer:
FID = fopen('data.txt');
T = fscanf(FID, '%f');
fseek(FID, 0, 'bof');
Data = fscanf(FID, [repmat('%f\n', 1, length(T)) '/&'], [length(T) Inf]);
fclose(FID);
Old Answer:
FID = fopen('data.txt', 'r');
Data = fscanf(FID, '%f\n%f\n%f\n%f\n/&', [4 Inf])
fclose(FID);
Data =
1 5 9
2 6 10
3 7 11
4 8 12

3 件のコメント

Mohamed Gamal
Mohamed Gamal 2017 年 10 月 12 日
Dear Donald:
The problem is that I do not have just four rows from my data I have unknown number sometimes more than 100,000 row.
Also I have tried the code but it could not see the separators "/&"
But anyway thank you for your effort.
OCDER
OCDER 2017 年 10 月 12 日
Hi Mohamed, try the new answer and see if this works. It uses the first scan to determine how many entries there are until the first non-double character (/&), and then uses that information to read in all the data into a Mx3 array.
Mohamed Gamal
Mohamed Gamal 2017 年 10 月 14 日
Dear Donald I found the answer based on your comment 1-I have used fscanf to count the numbers 2-Use reshape fuction to split the 1 column to 3 column 3-use Save to save this regular shape matrix. here is the code:
fid3=fopen('c:\mat3\CH_AN1.dat'); % reshape the one column matrix into "3 Channels" T = fscanf (fid3, '%15E'); % to read ascii data L=(length (T)/3); B = reshape(T,L,3); save 'c:\mat3\save20.dat' B -ascii fclose(fid3);
Thanks alot for your help

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

その他の回答 (0 件)

タグ

質問済み:

2017 年 10 月 11 日

コメント済み:

2017 年 10 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by