How do I decode and analyze a signal in Simulink in Real-Time??
10 views (last 30 days)
Show older comments
So I'm currently trying to set up an experiment where I'm running a driving simulation on my own laptop and I want to use some eye tracking data to either let the simulation run automatically or run manually using an external steering wheel. I have equipment that measures the eyes and that data is received on laptop A. I then send that data in real-time to my own laptop (which is running the simulation using simulink) using an UDP connection between the two laptops. Now that you know the whole idea, here comes the issue I keep encountering. The data is sent to my laptop and needs to be adjusted before it can be used in any way. Someone who worked with this equipment before me sent me his code:
OUT = evalin('base',u);
OUT2 = double(squeeze(OUT.Data));
for k = 1:size(OUT2,2)
GDX(k)=swapbytes(typecast(uint8(OUT2(81:88,k)),'double')); % GD = Gaze Direction
GDY(k)=swapbytes(typecast(uint8(OUT2(89:96,k)),'double'));
end
I checked it and it works fine for data analysis when I use it after all the data has been collected. However, because my simulation is dependent on the results from that data I need to adjust and analyse it in real-time. So I put a Matlab Function Block in simulink and added:
function [GDX,GDY] = fcn(u)
coder.extrinsic('evalin');
OUT = evalin('base',u);
OUT2 = double(squeeze(OUT.Data));
for k = 1:size(OUT2,2)
GDX(k)=swapbytes(typecast(uint8(OUT2(81:88,k)),'double')); % GD = Gaze Direction
GDY(k)=swapbytes(typecast(uint8(OUT2(89:96,k)),'double'));
end
But now when I run it I get an issue and it doesn't work anymore:
Attempt to extract field 'Data' from 'mxArray'.
Function 'MATLAB Function' (#336.101.104), line 4, column 23: "OUT"
I'm fairly new to matlab and simulink so I really don't know how to proceed from this point anymore. I would really appreciate any help since I can't move further without it :P
Thx!
Nico
0 Comments
Answers (1)
Walter Roberson
on 30 Nov 2015
You can get past that immediate problem by changing
OUT = evalin('base',u);
OUT2 = double(squeeze(OUT.Data));
to
OUT2 = evalin('base', sprintf('double(squeeze(%s.Data))',u));
However you will find that using OUT2 will give you an error about using mxArray.
What you have to do is assign something to OUT2 before you make the above evalin() call. If you do not know the size ahead of time then you will need to use coder.varsize
But I have to ask: you are implicitly using the passed-in u as the name of a variable in the evalin(), since it is pointless to evalin() a numeric array (the result would just be the numeric array.) But http://www.mathworks.com/matlabcentral/answers/30149-text-input-in-simulink
"Simulink signals can only be numeric or buses (structures) comprising numeric types. Character data types are not allowed."
so you cannot be passing in a string as u.
What exactly are you passing in as u? The data itself? A "bus" ?
Using evalin() would, by the way, prevent you from using Rapid Acceleration or code generation. Perhaps you would be better off using an Interpreted MATLAB Block rather than a MATLAB Function Block.
See Also
Categories
Find more on Simulink Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!