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
% 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
No comments:
Post a Comment