Reputation: 15
I have a large amount of data that is stored in multiple .net files and I wanted to process them using MATLAB (my current version is R2013a). Despite searching the MathWorks website, I was unable to find any command for importing data from .net files since the ncread
command works only for .nc files. An image of the structure of the file(s), after opening with Notepad++, can be seen here.
As can be seen, the file has the data separated by NUL US NUL
and starts with US NUL
, the Unit Separator and Null characters. I was looking for a method to read in the file such that each of the three entries between the NUL US NUL
characters corresponds to the three columns of one row.
One option would be for me to manually open each file with Notepad++ and replace \x00\x1f\x00
with \n
, then open the file with Microsoft Excel, delimited by spaces, and save it as a .csv file, which I can in turn import into MATLAB with the csvread
command. However, since I have nearly 600 .net files, this does not seem to be a practical solution, simply because of scale of the effort required. I would be really grateful if someone could help me out in this regard and suggest an efficient method to read these files into MATLAB. Thank you so much.
Upvotes: 0
Views: 504
Reputation: 306
Here is the solution:
function mat=filetrim(filename,'NUL US NUL') %
tic
fid=fopen(filename);
if (isunix) % Linux or Mac
[~, result] = system( ['wc -l ', filename] );
nl=strcat(regexp(result,'[\d]','match'));
numlines = str2double(nl);
elseif (ispc) % Windows
numlines = str2double( perl('countlines.pl', 'your_file') );
else
error('...');
end
tline=fgetl(fid);
r=regexp(strtrim(tline(7:end-6)),pat,'split');% here I assumed that the line ends with
% US NUL. If the line does not end with
% US NUL, please change the "end-6" to
% "end".
d=cell2mat(cellfun(@str2num,r,'UniformOutput',false));
mat=zeros(numlines,size(d,2));
for i=1:size(mat,1)
r=regexp(strtrim(tline(7:end-6)),pat,'split');
mat(i,:)=cell2mat(cellfun(@str2num,r,'UniformOutput',false));
end
fclose all;
toc
return
The loop to find out the number of lines in the file was adapted from one of the solutions provided in the "Stackoverflow". The credit goes to @Rody Oldenhuis for this part.
Upvotes: 0
Reputation: 493
Functions to consider:
Upvotes: 0