Writing matricies to ThingSpeak with MATLAB - matlab

I'm trying to use ThingSpeak to allow remote multiplayer for a game in MATLAB. All of the player data is stored in a table called playerTable, and I want to send this entire matrix to ThingSpeak, preferrably in a single field.my attempt
app.playerTable = ["","";"","";"","";"","";"","";"","";"","";"","";"","";"","";"","";"","";"","";"","";"","";"",""];
app.playerTable([7,15,16],1:2) = 0;
app.tStamps = datetime('now')-minutes(15):minutes(1):datetime('now');
thingSpeakWrite(app.channelID, app.playerTable, 'Fields',1,'Timestamp',app.tStamps, 'WriteKey',app.writeKey)
Here's the basis of what I have now. This approach is modeled closely from the ThingSpeak documentation. The image above shows the error I am getting.
I'm very new to MATLAB, and I would appreciate any guidance.

Related

Panel data regression comparison in Matlab

I have a very large panel data and would like to apply a number of simple machine learning techniques in Matlab (Logistic Regression, Decision Trees, Bagged Trees).
During my preparation I came across fitglm and fitLifetimePDModel, the latter of which is meant to capture panel data. I was trying to understand how/if that differs from fitglm because when I try the below, the results are exactly the same.
Why is that? For example, under fitglm I'm not telling the program that each customer can have more than one data points.
load RetailCreditPanelData.mat
pdModel_1 = fitLifetimePDModel(data,"Logistic", 'AgeVar','YOB', 'IDVar','ID', 'LoanVars','ScoreGroup','ResponseVar','Default');
disp(pdModel_1.Model)
pdModel_2 = fitglm(data,'Default ~ 1 + ScoreGroup + YOB', 'Distribution','binomial', 'link', 'logit');
disp(pdModel_2)

PLot Realtime data using MATLAB

