mex to open and read file

2 views (last 30 days)
Jw
Jw on 5 Dec 2011
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 Comments
Jw
Jw on 5 Dec 2011
linking with other file for other operations
Walter Roberson
Walter Roberson on 5 Dec 2011
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.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 5 Dec 2011
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 Comments
Walter Roberson
Walter Roberson on 5 Dec 2011
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 on 5 Dec 2011
thank you!!

Sign in to comment.

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!