Reputation: 988
I am trying to read experimentally collected data into MATLAB via a text file. The data are all integers in eight columns separated by white space.
I want to open the file, read in the data, and then reshape into the representative array.
So, I have:
fileID = fopen('String name of file');
A = fread(fileid);
B = reshape(A, (length(A) / 8), 8);
By default, fread
assumes the data are doubles, but when I try to specify integers, such as fread(fileid, inf, 'int')
, the data still come out with the wrong values.
The data were output by a Java program as 'int' and were generated in a Linux environment, but are now in Windows (only MATLAB license I have). I assume this is an issue of specifying the correct data type for the fread
but I'm not sure.
Any ideas?
Upvotes: 0
Views: 97
Reputation: 4602
I don't know about using fread, but textscan should work:
% Open file
fid = fopen('file.txt');
% Scan 8 columns of numeric values
tmp = textscan( fid, repmat('%f ', [1 8]));
% Close file handle
fclose(fid);
% Convert to int and join columns into matrix
data = int32(cat(2, tmp{:}));
Upvotes: 1
Reputation: 30579
Use *int
instead of int
. Possibly try int32
or another platform-independent syntax. From the manual for fread
:
By default, numeric and character values are returned in class
'double' arrays. To return these values stored in classes other
than double, create your PRECISION argument by first specifying
your source format, then following it by '=>', and finally
specifying your destination format. If the source and destination
formats are the same then the following shorthand notation may be
used:
*source
which means:
source=>source
Also note that you can use the simplified reshape syntax:
B = reshape(A, [], 8);
Upvotes: 0