Error using containers.Map

Hello, I am having following problem. I want to display single cells from my string array if my function finds a specific string. Here is the code:
function voddi_test(graph)
global vodcode
system_name = extract_system_name(graph);
variables = containers.Map();
variable = containers.Map();
keys = variables.keys;
for name = variables.keys
variable(name{1}) = true;
end
for node_name = filter_graph(graph, 'Variable', true)
node = graph(node_name{1});
for arg = node.arguments
variables([system_name '/' arg{1}]) = true;
end
end
for keys = 1:numel(variables)
if strcmp(variables(keys), 'Constant')
variables(keys{1})
else
variables(keys{2})
end
end
end
I am keeping getting the error message "Specified key type does not match the type expected for this container" when I try compare the content of the variables map with my desired strings. The code works fine, expect for the last for - loop. Any help is much appreciated, many thanks.

 Respuesta aceptada

Guillaume
Guillaume el 1 de Dic. de 2016

1 voto

Note: I would avoid having variable names that only differ by one being plural and singular unless one was just a scalar element of the plural container.
As per Walter's answer, it seems you have not shown all the code, since your first loop is useless if the map is not filled.
Assuming the map declaration you've shown is the actual you use:
variables = containers.Map();
then it uses 'char' as the key type. However, you then have:
for keys = 1:numel(variables)
if strcmp(variables(keys), 'Constant')
There are two issues here. For starter, since variables is a map, numel(variables) is just 1. Perhaps you meant, variables.Count. Secondly, keys will be a double. As said the key type is 'char', not 'double', so you can't actually use your keys as a key into the map. Hence the error message.
Perhaphs, you meant:
for elementidx = 1 : variables.Count
if strcmp(variables.values{elementidx}, 'Constant') %if you want to compare the value
%or maybe
if strcmp(variables.keys{elementidx}, 'Constant') %if you want to compare the actual key

8 comentarios

jungdnb
jungdnb el 2 de Dic. de 2016
Thank your for the clarification. I thought since the class Map is like an array, I could use numel. I've tried your code for comparing the actual key, since this is what I need. However I am keeping getting the error message: "No method 'keys' with matching signature found for class 'containers.Map'. So I guess, you're right, my first For Loop is useless.
Guillaume
Guillaume el 2 de Dic. de 2016
"No method 'keys' with matching signature found for class 'containers.Map'"
That is extremely odd. keys is a method of the containers.Map class, so you should never see this message, even if you've never filled the map.
Please, show the exact code you're using.
jungdnb
jungdnb el 2 de Dic. de 2016
It is working now. Many Thx.
function voddi_test(graph)
global vodcode
global vod_vars
vod_vars = containers.Map();
rev_vodcode = fliplr(vodcode); %umgekehrte reihenfolge
rev_vodcode = containers.Map;
for vars_idx = 1 : vod_vars.Count
for code_idx = 1: rev_vodcode.Count
if strcmp(vod_vars.keys{vars_idx}, rev_vodcode.keys{code_idx}) %key vergleich
match = 'jo'
else
match = 'nope'
end
end
end
end
Guillaume
Guillaume el 2 de Dic. de 2016
Note you can get a match matrix without the loops easily in R2016b:
match = strings(vod_vars.keys).' == strings(rev_vodcode.keys)
The rows are the keys of vod_var, the columns the keys of rev_vodcode.
In version < R2016b, this would do the same:
match = bsxfun(@(r, c) strcmp(vod_vars.key{r}, rev_vodcode.keys{c}), (1:vod_vars.Count).', 1:rev_vodcode.Count)
lotus whit
lotus whit el 3 de Nov. de 2017
I have the same error when run the code , B1 is string like this
( 'البَهارُ' 'نبت له نور أصفر) in Arabic language , so how i can solve it?
B1=table_Ba{46};
B2= table_Ba{36};
B3=table_Ba{6};
B4=table_Ba{26};
keySet1 = {'1 0 1 0 1','1 1 1 1 1','1 0 0 0 1','1 0 0 0 0'};
valueSet1 = [B1,B2,B3,B4];
mapObj1 = containers.Map(keySet1,valueSet1)
Walter Roberson
Walter Roberson el 3 de Nov. de 2017
lotus whit, I am not sure what code you are running?
lotus whit
lotus whit el 3 de Nov. de 2017
This is my all code :
filename = 'BA.xlsx';
% [num,txt,raw] = xlsread(filename)
[~, txt] = xlsread(filename,'Sheet1');
s_t= size(txt);
for ri=1:s_t(1)
for ci=1:s_t(2)
A=txt(ri,1);
B =txt(ri,2);
table_Ba{ri} = [A B];
end
end
% B1(:,1)= A;
B1=table_Ba{46};
B2= table_Ba{36};
B3=table_Ba{6};
B4=table_Ba{26};
keySet1 = {'1 0 1 0 1','1 1 1 1 1','1 0 0 0 1','1 0 0 0 0'};
valueSet1 = [B1,B2,B3,B4];
mapObj1 = containers.Map(keySet1,valueSet1)
valueSet1 = {B1,B2,B3,B4};
is my guess. If B1 and so on are character vectors, then [B1,B2,B3,B4] is a single concatenated character vector, which would not be appropriate for a list of keys like you have in keySet1.

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 30 de Nov. de 2016

0 votos

You create variables as an empty map. It has no keys. So your first for loop does nothing so no keys are created for variable

1 comentario

jungdnb
jungdnb el 1 de Dic. de 2016
Here is a screenshot from my matlab command line. First list are my variables.keys. Second list are the strings, in which I have to search for the variables.keys . Actually I've managed to modify the source code, so it does kind of the thing I want it to do. I want to test it for a bit, and will upload tomorrow, If I don't finde the right answer.

Iniciar sesión para comentar.

Categorías

Preguntada:

el 30 de Nov. de 2016

Comentada:

el 3 de Nov. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by