Anyone,
I want to extract the data in the end row.
The meaningful data comes every "0" changes in the first row...
Best,
Pen

2 件のコメント

dpb
dpb 2016 年 2 月 4 日
Paste the data as text rather than as an image if you expect somebody to actually do anything other than "air code"
Pengju
Pengju 2016 年 2 月 4 日
I am sorry for that.

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

 採用された回答

Star Strider
Star Strider 2016 年 2 月 4 日

0 投票

One option:
fidi = fopen('your_text_file_name.txt','r');
Data = textscan(fidi, '%f%f%f', 'HeaderLines',4, 'CollectOutput',1);
fclose(fidi);
You might have to add an 'EndOfLine' argument if you only get a (1x1) cell from the previous code:
fidi = fopen('your_text_file_name.txt','r');
Data = textscan(fidi, '%f%f%f', 'HeaderLines',4, 'CollectOutput',1, 'EndOfLine','\r\n');
fclose(fidi);
You can then parse the columns as you wish. Use a cell array. Find the zeros in the first column using the find function:
DataD = cell2mat(Data);
Zeros1 = [find(DataD(:,1) == 0); size(DataD,1)];
for k1 = 1:length(Zeros1)
Record{k1} = DataD(Zeros1(k1):Zeros1(k1+1)-1,:);
end
Note This is UNTESTED CODE, since I do not have your file to test it with.

10 件のコメント

Pengju
Pengju 2016 年 2 月 4 日
Thanks a lot.
But: ??? Attempted to access Zeros1(100002); index out of bounds because numel(Zeros1)=100001.
Error in ==> read_20160204_2 at 12 Record{k1} = DataD(Zeros1(k1):Zeros1(k1+1)-1,:);
Star Strider
Star Strider 2016 年 2 月 4 日
My pleasure.
The loop should be:
for k1 = 1:length(Zeros1)-1
Record{k1} = DataD(Zeros1(k1):Zeros1(k1+1)-1,:);
end
Error on my part. This new version should work.
Pengju
Pengju 2016 年 2 月 4 日
編集済み: Star Strider 2016 年 2 月 4 日
Thanks a lot,
I may give you the data file. I want put all the data in the red squares in one array...
Pengju
Pengju 2016 年 2 月 4 日
Pengju
Pengju 2016 年 2 月 4 日
Star Strider
Star Strider 2016 年 2 月 4 日
This code works correctly with your file:
fidi = fopen('test.txt','r');
Data = textscan(fidi, '%f%f%f', 'HeaderLines',4, 'CollectOutput',1);
fclose(fidi);
DataD = cell2mat(Data);
Zeros1 = unique([find(DataD(:,1) == 0); size(DataD,1)]);
for k1 = 1:length(Zeros1)-1
Record{k1} = DataD(Zeros1(k1):Zeros1(k1+1),:);
end
Output = [Record{1}(2:end-1,3); Record{2}(2:end-1,3); Record{3}(2:end-1,3)];
I had to work to edit your post so I could see your image, but I managed to do it.
The ‘Output’ array has the data in the red squares.
Pengju
Pengju 2016 年 2 月 4 日
Almost final. For the data in the red square, I have 100000 cells, the same number of k1.
So, actually, I want read all of those data into the same output array.
Star Strider
Star Strider 2016 年 2 月 4 日
I only have the data you provided, and I have demonstrated how to read your file and extract the data you want.
I leave it to you to do the rest.
Pengju
Pengju 2016 年 2 月 4 日
Thanks a lot
Star Strider
Star Strider 2016 年 2 月 4 日
My pleasure.
If my Answer solved your problem as you stated it in your Question, please Accept it.

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

その他の回答 (1 件)

dpb
dpb 2016 年 2 月 4 日

0 投票

As noted above, w/o a file to test, something like
fid=fopen('yourfile'); % open file
d=cell2mat(textscanf(fid,'','headerlines',4)); % read numeric data (Nx3 array)
fid=fclose(fid); % done w/ file
ix=[find(d(:,1)==0);length(d)+1]; % locations of segment breaks in data plus end
for i=1:length(ix)-1 % over the number of breaks
data{i}=d(ix(i):ix(i+1)),3); % save each group as a cell array
end
Do whatever with the contents of data for each cell as desired.

1 件のコメント

Pengju
Pengju 2016 年 2 月 4 日
??? Index exceeds matrix dimensions.
Error in ==> read_20160204_1 at 11 data{i}=d(ix(i):ix(i+1),3); % save each group as a cell array

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by