How to Read Text Files 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 we are going to write out our results to another text file. The procedure is easily adaptable to many situations.Let'south presume that we have 3 text files (it could be hundreds). They all have to have the same format, and have to have a basic file proper name, with numbered tag endings (otherwise it is harder to automate the reading process).
For example, we take files 'data_sheet1.txt', 'data_sheet2.txt' and 'data_sheet3.txt'.
And then, the basic file name is 'data_sheet'; then, the numbered tag is 1, two or three, respectively, and they all end with the '.txt' extension.
Let the content for each file be something unproblematic, for example, for 'data_sheet1.txt' the hypothetical content is:
dummy line 1
dummy line 2
dummy line 3
1 1234
2 2345
three 4320
four 4567
5 9876
This file has iv text lines (three dummy lines and 1 blank line) at the beginning, and then the real data in two columns.
In our case, the content for 'data_sheet2.txt' is:
dummy line one
dummy line 2
dummy line 3
one 12340
2 23452
3 43203
4 45674
5 98765
and the content for 'data_sheet3.txt' is
dummy line 1
dummy line 2
dummy line 3
1 123
2 234
3 432
four 456
5 987
Annotation that all the three files take four text lines at the showtime and all of them accept 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, just the files have to keep the same format or structure.
We are going to apply Matlab functions 'fopen', 'textscan' and 'num2str' to read data from all those '.txt' files (it's a proficient idea if you investigate those three functions a little scrap, merely I'll requite yous the recipe).
Nosotros are not interested in the four text lines at the get-go of the files, and we want to read the offset cavalcade of the outset file (which is the same for all the files, permit's say for identification purposes) and the 2nd column of each of the files, so, we want to end with something similar
1 1234 12340 123
2 2345 23452 234
3 4320 43203 432
iv 4567 45674 456
5 9876 98765 987
In this way, we now accept the data in ane matrix, and nosotros tin practise information analysis thereafter.
This is the function that I propose to read the files. Y'all accept two input parameters (the base file proper noun and the number of files to read) and one output (one cell array with your relevant data). Off-white, isn't it?
To automatically change the name of the file we use an assortment in this class:
[BaseFile num2str(i) '.txt']
This array concatenates the string BaseFile proper noun (input parameter) with a counting number (by changing the iteration counter into a string), and then concatenates the '.txt' extension.
For the starting time file, the idea could be represented by:
[BaseFile '1' '.txt'], or improve [BaseFile '1.txt']
The full code would exist:
function R = get_data(BaseFile, n)
% Open up the beginning file
d(1) = fopen([BaseFile 'i.txt' ]);
% Read the first two columns, skip the first 4 headerlines
R = textscan(d(1), '%f %f' , 'headerLines' , 4);
% Shut the file, you don't demand information technology any longer
fclose(d(1));
for i = two : n
% Open consecutively each of the remaining files
d(i) = fopen([BaseFile num2str(i) '.txt' ]);
% Skip the outset column of the new file (an '*' to practice this) % and go on on building the array
R = [R textscan(d(i), '%*f %f' , 'headerLines' , 4)];
% Shut the file
fclose(d(i));
cease
How are you going to use the above function to read text files and process data from Matlab?
This is one suggestion. You may process it the way y'all want...
% Reset your memory and clear your screen
clear; clc
% Provide base file proper name and number of files to exist read
BaseFile = 'data_sheet' ;
n = three;
% Use the developed part to read information
R = get_data(BaseFile, n);
% Transform your 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 data as you need information technology (exactly as shown before).
You tin can study information technology, or plot it... or perform data assay of any kind...
% Calculate the average of all of the columns and show
my_average = mean(my_data)
% Summate the standard deviation for each column
my_std = std(my_data)
% Summate the maximum
my_max = max(my_data)
% Calculate the minimum
my_min = min(my_data)
% Accommodate your data to be saved
my_results = [my_average' my_std' my_max' my_min']
% Save your 'my_results' matrix in file 'data_out.txt'
save data_out.txt -ascii my_results
Done! Now, you have a text file with your data assay or processed information.
From 'Information Assay' to domicile
From 'Data Assay' to 'Matlab Cookbook Menu'

Source: https://www.matrixlab-examples.com/data-analysis.html
0 Response to "How to Read Text Files in Matlab"
Post a Comment