Reading a line of binary file MATLAB - matlab

I am new in using MATLAB and I want to do a simple thing: I want to read a binary file that contains rows like this
32156432
345243867
454154351
35477
5641871
....
I know that the fread() in MATLAB reads the file byte by byte, but I want to read the value that there is on each line. All values are uint32_t and the file is generated with a script in C++ with just a printf, the values are printed in a file like my_file.bin launching the executable in this way ./executable param1 >> my_file.bin

You can use the function fscanf
Sample Code:
fileID = fopen('my_file.bin','w');
x = 32156432;
y = 345243867;
w = 454154351;
fprintf(fileID, '%d\n',x);
fprintf(fileID, '%d\n',y);
fprintf(fileID, '%d\n',w);
fclose(fileID);
fileID = fopen('my_file.bin','r');
formatSpec = '%d';
A = fscanf(fileID, formatSpec);

Related

Issue with format specification while reading from file Matlab

I have a .dat file with a table containing data in following order:
0,000E+0 4,069E-2 -5,954E+0 1,851E-2
What I need to do is to read this data with matlab and then somehow handle it.
Here is my code:
path = 'C:/Users/user/Desktop/file1.dat';
fileID = fopen(path,'r');
formatSpec = '%e';
A = fscanf(fileID,formatSpec);
fclose(fileID);
disp(A);
Unfortunately, it doesn't work. What did I do wrong?
After replacement of comma with dot in data you can read it using dlmread function:
M = dlmread('filename', ' ');
M is what you want.
For the first part, replacing a character, you can use the following code:
% read the file
fid = fopen('input.txt','r');
f=fread(fid,'*char')';
fclose(fid);
%replace the char
f = strrep(f,',','.');
% write into the another file
fid = fopen('output.txt','w');
fprintf(fid,'%s',f);
fclose(fid);

How to read a single character in file using MATLAB?

In my file data.txt, I have a string abcdefgh. Now I want to take just 1 character without read whole string. How can I do this in MATLAB?
For example, I want to take the first character, I use c = fscanf(data.txt, '%c'); and c = textscan(data.txt, '%c'); but it read whole line in data.txt. I know that c(1) is my answer but I don't want to do that.
You can limit the number of characters that are read in using the third input to either fscanf or textscan.
fid = fopen('data.txt', 'r');
c = fscanf(fid, '%c', 1);
c = textscan(fid, '%c', 1);
You could also just use a lower-level function such as fread to do this.
fid = fopen('data.txt', 'r');
c = fread(fid, 1, '*char');

Textscan until end of line

I'm trying to textscan a file and read a single line until the end of it, undependently of the number of elements in that line.
My file is a .txt file formatted like this :
602,598,302,456,1023,523,....
293,291,566,331,987,56,....
589,202,429,2911,294,567,...
And so on. I have the number of the line, and all lines have the same number of elements, but it can vary from one file to another.
I wrote something like:
fid = fopen('somefile.txt');
C = textscan(fid, formatSpec,'HeaderLines',Row-1);
TheLine = C{1};
fclose(fid);
X = numel(TheLine);
plot(1:X,TheLine);
I really don't know what to type in the formatSpec field. I've tried a few things in the way of %[^\n] but I didn't get much sucess.
Try this -
C = textscan(fid, '%d,','HeaderLines',Row-1);
Row will specify the row of data that you want to extract from the text file.

convert ascii .dat files to .xml using Matlab (or any other software)

I have a set of ascii files with the extension .dat and I need to convert them into a set of .xml files.
Is there anyway to do this with Matlab or any other software.
This is one of the files that I need to convert:
https://docs.google.com/open?id=0B1GI9KuZUKX3TDFCZDVTbzdINUE
I've used XML4MAT in the past. It will handle the data conversion into and out of an XML format, but doesn't quite handle actually reading and writing the XML file, so you have to add a little glue code. The sequence is:
Read the dat file into one variable in MATLAB (here I use the variable name Data). It looks like your file is essentially a table of numbers so that is easy.
Use DumpToXML.m and LoadFromXML.m as the glue code to the XML4MAT package that you'll download separately.
% function DumpToXML(XMLFileName, Data)
function DumpToXML(XMLFileName, Data)
% Generate the text of the XML file.
XMLData = ['<root>' 10];
XMLData = [XMLData mat2xml(Data, 'Data', 1)];
XMLData = [XMLData '</root>' 0];
% Now output the data.
fid = fopen(XMLFileName, 'w');
fprintf(fid, '%s', XMLData);
fclose(fid);
end
% function LoadFromXML(XMLFileName)
function Data = LoadFromXML(XMLFileName)
% Open the XML file.
fid = fopen(XMLFileName, 'r');
if(fid <= 0)
error(['Cannot open XML file ' XMLFileName]);
end
XMLData = fscanf(fid, '%c', inf);
fclose(fid);
% Now get the Data tag.
DataStartIndex = findstr(XMLData, '<Data');
% Now find the end.
DataEndIndex = findstr(XMLData, '</Data>');
% Extract the strings for this two variable from the string
% containing the loaded XML file.
XMLData = XMLData(DataStartIndex:DataEndIndex+6);
% And turn it back into a variable.
Data = xml2mat(XMLData);
end
I don't think Matlab is the weapon of choice for that.
I'd advocate for Python since there are nice XML packages such as lxml. You should be able to parse the dat file easily with open() and readlines().

How to indicate to the end of file in matlab

I have a flat file saved in binary format, i want to seek to specific byte and read untile the end of that file, so we need a condition for that, what is the condition that indicates the end of the file?
By the way, i don't want to load the whole file just open the file and seek to the position i need then read until to the end of the file...
MATLAB's fread function (and, indeed, the other file IO functions) will automatically detect the end of a binary file; there's no need for a special end-of-file marker.
fread documentation
General MATLAB IO documentation
You can use feof to test for the end of a file. For example, to read a file one character at a time:
fid = fopen('bench.dat');
k = 0;
while ~feof(fid)
curr = fscanf(fid,'%c',1);
if ~isempty(curr)
k = k+1;
benchstr(k) = curr;
end
end
fclose(fid);
You can pass Inf for the size argument when using the FREAD function (will read until end of file). Here is an example:
%# first lets create a simple binary file
fid = fopen('file.bin', 'wb');
fwrite(fid, 'hello world', 'char*1');
fclose(fid);
%# now open binary file, seek to some position, and read bytes till EOF
fid = fopen('file.bin', 'rb');
fseek(fid, 6, 'bof'); %# go to the 7th byte
B = fread(fid, Inf, 'uint8=>char'); %# read bytes until end-of-file (as chars)
fclose(fid);
disp(B)