Page 1 of 1
Live graphing voltage in Matlab
Posted: Mon Oct 30, 2017 2:36 pm
by dxc536
Hello,
I have the phidgets board interface kit and I have a bend sensor that has an analog input which the phidgets board converts to digital output. I would like to create a 'tracking' program in matlab where I feed in a predefined Sine Wave and the subject is able to see their phidgets voltage in real time. I would like the phidgets to only move along the Y axis (voltage) and then at the end save all outputs for that trial.
Can anyone provide assistants?
Thank you,
Re: Live graphing voltage in Matlab
Posted: Tue Oct 31, 2017 7:59 am
by mparadis
Matlab is only supported in Phidget21, so make sure you install that instead of Phidget22. You can find the drivers
here.
You can download our sample code
here, which will show you how to get data from the Phidget into your Matlab program. Once you get that working, I would look for a general guide on how to display graphs in Matlab (like
this). Then combine the two pieces of code to get live data graphing from your Phidget.
Re: Live graphing voltage in Matlab
Posted: Tue Oct 31, 2017 1:38 pm
by dxc536
Thank you for your reply.
I've looked at both resources and I have a follow-up question.
Essentially I am able to get the read out using the analogin.m function.
In order to get continuous output I placed the function in a while loop and have continuously plotted the sensor value:
-----
n = 1;
while n < 125
analogin(0)
n = n+1;
end
----
I'm sure this is probably not the right set up because the website says matlab can sample at 125 samples/second. However, when in this loop it's sampling much slower. Is there something else I should be doing in order to read continuous sensor data at a much faster rate?
Thank you,
Re: Live graphing voltage in Matlab
Posted: Tue Oct 31, 2017 1:46 pm
by mparadis
You'll want to use
CPhidgetInterfaceKit_setDataRate to set the data rate (in milliseconds). You can use the callib() Matlab function to call it, as shown in the code samples on the
Matlab page.
Re: Live graphing voltage in Matlab
Posted: Wed Nov 01, 2017 10:09 am
by dxc536
Thank you! That helped a lot.
Sorry, but I have another question.
I was able to plot the sensor value continuously in the while loop - this is the orange plot in the attachment. (
)
Ultimately I would like rescale the values in the while loop so they range between 0 to 100 based on the min and max of the sensor value (it's a bend sensor).
I used the following equation:
norm = ((analogin - sensor_min)/(sensor_max-sensor_min))*100
When I applied this equation in the while loop it only calculated '100' and '0' - this is the blue plot in the attachment.
If I apply the same equation to the array that I originally collected then I am able to get the graph I want, but this is after the fact - not in real time. This is the purple plot.
Does this have something to do with how the data is being acquired? Is there something else I can do to achieve the purple plot in real time?
Thanks again for all of your help.
Re: Live graphing voltage in Matlab
Posted: Wed Nov 01, 2017 10:35 am
by mparadis
If the blue line is only ever 0 or 100, it means that
(analogin - sensor_min)/(sensor_max-sensor_min)
is evaluating as either 0 or 1. I haven't used Matlab in a while but this sounds like a data type issue. For some reason Matlab thinks that this number is an integer when it's really a float. Make sure that those four variables are defined as floats or doubles rather than integers.