Showing posts with label ascii. Show all posts
Showing posts with label ascii. Show all posts

Thursday, 4 December 2014

How to read a formatted text (ASCII) file in Matlab?

1. If a text (ASCII) file contains data with different data types then 'load' command is not useful. Then, one option can be to use TEXTSCAN with the format specifier as shown below.


fid = fopen(fileName)
formatSpec = '%f %s %f %f'

data = textscan(fid,formatSpec,3600*1,'Delimiter',',','CollectOutput', true);

fclose(fid)

The output will be a cell array. Each cell contains each of the elements. If you...
CollectOutput - will collect the consecutive data of same type in a single cell array.
Delimiter - lets you specify the delimiter used in the text file. If do not use the 'Delimiter' option then space is treated as the delimiter.


2. Many examples are given in the Matlab link given above:


Saturday, 7 December 2013

How to read from and write to a binary and text (ASCII) file in Matlab?

1. Here is the example for binary:

% Clear screen, clear workspace, and close all figures
clc;
clear all;
close all;

% Path and file name
% Read file
fReadName = 'ReadDataFile.bin'; % file from the current working directory

% Write file
fWriteName = 'WriteDataFile.bin';  % file to be written into the current working directory

% Open the file in read mode
 fidRead = fopen(fReadName,'rb');

 % Open the file in write mode
fidWrite = fopen(fWriteName,'wb');

% Read the data of correct data-type
dataType = 'int16';
% For more details on the Matlab supported data types: click here

numberOfSeconds = 300;
fSamp = 12.5e6;

tic
for ii = 1:1:numberOfSeconds

    numberOfwordsToReadPerTime = fSamp * 1 * 2;

   % Read from File
   dataFromFile = fread(fidRead,numberOfwordsToReadPerTime,dataType);

   % Write into the file
   fwrite(fidWrite,dataFromFile,dataType);

end

toc; % tic and toc gives the time taken by Matlab to complete the execution of the above for-loop.

% Close the files
fclose(fidRead);
fclose(fidWrite);

% 2a. Example for ASCII/text file containing numbers

a = magic(10); % generates 10x10 matrix of numbers
save bFile.txt a -ascii;

fid = fopen('bFile.txt','r');
d = fscanf(fid,'%f',20)  % just reads first 20 words into a column
fclose(fid);

%Skipping few number of words from the file requires exact number of bytes + %bytes for end-of-line.
% we can check the number of bytes read so far using the command 'ftell'


% 2b. Example for ASCII/text file containing numbers
%To read the 10x10 matrix
rowStart = 0;
rowColumn = 0;
rowEnd = 9;
colEnd = 9;

d2 = dlmread('bFile.txt','',[rowStart rowColumn rowEnd colEnd])



%Result


d =

    92
    99
     1
     8
    15
    67
    74
    51
    58
    40
    98
    80
     7
    14
    16
    73
    55
    57
    64
    41


d2 =

    92    99     1     8    15    67    74    51    58    40
    98    80     7    14    16    73    55    57    64    41
     4    81    88    20    22    54    56    63    70    47
    85    87    19    21     3    60    62    69    71    28
    86    93    25     2     9    61    68    75    52    34
    17    24    76    83    90    42    49    26    33    65
    23     5    82    89    91    48    30    32    39    66
    79     6    13    95    97    29    31    38    45    72
    10    12    94    96    78    35    37    44    46    53
    11    18   100    77    84    36    43    50    27    59