containers.Map不能使用数字键

时间:2017-01-27 12:32:12

标签: matlab

我在Matlab中有一个containers.Map对象有以下问题。 使用数字键和值创建一个简单对象:

class ViewController: UIViewController, CBCentralManagerDelegate {

  var manager: CBCentralManager!
  var peripherals: [CBPeripheral]?

  @IBAction func actionScanBLEDevice(_ sender: AnyObject) {
     manager = CBCentralManager (delegate: self, queue: nil)
  }

  @IBAction func actionDisconnectBLE(_ sender: AnyObject) {
     //Use As per need where you want to disconnect perticular  peripheral
     manager.cancelPeripheralConnection(peripheral[0]) // Disconnent numbers of BLE Devices
  }

  func centralManager(_ central: CBCentralManager, didDisconnectPeripheral peripheral: CBPeripheral, error: Error?) {
     //Manage Some Condition then disconnect
     print("Disconnected from peripheral")  
     peripherals?.append(peripheral)
  }  

  func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {

    print("Discovered peripheral \(peripheral.name,peripheral.identifier.uuidString)")
    print("advertisementData\(advertisementData)")
    // Use discovered peripheral here 
  }

  func centralManagerDidUpdateState(_ central: CBCentralManager) {
    print("Checking")
    switch(central.state)
    {
    case.unsupported:
        print("BLE is not supported")
    case.unauthorized:
        print("BLE is unauthorized")
    case.unknown:
        print("BLE is Unknown")
    case.resetting:
        print("BLE is Resetting")
    case.poweredOff:
        print("BLE service is powered off")
    case.poweredOn:
        print("BLE service is powered on")
        print("Start Scanning")
        manager.scanForPeripherals(withServices: nil, options: nil)
    }
  }

}

按预期工作,但

m = containers.Map(1:3,2:4);
>> m(1)
ans =
 2

给了我一个我不期望也不理解的错误,因为关键类型显然是正确的。

我做错了吗?

2 个答案:

答案 0 :(得分:2)

containers.Map

默认构造使用的kTypekeySet中的键的数据类型决定,(此处为double)。

containers.Map不能有KeyType以外的任何内容:

'char', 'double', 'single', 'int32', 'uint32', 'int64', or 'uint64'.

在您的示例中,1:2是1x2的矩阵,因此不能用作密钥。

答案 1 :(得分:1)

使用

调用容器时
m(1:2)

您提供的1x2矩阵不允许作为containers.map中的键。 Matlab不允许基于地图类中的多个键来矢量化多个值的检索。

如果您的代码严重依赖于地图类,并且经常需要提取多个键,您可能需要重新考虑使用地图类。

参见"避免使用containers.map"在Yair Altman的书Accelerating Matlab Performance中。

相关问题