The “imageDatastore” function assign the labels for all the images in subfolder, given a "LabelSource".
Refer the following MATLAB documentation link to know more about “imageDatastore” function.
In addition, refer the following link to the MATLAB documentation link for a demo example on image category classification using deep learning.