what does this error mean?"Array formation and parentheses-style indexing with objects of class 'matlab.io​.datastore​.ImageData​store' is not allowed"

2 views (last 30 days)
Array formation and parentheses-style indexing with objects of class 'matlab.io.datastore.ImageDatastore' is not allowed. Use objects of class 'matlab.io.datastore.ImageDatastore' only as scalars or use a cell array. Error in Untitled (line 12)
Hi, I just started using Matlab. I got the following error and I don't know what to do. I will be glad if you can help.
I have a folder Data. it has 4 subfolders(labels) containing 40 images. i am trying to do image classification using deep learning and machine learning but i am getting the above error.
imdsData = imageDatastore('Data','IncludeSubfolders',true,'LabelSource','foldernames');
Error using imageDatastore
Cannot find files or folders matching: 'Data'.
[training_set, test_set] = splitEachLabel(imdsData,0.7,'randomized');
%% Create visual vocabulary
bag = bagOfFeatures(training_set,'VocabularySize',30,'PointSelection','Detector');
scenedata = double(encode(bag, training_set));
%% Visualize Feature Vectors
img = read(training_set(1), randi(training_set(1).Count));
featureVector = encode(bag, img);
title('Visual Word Occurances'); ylabel('Frequency');
xlabel('Visual Word Index');
img = read(training_set(2), randi(training_set(2).Count));
featureVector = encode(bag, img);
subplot(4,2,3); imshow(img);
bar(featureVector);title('Visual Word Occurrences');xlabel('Visual Word Index');ylabel('Frequency');
img = read(training_set(3), randi(training_set(3).Count));
featureVector = encode(bag, img);
subplot(4,2,5); imshow(img);
bar(featureVector);title('Visual Word Occurrences');xlabel('Visual Word Index');ylabel('Frequency');
img = read(training_set(4), randi(training_set(4).Count));
featureVector = encode(bag, img);
subplot(4,2,7); imshow(img);
bar(featureVector);title('Visual Word Occurrences');xlabel('Visual Word Index');ylabel('Frequency');
%% Create a Table using the encoded features
SceneImageData = array2table(scenedata);
sceneType = categorical(repelem({training_set.Description}', [training_set.Count], 1));
SceneImageData.sceneType = sceneType;
%% Use the new features to train a model and assess its performance using
%% Can we look at Beaches and Fields and see why they are misidentified?
jj = randi(randi(training_set(ii).Count));
title('Beaches vs. Fields');
%% Test out accuracy on test set!
testSceneData = double(encode(bag, test_set));
testSceneData = array2table(testSceneData,'VariableNames',trainedClassifier.RequiredVariables);
actualSceneType = categorical(repelem({test_set.Description}', [test_set.Count], 1));
predictedOutcome = trainedClassifier.predictFcn(testSceneData);
correctPredictions = (predictedOutcome == actualSceneType);
validationAccuracy = sum(correctPredictions)/length(predictedOutcome)
%% Visualize how the classifier works
ii = randi(size(test_set,2));
jj = randi(test_set(ii).Count);
img = read(test_set(ii),jj);
% Add code here to invoke the trained classifier
imagefeatures = double(encode(bag, img));
% Find two closest matches for each feature
[bestGuess, score] = predict(trainedClassifier.ClassificationSVM,imagefeatures);
% Display the string label for img
if strcmp(char(bestGuess),test_set(ii).Description)
titleColor = [0 0.8 0];
titleColor = 'r';
title(sprintf('Best Guess: %s; Actual: %s',...

Answers (1)

Steven Lord
Steven Lord on 25 May 2023
A datastore array must be a scalar (a 1-by-1 array.) You can't make an array of them, so where you attempt to index into it MATLAB complains. It is trying to prevent you from thinking or using it like it's an array with 1 element per file.
% img = read(training_set(1), randi(training_set(1).Count));
As a simpler example demonstrating the problem:
im = imageDatastore(fullfile(matlabroot, 'toolbox', 'matlab', 'demos', '*.jpg'));
im =
ImageDatastore with properties: Files: { '/MATLAB/toolbox/matlab/demos/cloudCombined.jpg'; '/MATLAB/toolbox/matlab/demos/landOcean.jpg'; '/MATLAB/toolbox/matlab/demos/ngc6543a.jpg' ... and 2 more } Folders: { '/MATLAB/toolbox/matlab/demos' } AlternateFileSystemRoots: {} ReadSize: 1 Labels: {} SupportedOutputFormats: ["png" "jpg" "jpeg" "tif" "tiff"] DefaultOutputFormat: "png" ReadFcn: @readDatastoreImage
ans = 5×1 cell array
{'/MATLAB/toolbox/matlab/demos/cloudCombined.jpg'} {'/MATLAB/toolbox/matlab/demos/landOcean.jpg' } {'/MATLAB/toolbox/matlab/demos/ngc6543a.jpg' } {'/MATLAB/toolbox/matlab/demos/street1.jpg' } {'/MATLAB/toolbox/matlab/demos/street2.jpg' }
Array formation and parentheses-style indexing with objects of class 'matlab.io.datastore.ImageDatastore' is not allowed. Use objects of class 'matlab.io.datastore.ImageDatastore' only as scalars or use a cell array.
Eliminate your indexing on the datastore object itself. Either use the datastore by its name (as I did above when I displayed the im variable) or index into its properties (like the list of Files as I did by the im.Files command above) rather than im(1) (which as you can see threw the error you listed in your question.)

Community Treasure Hunt

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

Start Hunting!