필터 지우기
필터 지우기

mex to open and read file

조회 수: 2 (최근 30일)
Jw
Jw 2011년 12월 5일
How do i use mex file to open a text file and read in the data then saving to a .dat file??
my data is in this form 1318487443.785868,$GPGGA,063032.00,0120.46992477,N,10340.89226612,E,1,07,1.7,27.2,M,4.2,M,,*6B
I only wanna read in the N and E data
Thank you.
  댓글 수: 3
Jw
Jw 2011년 12월 5일
linking with other file for other operations
Walter Roberson
Walter Roberson 2011년 12월 5일
Yes, and? MATLAB can write .dat files nearly as well as C can (the difference is sufficiently obscure that in more than 4 years, I have never seen anyone other than me request that the one minor missing part be implemented.)
Are you trying to write a .dat file or a .mat file?
Is the routine to be called from within MATLAB or from within some other programming language? If it is to be called from within some other programming language, then mex is not appropriate for the implementation, as mex is specifically for creating things that are callable within MATLAB.

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

답변 (1개)

Walter Roberson
Walter Roberson 2011년 12월 5일
The data sub-selection can be done within a very small number of lines of perl; perl is supplied as part of every MATLAB installation.
If you were to invoke the perl executable directly instead of using the MATLAB perl() function, it could potentially be as simple as
INFILE = 'InputFileName.txt'; %include directory if you want
OUTFILE = 'OutputFileName.dat'; %include directory if you want
cmd = sprintf('perl -anF, -e ''{print $4, $6, "\\n"}'' < "%s" > "%s", INFILE, OUTFILE);
system(cmd);
You did not, however, define what the "N" and "E" data actually were (I guessed the field before), and you did not define the output file format (there is no standard at all for .dat files), so I just output the strings from those fields. If you want the fields converted to binary, it would be a little more work but not much.
  댓글 수: 3
Walter Roberson
Walter Roberson 2011년 12월 5일
data = textscan('%*s%*s%*s%f%*s%f%*[^\n]', 'Delimiter', ',');
That is, throw away 3 strings (sequences of characters, who cares if they happen to look like numbers or not), read a number, throw away a string ('N'), read a number, throw away everything else to the end of line.
You might also prefer to add 'CollectOutput', 1 to the options.
I notice that you used semi-colon as the delimiter, but the text you showed used commas. I wrote in terms of commas.
Jw
Jw 2011년 12월 5일
thank you!!

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

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by