Reputation: 11
I am trying to obtain a 5X2 matrixfrom a text file.
For Example :
0_0 1_0
0_200 1_200
0_400 1_400
0_600 1_600
0_800 1_800
This is the code I am currently using:
[filename,pathname]= uigetfile({'*.txt'});
set(handles.temp1,'String',fullfile(pathname,filename));
chosenfile=get(handles.temp1,'String');
fid=fopen(chosenfile);
allcoordinates=textscan(fid,'%s,%s','whitespace','\n');
fclose(fid);
This code would produce a 5X1 matrix as shown below :
0_01_0
0_2001_200
0_4001_400
0_6001_600
0_8001_800
Upvotes: 0
Views: 172
Reputation: 38032
Sadly, the approach that works best with interpretation of files, is to be very conservative with relying on the capabilities of 'canned routines' like textscan
, dlmread
and similar.
This is not because these routines are implemented badly, it's because there is very little standardization in number formatting in text files, and basically, everybody just invents a new standard on the spot.
You just can't design a routine that always works correctly for all text files. I think The Mathworks did a very decent job with their dlmread
and similar, however, you have just presented yet another standard in number formatting that is overly difficult to interpret in one go with textscan
or dlmread
or others. Therefore, be conservative: just read it without too much hassle, and do the conversion yourself.
For example:
%// Read data
fid = fopen('yourFile.txt', 'r');
C = textscan(fid, '%s %s');
fclose(fid);
%// Replace all underscores with '.', and convert to 'double'
C = str2double(strrep([C{:}],'_','.'))
Results:
C =
0 1.0000
0.2000 1.2000
0.4000 1.4000
0.6000 1.6000
0.8000 1.8000
Upvotes: 3