How to Read a .dat File in Matlab
Data Analysis - reading text files and processing them with Matlab
In this commodity, we're going to read text files with Matlab, performdata assay or processing, and finally nosotros are going to write out our results to some other text file. The process is hands adaptable to many situations.Allow's assume that we take iii text files (it could be hundreds). They all take to take the aforementioned format, and have to have a basic file name, with numbered tag endings (otherwise it is harder to automate the reading process).
For example, we have files 'data_sheet1.txt', 'data_sheet2.txt' and 'data_sheet3.txt'.
And then, the basic file proper name is 'data_sheet'; then, the numbered tag is 1, ii or 3, respectively, and they all finish with the '.txt' extension.
Allow the content for each file be something simple, for instance, for 'data_sheet1.txt' the hypothetical content is:
dummy line one
dummy line ii
dummy line 3
1 1234
ii 2345
3 4320
4 4567
5 9876
This file has four text lines (three dummy lines and 1 blank line) at the starting time, and so the real data in ii columns.
In our example, the content for 'data_sheet2.txt' is:
dummy line 1
dummy line 2
dummy line 3
1 12340
2 23452
3 43203
4 45674
five 98765
and the content for 'data_sheet3.txt' is
dummy line 1
dummy line 2
dummy line 3
1 123
2 234
3 432
4 456
5 987
Annotation that all the three files take four text lines at the beginning and all of them have the relevant data in the same format, with the same number of elements (two columns and five rows). The number of columns or rows is not relevant for our purpose, but the files have to continue the same format or structure.
We are going to use Matlab functions 'fopen', 'textscan' and 'num2str' to read data from all those '.txt' files (information technology'due south a good idea if you investigate those iii functions a little fleck, simply I'll give you the recipe).
Nosotros are not interested in the 4 text lines at the beginning of the files, and we desire to read the first cavalcade of the offset file (which is the same for all the files, let's say for identification purposes) and the second column of each of the files, then, nosotros want to finish with something like
1 1234 12340 123
2 2345 23452 234
iii 4320 43203 432
4 4567 45674 456
5 9876 98765 987
In this mode, nosotros at present take the information in one matrix, and nosotros tin do data analysis thereafter.
This is the part that I propose to read the files. Y'all take two input parameters (the base of operations file name and the number of files to read) and i output (one jail cell assortment with your relevant data). Fair, isn't information technology?
To automatically change the name of the file we utilise an array in this form:
[BaseFile num2str(i) '.txt']
This array concatenates the string BaseFile proper noun (input parameter) with a counting number (past changing the iteration counter into a cord), and then concatenates the '.txt' extension.
For the beginning file, the idea could be represented by:
[BaseFile '1' '.txt'], or better [BaseFile '1.txt']
The full code would exist:
part R = get_data(BaseFile, due north)
% Open the first file
d(1) = fopen([BaseFile '1.txt' ]);
% Read the outset 2 columns, skip the kickoff iv headerlines
R = textscan(d(ane), '%f %f' , 'headerLines' , 4);
% Close the file, you don't need it any longer
fclose(d(1));
for i = 2 : north
% Open consecutively each of the remaining files
d(i) = fopen([BaseFile num2str(i) '.txt' ]);
% Skip the first cavalcade of the new file (an '*' to do this) % and keep on edifice the array
R = [R textscan(d(i), '%*f %f' , 'headerLines' , 4)];
% Shut the file
fclose(d(i));
stop
How are you going to use the in a higher place function to read text files and process data from Matlab?
This is one suggestion. You may process it the fashion you want...
% Reset your retention and clear your screen
clear; clc
% Provide base file proper noun and number of files to be read
BaseFile = 'data_sheet' ;
n = 3;
% Use the developed function to read data
R = get_data(BaseFile, n);
% Transform your prison cell array into an ordinary matrix
% and show your data
my_data = cell2mat(R)
At this point 'my_data' is a matrix that has the information as you need it (exactly as shown earlier).
You can study information technology, or plot it... or perform data analysis of any kind...
% Calculate the average of all of the columns and show
my_average = hateful(my_data)
% Calculate the standard departure for each cavalcade
my_std = std(my_data)
% Calculate the maximum
my_max = max(my_data)
% Calculate the minimum
my_min = min(my_data)
% Arrange your information to be saved
my_results = [my_average' my_std' my_max' my_min']
% Save your 'my_results' matrix in file 'data_out.txt'
relieve data_out.txt -ascii my_results
Done! Now, you have a text file with your information analysis or processed information.
From 'Information Analysis' to home
From 'Information Assay' to 'Matlab Cookbook Card'

Source: https://www.matrixlab-examples.com/data-analysis.html
0 Response to "How to Read a .dat File in Matlab"
Enregistrer un commentaire