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