DateTime stamps in uitable from thingSpeakRead() data

조회 수: 5 (최근 30일)
Mark Kiehl
Mark Kiehl 2020년 5월 19일
댓글: Cris LaPierre 2020년 5월 23일
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};

채택된 답변

Cris LaPierre
Cris LaPierre 2020년 5월 22일
편집: Cris LaPierre 2020년 5월 22일
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
Mark Kiehl
Mark Kiehl 2020년 5월 23일
The 2nd option worked out the best. Thank you so much for your help.
Cris LaPierre
Cris LaPierre 2020년 5월 23일
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);

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Cris LaPierre
Cris LaPierre 2020년 5월 19일
편집: Cris LaPierre 2020년 5월 19일
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
Mark Kiehl
Mark Kiehl 2020년 5월 20일
편집: Mark Kiehl 2020년 5월 20일
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 2020년 5월 20일
I'm not getting an error. What version of MATLAB are you using?

댓글을 달려면 로그인하십시오.


Mark Kiehl
Mark Kiehl 2020년 5월 21일
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;

커뮤니티

더 많은 답변 보기:  ThingSpeak 커뮤니티

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by