Do you still remember question B of this year's mathematical modeling? In the face of a large amount of image data in a folder, many people write hundreds of lines of repeated code to read various images, in fact, we can use the dir function to get the file names in this folder, and then filter them to obtain the names we need in batch and then read the data.
Today, you can search Baidu for the keyword "MATLAB" first.Dir "or docDir to see the basic usage of this function.
The general rule is strnam.=Dir ('path \*.#')
Path is the folder path you want to obtain, * is a wildcard, # Is the file type you want to obtain, such as jpg
Example: In this example, the data of all jpg image files in the deal folder is read and stored in the cell.
clc,clear,close allfinf = dir('deal\*.jpg');n = length(finf);data = cell(n,1);for k=1:n filename = ['deal\',finf(k).name]; data{k} = importfile(filename);end
The second line of the function is to return all the attributes of jpg files with the suffix name in the deal folder.
Whos finf
Name Size Bytes Class Attributes
Finf 86x1 32378 struct
Finf =
86x1 struct array with fields:
Name
Date
Bytes
Isdir
Datenum
Finf is a struct array that contains attributes such as the name, modification time, size, and whether the 86 jpg files under deal.
The third action gets the size of the array.
Create n * 1 cells in row 4.
The location where the k files are constructed in row 6th (merged file path and file name)
Line 3 calls the importfile function to read the data of the file. (The importfile function is automatically generated by the import tool. The generation method is not described in this section)
The End:
Pay attention to Weibo, update programming knowledge usage on a daily basis, and improve programming skills!