DateTime stamps in uitable from thingSpeakRead() data

4 vues (au cours des 30 derniers jours)
Mark Kiehl
Mark Kiehl le 19 Mai 2020
Commenté : Cris LaPierre le 23 Mai 2020
I cannot figure out how to add the date/time stamp data in chnX to the uitable without generating an error.
chnId = #######;
apiReadKey = 'thingspeak-api-read-key';
[data, chnX] = thingSpeakRead(chnId, 'Fields',[1, 2, 3, 4, 5, 6], ...
'NumPoints', 5, ...
'ReadKey', apiReadKey);
% Visualize data with a uitable
f = figure();
uit = uitable(f);
uit.Data = data;
uit.ColumnName = {'A0','A1','A2','A3','A4','A5'};
uit.Position = [5 5 450 150]; %[left bottom width height]
uit.ColumnWidth = {65,65,65,65,65,65};

Réponse acceptée

Cris LaPierre
Cris LaPierre le 22 Mai 2020
Modifié(e) : Cris LaPierre le 22 Mai 2020
My apologies for not catching onto that initially. While uifigure is needed to add datetimes, it is not supported in ThingSpeak. You can find a list of the table properties and their accepted inputs here. From that link, you can see that only numeric, logical, or cell arrays can be passed to the Data property of uitables created with the figure function.
Option 1
You could just display the table without trying to put it into a uitable.
chnId = 1044636;
apiReadKey = 'SJS9JQYUSJSJZYW3';
data = thingSpeakRead(chnId, 'Fields',[1, 2, 3, 4, 5, 6], ...
'NumPoints', 5, ...
'ReadKey', apiReadKey);
% Visualize data
varNames = {'A0','A1','A2','A3','A4','A5'};
Tbl = array2timetable(data,'RowTimes',chnX,'VariableNames',varNames)
Option 2
If you really want it in a table, and
  1. don't want to compress the date to a single numeric representation (datenum), and
  2. also don't want to split the date into parts (datevec), creating a column for each one (Y,M,D,h,m,s)
the answer is to create a cell array. To do this, you need to turn the dates into a character array (datestr), and then turn the combined data into a cell array. The easiest way I could find to do that was with table2cell, which meant I first had to turn the data into a table (array2table).
I noticed there is a maximum width allowed for a uitable, so I had to adjust the formatting of the timestamp to get all the columns to fit.
Here is what I came up with.
%% Read Data %%
[data, chnX] = thingSpeakRead(1044636, ...
'Field', [1, 2, 3, 4, 5, 6], ...
'NumPoints', 5);
%% Convert chnX + data to a cell array %%
Tbl = array2table(data);
Tbl.timestamp = datestr(chnX,'mm/dd/yy HH:MM');
Tbl = movevars(Tbl,"timestamp","Before",1);
tblData = table2cell(Tbl);
%% Visualize Data with a uitable %%
f = figure;
uit = uitable(f);
% uit.Data = [dates(:,1),dates(:,2:3)*[1;.01],dates(:,4:6)*[60;1;1/60],data];
uit.Data = tblData;
uit.ColumnName = {'Timestamp','A0','A1','A2','A3','A4','A5'};
uit.Position = [5 5 590 150]; %[left bottom width height]
uit.ColumnWidth = {130,65,65,65,65,65,65};
  2 commentaires
Mark Kiehl
Mark Kiehl le 23 Mai 2020
The 2nd option worked out the best. Thank you so much for your help.
Cris LaPierre
Cris LaPierre le 23 Mai 2020
It seems char is better than datestr. In that case, replace that line of code with the following.
chnX.Format = 'MM/dd/yy hh:mm';
Tbl.timestamp = char(chnX);

Connectez-vous pour commenter.

Plus de réponses (2)

Cris LaPierre
Cris LaPierre le 19 Mai 2020
Modifié(e) : Cris LaPierre le 19 Mai 2020
Try using uifigure instead of figure. We don't have access to your channel, so here's a quick mockup I did using a public channel. To support multiple data types in the output, I also had to add the "OutputFormat","table" Name-Value pair to thingSpeakRead.
chnId = 841669;
[data, chnX] = thingSpeakRead(chnId,'Fields',[1, 2],...
'NumPoints', 5, ...
"OutputFormat","table");
% Visualize data with a uitable
f = uifigure();
uit = uitable(f);
uit.Data = data;
uit.ColumnName = {'Date','A1','A2'};
uit.Position = [5 5 450 150]; %[left bottom width height]
uit.ColumnWidth = {150,65,65};
  4 commentaires
Mark Kiehl
Mark Kiehl le 20 Mai 2020
Modifié(e) : Mark Kiehl le 20 Mai 2020
chnId = 1044636;
apiReadKey = 'SJS9JQYUSJSJZYW3';
data = thingSpeakRead(chnId, 'Fields',[1, 2, 3, 4, 5, 6], ...
'NumPoints', 5, ...
'ReadKey', apiReadKey);
% Visualize data with a uitable
f = uifigure();
uit = uitable(f);
uit.Data = data;
uit.ColumnName = {'A0','A1','A2','A3','A4','A5'};
uit.Position = [5 5 450 150]; %[left bottom width height]
uit.ColumnWidth = {65,65,65,65,65,65};
Cris LaPierre
Cris LaPierre le 20 Mai 2020
I'm not getting an error. What version of MATLAB are you using?

Connectez-vous pour commenter.


Mark Kiehl
Mark Kiehl le 21 Mai 2020
This is a online Visualization within ThingSpeak. https://thingspeak.com/apps/matlab_visualizations
If I read the data with the thingSpeakRead() function and then create a line plot, the datetime values in chnX are correctly understood by the plot() function, and some automatic formatting is performed. Based on this, the ISO8601 formatted (similar to JSON date/time format) seems to be understoon well by MatLab within that context.
What I want is a table that has the chnX datetime values in the first column of the table, and the numeric data in the remaining columns. If i execute .uit.Data = data; then I get the numeric values without an error. But when I attempt to display the chnX datetime values in a uitable, I experience the error. For example, the code below results in the error:
Error setting property 'Data' of class 'Table':
Data must be a numeric, logical, string, cell, or table array
Error in Table of Particle device ai channel data (line 9)
uit.Data = chnX;
[data, chnX] = thingSpeakRead(chnId, 'Fields',[1, 2, 3, 4, 5, 6], ...
'NumPoints', 5, ...
'ReadKey', apiReadKey);
f = figure();
uit = uitable(f);
uit.Data = chnX;

Communautés

Plus de réponses dans  ThingSpeak Community

Catégories

En savoir plus sur Act on Data dans Help Center et File Exchange

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by