MATLAB: How to publish non-string messages using the MQTT Toolbox

arraydatainternetiotMATLABmosquittonumbernumericofreadreceivesendstructuresubscribetcpthings

We would like to publish messages in MATLAB MQTT toolbox that are not just a single string, for example a structure of different data type (Array of numbers, string, etc.) Can you please suggest a way to do so and if possible send us an example?

Best Answer

The message to be passed into the 'publish' function must be a string or a character array. Anything that is subscribed to the MQTT service will receive messages as a text.
If you would like to publish something like an array of numbers to the subscribed devices, you can pass the array as a string of numbers and process it on the receiving end:
 
>> myMQTT = mqtt('tcp://localhost')
>> mySub = subscribe(myMQTT, 'Topic')
>> publish(myMQTT, 'Topic', '1 2 3 4 5')
>> output = mySub.read
>> myArray = str2double(strsplit(output, ' '))