Matlab:指定的值类型与此容器的预期类型不匹配 [英] Matlab: Specified value type does not match the type expected for this container

查看:122
本文介绍了Matlab:指定的值类型与此容器的预期类型不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的问题的一个例子:



  valueSet = myClass.empty(4,0); 
keySet = cell(1,4);

for i = 1:4
valueSet(i)= myClass();
keySet {i} = valueSet(i).name;
end
map = containers.Map(keySet,valueSet);

  classdef myClass<处理
属性
名称;
end

方法
函数self = myClass()
self.name = randstr(10);
end

function output = randstr(n)
symbols = ['a':'z''A':'Z''0':'9'];
nums = randi(numel(symbols),[1 n]);
output = symbols(nums);
end
end
end

我得到这个错误: / p>

 使用containers.Map的错误
指定的值类型与此容器的预期类型不匹配。

然而,matlab文档说:


mapObj = containers.Map(keySet,valueSet)构造一个Map,
包含一个或多个值,每个值都有一个唯一的键。



keySet 1-by-n数组,为地图指定n个唯一键。如果n>
1并且键是字符串,则keySet必须是单元格数组。



valueSet:1-by-n数组的任何类,它指定
的n个值。 valueSet中的值的数目必须等于keySet中的
键的数量。


我还尝试指定类类型但是它也引发了一个错误:

  containers.Map('KeyType','char','ValueType','myClass' )
使用containers.Map的错误
指定了不支持的ValueType'myClass'。请参阅有效值类型的文档。

所以我不明白...如果containers.Map正在为任何类工作,为什么不适用于myClass?

解决方案

如果你这样做

  help containers.Map 

你会得到一个说明


vType的有效值为字符串:'char','double','single',
'int8','uint8','int16','uint16','int32','uint32','int64',
'uint64','logical','any'。键值类型和值
类型参数的顺序并不重要,但必须提供。


您可以使用:

  containers.Map('KeyType','char','ValueType','any')

但是,您可能想要的行为是:

  myMap = containers.Map(keySet,num2cell(valueSet))

当您放入正确的键时,将为您提供一个类型为myClass的对象。这很可能是因为 containers.Map 正在期待一个单元格数组的自定义对象而不是一个对象数组。



您的代码看起来更清晰:

  valueSet = cell (1,4); 
keySet = cell(1,4);

for i = 1:4
valueSet {i} = myClass();
keySet {i} = valueSet {i} .name;
end

map = containers.Map(keySet,valueSet);


I am experiencing problems to use the containers.Map of matlab.

Here an example of my issue:

When I try to build the map of an array of myClass instance with a cellarray of keys defined as :

valueSet = myClass.empty(4,0);
keySet = cell(1,4);

for i=1:4
   valueSet(i) = myClass();
   keySet{i} = valueSet(i).name;
end
map = containers.Map(keySet, valueSet);

with

classdef myClass < handle
    properties
        name;
    end

    methods
        function self = myClass()
            self.name = randstr(10);
        end

        function output = randstr(n)
            symbols = ['a':'z' 'A':'Z' '0':'9'];
            nums = randi(numel(symbols),[1 n]);
            output = symbols (nums);
        end
    end
end

I get this error:

Error using containers.Map
Specified value type does not match the type expected for this container.

However the matlab documentation says :

mapObj = containers.Map(keySet,valueSet) constructs a Map that contains one or more values and a unique key for each value.

keySet 1-by-n array that specifies n unique keys for the map. If n > 1 and the keys are strings, keySet must be a cell array.

valueSet : 1-by-n array of any class that specifies n values for the map. The number of values in valueSet must equal the number of keys in keySet.

I also tried to specified the class type but it raised also an error:

containers.Map('KeyType','char', 'ValueType','myClass')
Error using containers.Map
Unsupported ValueType 'myClass' specified.  See documentation for valid value types.

So I don't understand... if containers.Map is working for any class, why is not working for myClass ?

解决方案

If you do

help containers.Map

you get a section which says

Valid values for vType are the strings: 'char', 'double', 'single', 'int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'int64', 'uint64', 'logical', or 'any'. The order of the key type and value type arguments is not important, but both must be provided.

You could use:

containers.Map('KeyType','char', 'ValueType','any')

However, the behaviour that you probably want is:

myMap = containers.Map(keySet, num2cell(valueSet))

That will give you one an object of type myClass when you put in the correct key. This is most likely because containers.Map is expecting a cell array of custom objects rather than an object array.

Your code would look clearer like this:

valueSet = cell(1,4);
keySet = cell(1,4);

for i=1:4
   valueSet{i} = myClass();
   keySet{i} = valueSet{i}.name;
end

map = containers.Map(keySet, valueSet);

这篇关于Matlab:指定的值类型与此容器的预期类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