I am trying to plot real-time data from a sensor that has a frequency of 25 Hz. I read data from the sensor using TCPIP protocol, parse it and then plot the data. However, the plotting part is not very fast and starts to lag after some time. So for e.g. if I move the sensor, I see the response 5 seconds later. I used Serial Plotter in Arduino (which has much less specifications than my laptop) but it is able to plot real-time data without any delays / problems.
My code looks a bit like the following
IMUData = nan(1500,6);
InterfaceObject = tcpip('my_ip_address',50001);
InterfaceObject.BytesAvailableFcn ={#PlotSensorData};
And the PlotSensorData function looks like
function PlotSensorData(~,~)
RecievedData = fscanf(InterfaceObject,'%s');
Identifier = RecievedData(6); % 6th byte is the sensor identifier
DataStartIdx = 28; % For each sensor, data start position is common
if Identifier == 'I'
DataEndIdx = DataEndPosition(RecievedData, 1);
SlicedData = RecievedData(DataStartIdx:DataEndIdx);
ParsedData = textscan(SlicedData,'%f', 'Delimiter',',');
% Append new data to IMUData matrices
prevval = IMUData;
val = [prevval(2:end,:); ParsedData{1}'];
IMUData = val;
set(PlotHandle{1},'ydata',val(:,3));
set(TopAxes,'ylim',[-15 15]);
drawnow limitrate;
end
end
Also, instead using plot, I have already tried animatedLine. It seems faster initially as it plots very fast, but after sometime, it starts to lag as well and the lag is more than 10 Sec.
So my questions are
What can be done to speed up the real-time data in MATLAB.
Also, I have already tried plotting the data after a certain number of samples (say 10, 20) are received instead of plotting after every received sample, but the results are still lagging and the GUI hangs as well. Is there any other strategy that I can use? In Python I used Multi-threading, but can I use it here as well? Or is there a better approach to handle this data rate?
I understand that Arduino is only running one script but the computer has a lot of overhead, but how is Arduino able to plot the data so fast while MATLAB just hangs up?
Thanks
Reading Data Asynchronously instead of continuously solved my problem.

Use both GUI and script at the same time

I have a .m file (script) that is controlling a real-time robot.
What i do within this file is:
1- find a trajectory
2-infinite loop:
read from robot
update robot
plot some stuff (basically I'm drawing a new point in each iteration that represents the position of the robot in a previously opened map, it's updating the map)
end of loop
What I want to do is to create a GUI that allows me to make the plots and see some values that the robot returns at the same time, in real time.
From what I read, MATLAB can't run both a script and a GUI at the same time.. I can make it plot in real time in the GUI but I can't seem to be able to update the values returned by the robot in text boxes in GUI..
Do I have to put it all in the same file or is there a way for the GUI and the script to work in separate files?
Thank you in advance!
MATLAB has not trouble running both. I don't know where you read that but it's not true; MATLAB is not the best tool to solve this problem, but it can do it.
First, I'm going to frame your problem in code, to make it easier to solve. Your question is vague and general, so my response has to be be general as well. I'm making some assumption about your function structure, but it really should look a lot like this:
endflag = 0;
while ~endflag
robotData = getRobotData(robotHandel);
derivedData = doStuffWithData(robotData);
updateRobot(derivedData);
showData(robotData, derivedData)
endflag = checkEndFlag(robotData, derivedData)
end
So, your problem is the showData functionality. What it should do, is determine which values need to be displayed from it's inputs, and pass those to your GUI. Like so:
function showData(robotData, derivedData)
guiInputData = dataParser(robotData, derivedData)
YourGUIFunctionName(guiInputData)
end
The GUI function should then build itself using those inputs. Any GUI function working that way, will do what you want it to. If you want a more specific solution, you need to give me more specific information about your problem. Good luck, I hope this helps.

Create a circular buffer for image acquisition

I'm new in programming with matlab and trying to do the following:
I continously capture an image (size 1024x1024) with a camera to have a live image using the getdata function.
To do a measurement I would like to store only 100 images using a circular buffer- more precisely I'm thinking of storing 100 images and erasing the oldest images if new data is acquired and do a measurement on the last 100 images.
Hope my concern is understandable...
Thanks for an answer!
This question has been answered here by a worker from MathWorks : Create a buffer matrix for continuous measurements. ( He also made a video of it : http://blogs.mathworks.com/videos/2009/05/08/implementing-a-simple-circular-buffer/
The code :
buffSize = 10;
circBuff = nan(1,buffSize);
for newest = 1:1000;
circBuff = [newest circBuff(1:end-1)]
end
Check the update made by gnovice which applies the circular buffer to image processing.
What you call a "circular buffer" is known as a queue or FIFO (First In, First Out). Usually this would be stored in a linked list data structure, where every object (matrix, in your case) points to the next object. In Matlab however, there is not built-in linked list structure, but Matlab arrays (vectors/matrices) are pretty flexible and efficient when it comes to manipulating them.
So you can simply store each image as a matrix inside an array of length 100, giving you a 3 dimensional matrix of dimensions 100x1024x1024. Then, when you get new data you simply remove the last matrix from the array and insert a new matrix at the beginning of the array. Hopefully this will be fast enough for you.
Good luck!
May you can create an array of 100 1024x1024-matrices. and refer the following link to maintain the read and write position.
logic of circular buffer

Modelica: use of der() in a custom class/model

I'm trying to learn Modelica and I have constructed a very simple model using the multibody library. the model consists of a world object and a body (mass) connected to to beams which are then connected to 2 extended PartialOneFrame_a classes (see below) which I modified to create a constant force in one axis. essentially all this group of objects does is fall under gravity and spin around due to the two forces acting at a longituidnal offset from the body center creating a couple about the cg.
model Constant_Force
extends Modelica.Mechanics.MultiBody.Interfaces.PartialOneFrame_a;
parameter Real force = 1.0;
equation
frame_a.f = {0.0,0.0,force};
frame_a.t = {0.0,0.0,0.0};
end Constant_Force;
I next wanted see if I could create a very simple aerodynamic force component which I would connect to the end of one the rotating 'arms'. My idea was to follow the example of the Constant_force model above and for my first simple cut generate forces based on the local frame velocities. This where my problem arose - I tried to compute the velocity using der(frame_a.r_0) which I was then going to transform to local frame using resolve2 function but adding the der(...) line caused the model to not work properly - it would 'successfully' simulate (using OpenModelica) but the v11b vector (see below) would be all zeros, so would der(frame_a.r_0) that appeared for plot plotting - not only that, all the other component behaviors were now also just zero constantly - (frame_a.r_0, w_a etc of the body).
model Aerosurf
extends Modelica.Mechanics.MultiBody.Interfaces.PartialOneFrame_a;
import Modelica.Mechanics.MultiBody.Frames;
import Modelica.SIunits;
//Real[3] v11b;
SIunits.Velocity v11b[3];
//initial equation
// v11b={0.0,0.0,0.0};
algorithm
//v11b:=der(frame_a.r_0);
equation
v11b=der(frame_a.r_0);
frame_a.f = {0.0,0.0,0.0};
frame_a.t = {0.0,0.0,0.0};
end Aerosurf;
I tried a number of ways just to simply compute the velocities (you will see from the commented lines) so i could plot to check correct behavior but to no avail. I used algorithm or equation approach - I did acheive some different (but also incorrect behaviours) with the different approaches.
Any tips? I must be missing something fundamental here, it seems the frame component does not inherently carry the velocity vector so I must have to compute it??
The simplest way is to use the Modelica.Mechanics.MultiBody.Sensors.AbsoluteVelocity block from MSL and connect it to your MB frame, then just use the variable of the output connector in your equation.