load text file with specific format

Hello,
I have a problem loading a text file that has format like this:
20
65
33
500
12 34 3.99
14 20 6.09
12 31 2.90
I have tried different functions e.g(import data,open, load,textscan ... ), none of them works. I need to use the above format as it is.
any suggestions please.

답변 (1개)

Walter Roberson
Walter Roberson 2014년 3월 12일

0 개 추천

fid = fopen('YourFile.txt', 'r');
first4cell = textscan(fid, '%g', 4);
restcell = textscan(fid, '%g%g%', 'CollectOutput', 1);
fclose(fid);
first4 = first4cell{1};
rest = restcell{1};
now first4 will be a column vector containing the 4 leading values, and rest will be an N x 3 matrix containing the values from the remainder of the file.

댓글 수: 3

Mr. Unkown
Mr. Unkown 2014년 3월 12일
Thanks Walter,
in this way my file will be in two parts. I need to read or load the file in the same format,then I'll send it to an executable file that i'm able to use through MATLAB.
Walter Roberson
Walter Roberson 2014년 3월 13일
What datatype would you want it stored before you send it? What format does the executable need the data to be in?
Mr. Unkown
Mr. Unkown 2014년 3월 13일
Walter,
I believe I have asked a wrong question:) In fact, i just need to pass the text file to the executable. I figure how to do it.
your answer helped me in another part, and i've a question: when I use your approach I got this error:(Error using textscanBadly formed format string.) I changed %g%g%g to %d%d%d and works fine but it doesn't display the floating number and if i used %d%d%f it displays only two columns. Thank you for help

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

카테고리

도움말 센터File Exchange에서 Standard File Formats에 대해 자세히 알아보기

질문:

2014년 3월 12일

댓글:

2014년 3월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by