2013-06-20 224 views
0

我有一个exif文件,我想要读取和写入一个csv文件中的数据。读取和写入一个txt文件

这只是一个示例文件,真正的文件太复杂而且太长。

---- File ---- 
File Name      : IMG_5547.JPG 
Directory      : . 
File Size      : 3.1 MB 
File Modification Date/Time  : 2013:05:27 18:10:31+02:00 
File Access Date/Time   : 2013:06:19 13:53:37+02:00 
File Creation Date/Time   : 2013:06:19 13:53:37+02:00 
File Permissions    : rw-rw-rw- 
File Type      : JPEG 
MIME Type      : image/jpeg 
Exif Byte Order     : Little-endian (Intel, II) 
Image Width      : 4000 
Image Height     : 3000 
Encoding Process    : Baseline DCT, Huffman coding 
Bits Per Sample     : 8 
Color Components    : 3 
Y Cb Cr Sub Sampling   : YCbCr4:2:2 (2 1 
...... 
....... 
....... 

'muster.txt'

File Name      : IMG_5547.JPG 
GPS Time Stamp     : 08:52:21 
GPS Latitude     : 52.419358° 
GPS Longitude     : 9.652666° 
GPS Altitude     : 140.1 m 

%阅读文件

fid = fopen('muster.txt','r'); 

filename_ = fgetl(fid); 
Skip2_ = fgetl(fid); // **HOW CAN I SKIP MORE THAN ONE LINE ?????** 
GPS_Latitude =fgetl(fid); 
GPS_Longitude =fgetl(fid); 
GPS_Altitude =fgetl(fid); 
fclose(fid); 

%Wrting成csv文件

%的输出应该是这样的

%sample_out.csv

IMG_5547 52.419358 9.652666 140.1 

我不知道我怎样才能得到所需的( 'filename_,' GPS纬度”, 'GPS_Longitude', 'GPS_Altitude')值并写入csv文件

+0

我建议用['textscan'](http://www.mathworks.com/help/matlab/ref/textscan.html)读取输入内容,然后使用['regexp'](http:// www .mathworks.com/help/matlab/ref/regexp.html)来隔离所需的值。只要把所有东西都当作字符串处理,你就不需要将任何东西转换成实际的数字这里提供了很多答案,说明如何去做。 –

+0

如果文件是我们看到的所有文件,那么他甚至可以对左列进行硬编码,并在'textscan'格式中包含'\ n',以便在没有'regexp'的情况下一次性获得所需的所有内容。 – Oleg

+0

它只是一个示例文件。 – Shahgee

回答

1

这里是如何我会攻击这个问题:

% read in the file and convert to array of characters 
fid = fopen('muster.txt'); 
A = char(fread(fid))'; 
fclose(A); 
% create a cell array of strings for each line in the file 
fileLines = regexp(A,'\n','split'); 
% use the ' : ' as a kind of delimiter between the field name 
% and the data 
toks = regexp(fileLines,'\s+:\s+','split'); 
% create a structure to hold the data, and fill it 
data = struct(); 
for ii=1:length(toks) 
    currTok = toks{ii}; 
    data.(char(regexprep(currTok{1},'\s+',''))) = char(regexprep(currTok{2},'\s+','')); 
end 
% write the csv file 
fid = fopen('sample_out.csv','w'); 
fprintf(fid,'%s %s %s %s %s\n,...data.FileName, ... 
           data.GPSTimeStamp, ... 
           data.GPSLatitude, ... 
           data.GPSLongitude,data.GPSAltitude); 
fclose(fid); 

当然,有更有效的方法,但考虑到问题陈述,我会从这里开始。

HTH!