Reading Data from more than 2000 txt files in matlab

I have more than 3000 Data txt files. I create the name for these file according to special rate h=[1]; k+=[0.1 0.2 0.4 0.7 1 1.1 1.2 1.5 1.7 2 2.5 3 3.5 4 5]; K-T=[1e-6 0.1 0.2 0.4 0.7 1 1.1 1.2 1.5 1.7 2 2.5 3 3.5 4 5]; K-D=[1e-6 0.1 0.2 0.4 0.7 1 1.1 1.2 1.5 1.7 2 2.5 3 3.5 4 5];
The Name of each files like this one (MTN100_k+0.1_k-T_0.1_k-D0.4_h1_GTP0.txt) How can I read these files in Matlab. I need to work with each file separately. I tried to use dlmread and fscan but it is not helpful. Any help would be greatly appreciated. I will be grateful to you.

回答 (1 件)

Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh 2014 年 8 月 19 日

0 投票

Hi Ahmed,
It's really good that the files name has an order, you can use that to write a program to automatically open a specific file.
What you need to do is to open the text file and read it. dlmread and fscan are used after you opened the file, at the end you need to close the file.
I'll add you some links to help you do this.
Good Luck.

5 件のコメント

Ahmed
Ahmed 2014 年 8 月 19 日
Hi Salaheddin, Unfortunately, I used C++ to get the Data (txt.files) and than read these data in Matlab
Michael Haderlein
Michael Haderlein 2014 年 8 月 19 日
Hello Ahmed,
so where do you struggle? Reading the file or interpretation of the file name?
As Salaheddin has written, text files can usually be opened with dlmread or, if you need more control, fscan. If the content is very complex, you might need to use fgetl and so on, but we need the structure of the file to help you in this case.
In case you need to work with the file name, you can get all the file names with the dir function and apply textscan on the file names you found.
In any case it doesn't matter if the files have been written in Matlab or C++ or any other language (as long as the encoding is available in Matlab).
Ahmed
Ahmed 2014 年 8 月 19 日
Hello Michael, Yes I am struggle in an interpretation of the file name. How can read these files according to the name because I need to sort the output as a table contain the file name.
Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh 2014 年 8 月 19 日
編集済み: Salaheddin Hosseinzadeh 2014 年 8 月 19 日
Ahmed,
You need some programming, just write an algorithm to generate the correct file name automatically.
for example if your file names are
MTN100_k+0.1_k-T_0.1_k-D0.4_h1_GTP0.txt)
MTN100_k+0.1_k-T_0.1_k-D0.4_h1_GTP1.txt)
MTN100_k+0.1_k-T_0.1_k-D0.4_h1_GTP2.txt)
You can generate file names with a single for loop
for i = 0:1:2
fileName = ['MTN100_k+0.1_k-T_0.1_k-D0.4_h1_GTP',num2str(i),'.txt'];
fopen(fileName);
% do the rest of your processing.
fclose(fileName);
end % terminating the for loop
in C++ you put the string together by adding them
str = str1 + str2...;
In MATLAB you can simply concatenate them as follow
str = [str1,str2,str3]
I assumed you're mainly using C++ and not really familiar with how to do the similar things in MATLAB.
Hope this is helpful enough.
Ahmed
Ahmed 2014 年 8 月 19 日
編集済み: Ahmed 2014 年 8 月 19 日
Thanks Salaheddin, I think you got it, I need some programming, just write an algorithm to generate the correct file name automatically.
for example if your file names are
MTN100_k+0.1_k-T_0.1_k-D0.4_h1_GTP0.txt)
MTN100_k+0.1_k-T_0.1_k-D0.7_h1_GTP0.txt)
MTN100_k+0.1_k-T_0.1_k-D1_h1_GTP0.txt)
according to these values h=[1]; k+=[0.1 0.2 0.4 0.7 1 1.1 1.2 1.5 1.7 2 2.5 3 3.5 4 5]; K-T=[1e-6 0.1 0.2 0.4 0.7 1 1.1 1.2 1.5 1.7 2 2.5 3 3.5 4 5]; K-D=[1e-6 0.1 0.2 0.4 0.7 1 1.1 1.2 1.5 1.7 2 2.5 3 3.5 4 5];
h1 and GTP0 are fixed I just need to change k+,k-T,k-D

この質問は閉じられています。

タグ

タグが未入力です。

質問済み:

2014 年 8 月 19 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by