MATLAB reads files in a folder in batches
MATLAB reads TXT files in a folder in batches Browsing times: 1101
Reward score: 10 | Solution time:
| Questioner: Cloned
| Report
Now there is a folder containing about 50 TXT files. Each file contains about 30 thousand rows and two columns. The first column is a string, and the second column is a floating point number. I only need to read the second column. Now I want to write one. m Files Read the TXT files in this folder in batches. After reading the TXT files, the array variable name is the TXT file name. Now the problem is: 1. How does MATLAB traverse all functions in a folder? 2. If a variable is created by file name?
Best Answer
For example, there is a series of TXT files under E: \ new, with the file name following the naming rules of the MATLAB variable name and no temp.txt file. Each file has two columns separated by spaces. The first column is a string, and the second column is a floating point number, the floating-point number in the second column to be read is an array and the variable name is the file name. File = Dir ('e: \ new \*. TXT '); for n = 1: length (File) temp = dlmread (['e: \ new \', file (n ). name], '', 0, 1); EVAL () End
1. MATLAB uses the Dir function to obtain all subfolders and files in the specified folder and store them in an array of object structures. the dir function can call Dir ('. ') list all subfolders and files in the current directory Dir ('G: \ MATLAB') list all subfolders and files in the specified directory Dir ('*. m ') list the folders and files in the current directory that match the regular expression. The structure array is obtained. Each element is in the following format: struct name -- filename date -- modification date bytes -- number of bytes allocated to the file isdir. -- 1 If name is a directory and 0 if not datenum -- modification date as a MATLAB serial date number are file names, modification date, size, whether it is a directory, Matlab The specified modification date can be used to extract the file name for reading and saving. 2. I can't think of any good method. Generally, a unified temporary variable is used to read file data, such as TMP, and then the variable name saved with a string variable, for example, to implement a statement such as a1 = TMP in STR = 'a1', use the following statement str_a1 = [STR, '= TMP']; EVAL (str_a1 ); in this way, the A1 variable appears in workspace, but the problem is that you do not know the variable name is A1? Or I still cannot find a method to address variables using string values. Instead, I can only import the values to a variable with a name, such as TMP. if you are interested, you can discuss it.