0

I am trying to create a dictionary for which the keys are strings. I read an example doing this. However, in my case, I get

Error using containers.Map/subsasgn
Specified key type does not match the type expected for this container.

How is it different from the example in the link?

k = {}
k = [k,{'01'}];
k = [k, {'02'}];
m = containers.Map;
m(cellstr(k(1))) = 1

1 Answer 1

4

cellstr(k(1)) returns a cell type, not a string. So k{1} works:

k = {'01', '02'};
m = containers.Map;
m(k{1}) = 1

This code works for me, with MATLAB 2014b on Linux:

>>     k = {'01', '02'};
    m = containers.Map;
    m(k{1}) = 1

m = 

  Map with properties:

        Count: 1
      KeyType: char
    ValueType: any
Sign up to request clarification or add additional context in comments.

1 Comment

@giulio: With the code above it works, please verify that class(k{1}) is char.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.