How do I decode and analyze a signal in Simulink in Real-Time??

9 views (last 30 days)
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

Answers (1)

Walter Roberson
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" ?
Perhaps instead of using evalin() you should be using a From Workspace block?
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.
  1 Comment
Njanssen
Njanssen on 1 Dec 2015
The input u is a timeseries where the data type is uint8. The size of OUT2 differs, because it is dependent on the size of u. The size of u is in turn dependent on how long the simulation runs (will differ for each participant testing). The data itself is fed trough directly from the original measurement program, cramped into one big timeseries file. I tried using a from workspace block but then I would also need to add a delay or something otherwise at the start of the simulation the function block would try to read a file that does not yet exist right? I'm not familiar with the Interpreted Matlab Block so I don't know if that is an option.
I apoligze if what I'm saying doesn't make a lot of sense but I'm in way over my head here :P

Sign in to comment.

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!