To eliminate the lines begin with "%"

조회 수: 1 (최근 30일)
aneps
aneps 2013년 11월 25일
답변: Image Analyst 2013년 11월 26일
I have a file (attached here) which I need to eliminate all the lines begin with "%" and remove the "commas" after the numbers. Then read the numbers and put in separate columns X, Y and Z. Can anyone help me to make the program?
  댓글 수: 2
Walter Roberson
Walter Roberson 2013년 11월 25일
The file did not get attached.
aneps
aneps 2013년 11월 25일
Thank you... Now the file is attached

댓글을 달려면 로그인하십시오.

채택된 답변

Simon
Simon 2013년 11월 25일
Hi!
Read in your file:
% read in a file
fid = fopen(FileName);
FC = textscan(fid, '%s', 'delimiter', '\n');
fclose(fid);
FC = FC{1};
You can get rid of all "%" lines if you compare the first char like:
FC = FC(~strncmp('%', FC, 1));
You can get rid of all ',' with a regex like:
FC = regexprep(FC, ',', '');
Afterwards you can parse your cell array FC line-by-line with sscanf to extract numbers.
  댓글 수: 3
aneps
aneps 2013년 11월 26일
This seems not working! As the file is bigger, I have uploaded in "transfer big files.com"(https://www.transferbigfiles.com/dd8e3ef1-95c6-4fd8-a00e-908d2a0706d8/a8MGzUPeLZXl-HV0oJ-nMg2). The file name is VMIVolt.asc.
Here, when I run this program, the final data "A" is seems somehow messed up! I couldn't understand what has happened! If I manually delete all the lines in the data begin with "%" and compare with the one I got by running the program above doesn't seems to match. For example, all the elements in the third column in the original data are zeros but after running the above program, the zeros are replaced with some numbers! Thanks a lot.
Simon
Simon 2013년 11월 26일
Hi!
It seems that sscanf fails for some reasons I don't know. But you can as well use
% use sscanf for each cell
A = cellfun(@(x) sscanf(x, '%f %f %f'), FC, 'UniformOutput', false);
% convert from cell array to numeric array
A = [A{:}].';

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2013년 11월 26일
Why not just use importdata() where you can tell it to throw away some number of headerlines?

카테고리

Help CenterFile Exchange에서 Large 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