Use in Python
Save xmat-file
save variable as a key-value pair in a file
import pathlib
import numpy as np
import numpy.random
import xmat
# set the file name
file_out = pathlib.Path(__file__).parents[2].joinpath('data', 'example_python.xmat')
xout = xmat.MapStreamOut.file(file_out)
# floating-point numbers
xout.setitem('a', 3.14)
xout.setitem('b', [n/3 for n in range(-3, 4)])
xout.setitem('c', np.arange(2*3).reshape([2, 3]))
xout.setitem('d', np.arange(2*3*4).reshape([2, 3, 4]))
xout.setitem('e', np.arange(3**5).reshape([3, 3, 3, 3, 3]))
# integer numbers
xout.setitem('f', numpy.random.randint(0, 255, size=(2, 3), dtype=np.uint8))
xout.setitem('g', numpy.random.randint(-128, 127, size=(2, 3), dtype=np.int8))
# complex-value numbers
xout.setitem('h', np.exp(1j*2*np.pi*3*np.linspace(0, 1, 32)))
xout.setitem('i', np.exp(np.pi*np.array([2., 3., 4.])*np.linspace(0, 1, 32).reshape([-1, 1])))
# ascii-string
xout.setitem('j', 'string variable from python')
# name of a data-block could be any ascii-string with less than 32 length
xout.setitem('longer-name', 1.)
xout.setitem('maximun_allowed_block_name_is_32', 2.)
xout.setitem('name_can_contain_any_ascii:@<>[]', 3.)
xout.close()
Load xmat-file
get the value by string key
import pathlib
import numpy as np
import xmat
# set the file name to load
file_in = pathlib.Path(__file__).parents[2].joinpath('data', 'example_python.xmat')
xin = xmat.MapStreamIn.file(file_in)
print(xin)
print(xin.getitem('a'))
print(xin.getitem('b'))
print(xin.getitem('c'))
xin.close()
TCP-server
Open xmat-server and receive / reply message
import time
import xmat
ipaddress = xmat.IPHOST # OR '127.0.0.1' OR other ip-string
ipport = xmat.PORT # OR 27015 OR other port-number
print('create connection')
xtcp = xmat.TCPService()
listener = xtcp.listener((ipaddress, ipport))
connection = xtcp.accept(listener)
print('receive message')
xin = connection.recv()
msg = xin.getitem('msg')
data = xin.getitem('data')
print('msg: ', msg)
print('data: ', data)
print('reply message')
xout = xmat.MapStreamOut.byte()
xout.setitem('msg', 'message from server: Python')
xout.setitem('data', data)
xout.close()
connection.send(xout)
time.sleep(2.0)
xtcp.close()
TCP-client
Open xmat-client and send / receive message
import xmat
ipaddress = xmat.IPLOCALHOST
ipport = xmat.PORT
print('create connection')
xtcp = xmat.TCPService()
connection = xtcp.connection((ipaddress, ipport))
print('send message')
xout = xmat.MapStreamOut.byte()
xout.setitem('msg', 'message from client: Python')
xout.setitem('data', [float(n) for n in range(1, 9)])
xout.close()
connection.send(xout)
print('receive message')
xin = connection.recv()
msg = xin.getitem('msg')
data = xin.getitem('data')
print('msg: ', msg)
print('data: ', data)
xtcp.close()