Error using containers.Map
15 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Guillaume
2016-12-1
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 个评论
lotus whit
2017-11-3
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)
Walter Roberson
2017-11-3
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.
更多回答(1 个)
Walter Roberson
2016-11-30
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!