[MATLAB function sharing] dir function to read documents in specific formats in a specified folder in batches

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.