Error Brace indexing is not supported

Hi there,
I am having an odd problem, and I'm not sure how to fix the error below. Any assistance would be greatly appreciated.
Brace indexing is not supported for variables of this type.
Many thanks
Following is the code i am using
imageFileNames = imageFileNames(imagesUsed);
originalImage = imread(imageFileNames{1});
Brace indexing is not supported for variables of this type.
class(imageFileNames)
ans =
'char'
imageFileNames
imageFileNames =
'rcngvieo ram'
where imagesUsed =
25×1 logical array
0
0
1
0
0
0
0
1
0
1
1
0
1
1
0
1
1
1
0
1
1
1
0
0
0

3 Comments

Stephen23
Stephen23 on 4 Apr 2023
Edited: Stephen23 on 4 Apr 2023
"I am having an odd problem"
It is not odd at all: the variable imageFileNames is a character array, for which curly brace indexing is not defined.
Thus the error.
"I'm not sure how to fix the error below. Any assistance would be greatly appreciated."
Do you expect imageFileNames to be a cell array? If so, then you will need to check your code for the place where you replaced the expected cell array with a char array of the exactly same name (because it isn't in the code that you showed us). That will fix the error.
Do you expect imageFileNames to be a cell array
This is not a cell array, it's a char
class(imageFileNames)
ans =
'char'
"This is not a cell array, it's a char "
Sure, which is exactly why you cannot use curly brace indexing with it.
However I did not ask you what it is, I asked you what you expect it to be. A totally different question.

Sign in to comment.

 Accepted Answer

How did you get imageFileNames? Was it from using dir somehow?
% Get file listing of all PNG files in the current folder.
fileList = dir('*.png');
% Convert from structure to cell array.
imageFileNames = {fileList.name};
% Load that cell array into a listbox.
handles.listbox1.String = imageFileNames;
Was it from a listbox of file names that you had loaded up previously? Then you can do
% Get a list of all filenames in the listbox.
imageFileNames = handles.listbox1.String;
% Get the indexes the user selected.
imagesUsed = handles.listbox1.Value;
% Extract the selected names into a subset.
selectedImageFileNames = imageFileNames(imagesUsed);
% Get the name of the first one that was selected:
firstImageFileName = imread(selectedImageFileNames{1});
[EDIT}
Most likely the problem was you did this
imageFileNames = [fileList.name] % Used brackets to concatenate.
instead of this
imageFileNames = {fileList.name} % Should use braces to concatenate.
That will give you spaces in the name and make a long character array, rather than a cell array.
See the FAQ to learn when to use parentheses, braces, and brackets:

6 Comments

I am using following code , it isn't derived from either dir nor listbox.
% Extract the selected names into a subset.
selectedImageFileNames = imageFileNames(imagesUsed);
% Get the name of the first one that was selected:
firstImageFileName = imread(selectedImageFileNames{1});
How do you create imageFileNames ?
What I asked was how you got the original imageFileNames. You did not tell me this.
Like I said
Most likely the problem was you did this
imageFileNames = [fileList.name] % Used brackets to concatenate.
instead of this
imageFileNames = {fileList.name} % Should use braces to concatenate.
Yup, I did as you are saying which is derived from the imageDatastore function
imageFileNames = imageDatastore({PathToimageFileNames},"FileExtensions",[".png", ".jpg",".tif"]);
% Detect and localize the tags
[imagePoints, imagesUsed] = detectPatternPoints(detector, imageFileNames.Files);
imageFileNames.Files
That's not how you do it. Try this
imds = imageDatastore({PathToimageFileNames},"FileExtensions",[".png", ".jpg",".tif"]);
imageFileNames = imds.Files
It's perfect!!!! You've got the problem under control, now I can see
imageFileNames = imageFileNames(imagesUsed);
K>> class(imageFileNames)
ans =
'cell'
Your proposal work well.
Thank you

Sign in to comment.

More Answers (1)

Your imageFileNames variable is not a cell array, so you cannot use brace indexing with it. It is a long char array with words separated by spaces. You can use the split command to generate a cell array of char variables from it:
imageFileNames = 'rcngvieo ram'
imageFileNames = 'rcngvieo ram'
names = split(imageFileNames, ' ')
names = 2×1 cell array
{'rcngvieo'} {'ram' }
names{1}
ans = 'rcngvieo'
names{2}
ans = 'ram'

3 Comments

But then I am facing new error , is this expected ?
Thank you !!
>> names = split(imageFileNames, ' ')
names =
2×1 cell array
{'rcngvieo'}
{'ram' }
>> imread(names{1} )
Error using imread>get_full_filename
File "rcngvieo" does not exist.
I am assuming 'ram' is the file extension for your image file. If that is the case, the way you construct the imageFileNames variable is problematic. The imread command needs the full name of the file, including the file extension.
No, it was a logical index that was changed to a char rather than a cell.

Sign in to comment.

Categories

Products

Release

R2022b

Tags

Community Treasure Hunt

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

Start Hunting!