The specified key is not present within this container error

조회 수: 22 (최근 30일)
Kevin Akash Rajasekaran
Kevin Akash Rajasekaran 2021년 5월 17일
답변: Maneet Kaur Bagga 2024년 5월 10일
Hey all! So I'm running the following code to essentially identify and extract an image within a database of images and assign it as 'rec'. Here's the code:
classdef (Abstract) db
properties (GetAccess = public, SetAccess = protected)
path; % path(s) containing the images and metadata
info; % info for each image in the database
end
properties (Dependent)
keys;
imgs;
end
methods
function v = get.keys(db)
v = db.info.keys();
end
function v = get.imgs(db)
v = db.info.values();
end
end
methods
function db = db(varargin) % constructor
% create empty database
db.path = {};
db.info = containers.Map('KeyType','char','ValueType','any');
if nargin < 1
return
end
% varargin contains one or more paths to add
for ii = 1:nargin
db = db.add(varargin{ii});
end
end
end % methods
methods (Abstract)
db = add(db,pth); % add images in pth to the database
img = getImg(db,key,varargin); % retrieve an image (by key) from disk
end
end
I call the above script using this command
db = imgdb.geisler('C:\toolbox\image-db\Set1_campus');
But when I try to assign an image with the specific key i.e the image file name through this
rec = db.info('D7H_3900')
I get this error
Error using containers.Map/subsref
The specified key is not present in this container.
Can someone kindly point me towards where I'm going wrong? Note that to parse through each file, I'm using the rdir() recursive directory listing extension. So I'm thinking there might be a problem with that maybe? Thanks in advance!

답변 (1개)

Maneet Kaur Bagga
Maneet Kaur Bagga 2024년 5월 10일
Hi,
As per my understanding to resolve te error you may refer to the following workaround:
Before directly accessing an item in "Containers.Map" with the key, check if the key exists to prevent the runtime error. Also, it is case-sensitive, so when you attempt to access an item in the map, the key's case matches exactly how it was added.
if isKey(db.info, 'D7H_3900')
rec = db.info('D7H_3900');
else
error('Key not found in the database.');
end
As you have not mentioned the implementation of the add method check if it populates correctly the data in "db.info" map. Please refer to the following as the reference to the "add" method.
function db = add(db, pth)
% Example implementation of the add method
files = rdir([pth, '\*.jpg']); % Adjust the pattern as needed
for i = 1:length(files)
fileName = files(i).name;
key = extractFileName(fileName); % Implement this function based on your naming scheme
value = ... % Whatever value you want to associate with this key
db.info(key) = value;
end
end
Also as an alternate option if you want to use your "filename" as the key, extract the filename from the fullpath before adding it to the map to access an item in the map.
Hope this helps!

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by