how to receive a array in matlab from processing
1 次查看(过去 30 天)
显示 更早的评论
hi guys, i need some expert advice on this.
I have successfully created the tcp connection between processing and matlab. I am using oscp5 library for that. I am sending an array containing x and y coordinates from processing and my intent is to draw a sine wave in matlab with these received x & y values.
My question is how i can separate the coordinates after receiving in matlab? Also, i think i need to change some setting in tcpip object because i am not receiving the same values in matlab, which i am sending. thank you in advance
You can see the code and their values below.
-------------Processing Code-------------
import oscP5.*;
import netP5.*;
OscMessage myMessage;
OscP5 oscP5tcpClient;
int xspacing = 16; // How far apart should each horizontal location be spaced
int w; // Width of entire wave
float theta = 0.0; // Start angle at 0
float amplitude = 75.0; // Height of wave
float period = 500.0; // How many pixels before the wave repeats
float dx; // Value for incrementing X, a function of period and xspacing
float[] yvalues; // Using an array to store height values for the wave
int x;
void setup() {
size(640, 360);
oscP5tcpClient = new OscP5( this, "141.44.219.161", 1234, OscP5.TCP);
w = width+16;
dx = (TWO_PI / period) * xspacing;
yvalues = new float[w/xspacing];
}
void draw() {
background(0);
calcWave();
OscMessage myMessage = new OscMessage("/test");
myMessage.add(new float[] { x*xspacing, height/2+yvalues[x]}); =====>>> These values are sent to Matlab.
oscP5tcpClient.send(myMessage);
print(x*xspacing, height/2+yvalues[x]);
}
void calcWave() {
// Increment theta (try different values for 'angular velocity' here
theta += 0.02;
// For every x value, calculate a y value with sine function
float x = theta;
for (int i = 0; i < yvalues.length; i++) {
yvalues[i] = sin(x)*amplitude;
x+=dx;
}
}
void renderWave() {
noStroke();
fill(255);
// A simple way to draw the wave with an ellipse at each location
for (int x = 0; x < yvalues.length; x++) {
ellipse(x*xspacing, height/2+yvalues[x], 16, 16);
}
}
-----------------------------------------------------------------
--------------------------MATLAB Code----------------------------
>> tcpipServer = tcpip('141.44.219.161',1234,'NetworkRole','Server');
>> fopen(tcpipServer)
>> data =fread(tcpipServer)
data =
0
0
0
20
47
116
101
115
116
0
0 ===>>> these are values i am receving in matlab, which is completely different from what i am sending.
0
44
102
102
0
0
0
0
0
67
53
127
249
>>
------------------------------------------------------------------
Values which i am sending from processing is look something like below
230.19330 229.068510 227.924120 226.760530 225.578230 224.37770 223.159410 221.923840 220.671510 219.40290 218.118520 216.818880 215.504520 214.175930 212.83370 211.47830 210.110320 208.730290 207.338760 205.936280 204.523440 203.100770 201.668870 200.228290 198.779620 197.323430 195.860320 194.390850 192.915620 191.435230 189.950270 188.461320 186.968980 185.473860 183.976550 182.477630 180.977740 179.477450 177.9773 -------------------------------------------------------------------
0 个评论
采纳的回答
Walter Roberson
2016-8-18
You are storing java float into the array, but you are asking MATLAB to read it as uint8 . Java float appears to correspond to IEEE single precision, so you should add the argument '*single' to your fread()
Note: you might also need to worry about byte order, so you might need to instead use '*uint8' and then swapbytes() or flipud() to get the right byte order to typecast() to 'single'
3 个评论
Walter Roberson
2016-8-21
data = fread(tcpipServer, [2 32], 'float') .' ;
This would result in a 32 x 2 array in which the first column was x and the second was y.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!