Error using thingsspeak in MATLAB
We have sensors connected on power grid lines that will tell the status wether there is any issue . We are trying to use thingsspeak and we performed some initial working which is shown in attached snap
Relevant code view is:
classdef IoTDataExplorerForThingSpeakUsingMATLAB < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
IoTDataExplorerForThingSpeakUIFigure matlab.ui.Figure
GridLayout matlab.ui.container.GridLayout
LeftPanel matlab.ui.container.Panel
F4CheckBox matlab.ui.control.CheckBox
ReadAPIKeyEditField matlab.ui.control.EditField
ReadAPIKeyEditFieldLabel matlab.ui.control.Label
ChannelIDEditField matlab.ui.control.NumericEditField
ChannelIDEditFieldLabel matlab.ui.control.Label
RightPanel matlab.ui.container.Panel
StatusLabel matlab.ui.control.Label
end
% Properties that correspond to apps with auto-reflow
properties (Access = private)
onePanelWidth = 576;
end
% Copyright 2020 – 2020 The MathWorks, Inc.
properties (Access = private)
legendLabel1
legendLabel2
myChans
end
methods (Access = private)
function displayFields=enumerateSelectedFields(app)
% Create array of the fields for processing
displayFields = [app.F4CheckBox.Value];
end
end
methods (Access = private)
function [query] = buildQueryFromInputs(app)
% buildQueryFromInputs build the querry inputs into a struct.
% q = buildQueryFromInputs(app)
%is a list of inputs needed to
% get the appropriate data from ThingSpeak.
% The data includes the start and stop date, channel ID and API keys.
%
query = struct();
% queryRecent = struct();
% queryOld = struct();
app.StatusLabel.Text = ‘Status’;
query.APIKey = app.ReadAPIKeyEditField.Value;
query.channelID = app.ChannelIDEditField.Value;
%query.startHour = hours(str2double(app.StartHourDropDown.Value));
%query.startMinute = minutes(str2double(app.MinDropDown.Value));
% query.dmult = app.plotDuration.Value;
%query.lmult = app.plotLengthofComparison.Value;
% build the start date with date, minutes, hours and am/PM
% queryRecent.startDate = app.StartDateDatePicker.Value + …
% query.startHour + query.startMinute;
% if app.AMPMSwitch.Value == "PM"
% queryRecent.startDate = queryRecent.startDate + hours(12);
% end
%queryRecent.endDate = queryRecent.startDate + getCompareDuration(app);
%query.fieldsList = {};
%queryDisplayFields = enumerateSelectedFields(app);
% build the fields list for ThingSpeak Read
% for i= 1:8
% if queryDisplayFields(i) > 0
% query.fieldsList = [query.fieldsList, i];
% end
% end
% query.fieldsList = cell2mat(query.fieldsList);
% queryOld.startDate = queryRecent.startDate – getCompareWidth(app);
% queryOld.endDate = queryOld.startDate + getCompareDuration(app);
% end
% function myData = getDataFromQuery(app, query,queryStartEnd)
% getDataFromQueryRecent() get the recent data.
% data = getDataFromQueryRecent(app, queryRecent) is a the
% most recent data. Requires buildQueryFromInputs to get
% startDate, endDate, fieldsList, channelID, and APIKey.
%
% try
% myData = thingSpeakRead(query.channelID, …
% ‘ReadKey’, query.APIKey, …
% ‘DateRange’,[queryStartEnd.startDate queryStartEnd.endDate], …
% ‘Fields’, query.fieldsList, …
% ‘OutputFormat’,’Timetable’);
% catch readingError
% myData = timetable();
% uialert(app.IoTDataExplorerForThingSpeakUIFigure,readingError.identifier,"Data error for that time interval");
% return
% end
% if isempty(myData)
% uialert(app.IoTDataExplorerForThingSpeakUIFigure,"No Data for that time interval","Pick some different data");
% myData = timetable();
% return
% end
% if height(myData) >= 8000
% app.StatusLabel.Text = "Read limit reached. Duration may be shorter than expected";
% end
% Use retime if the user has selected that option
% if app.RetimeDropDown.Value ~= "Raw"
% myData = retime(myData,app.RetimeDropDown.Value,’linear’);
% end
% end
% function visualizeData(app,recentData, oldData, queryInfo)
% visualizeData() display the data.
% visualizeData(app,recentData, oldData) uses a the recentData and oldData to
% plot the data in a tiled layot depending on the number of
% fields selected.
% dataDisplayFields = enumerateSelectedFields(app);
% if sum(dataDisplayFields) > width(oldData)
% uialert(app.IoTDataExplorerForThingSpeakUIFigure,"Not enough Fields selected","Pick a different channel or different fields.");
% end
% Check to make sure the earlier functions produced data.
% if width(recentData) == 0
% return
% end
% if width(oldData) == 0
% return
% end
% t = tiledlayout(app.RightPanel,sum(dataDisplayFields),1,’tilespacing’,’none’);
% Change data to elapsed time
% elapsedRecent = recentData.Timestamps – recentData.Timestamps(1) + …
% queryInfo.startHour + queryInfo.startMinute;
% elapsedOld = oldData.Timestamps-oldData.Timestamps(1) + …
% queryInfo.startHour + queryInfo.startMinute;
% Determine which set is shortest.
% minLength = min(height(oldData),height(recentData));
% myTile = 0;
% for index = 1:8
% if dataDisplayFields(index)>0
% myTile = myTile + 1;
% myAxes = nexttile(t);
% plotVar = recentData.(myTile);
% plot(myAxes,elapsedRecent(1:minLength),plotVar(1:minLength),’-o’,’MarkerSize’,2); %#<ADMTHDINV>
% if app.CompareLengthDropDown.Value ~= minutes(0) % minutes(0) is the setting for don’t show the old data
% hold(myAxes,"on");
% plotVar2=oldData.(myTile);
% plot(myAxes,elapsedOld(1:minLength),plotVar2(1:minLength),’-*’,’MarkerSize’,2);
% end
% title(myAxes,recentData.Properties.VariableNames(myTile));
% if myTile > 1
% set (myAxes,’xtick’,[]);
% legend(myAxes,["Recent" ,"Old"]);
% else
% legend(myAxes,{app.legendLabel1, app.legendLabel2});
% end
% end
% end
% end
% function compareDuration = getCompareDuration(app)
% % Determine the length in time of the data analysis for each
% window from the GUI inputs.
% dmult = app.plotDuration.Value;
% compareDuration = app.DurationDropDown.Value * dmult;
% end
% function legendLabel = getLegendLabel(app,startDate)
% Format the legendLabel based on the start data and duration
% selected.
% dmult = app.plotDuration.Value;
% switch app.DurationDropDown.Value
% case minutes(1)
% legendLabel = string(dmult) + ‘ Minutes on ‘ + string(startDate);
% case hours(1)
% legendLabel = string(dmult) + ‘ Hours on ‘ + string(startDate);
% case hours(24)
% legendLabel = string(dmult) + ‘ Days on ‘ + string(startDate);
% case days(7)
% legendLabel = string(dmult) + ‘ Weeks on ‘ + string(startDate);
% case days(365)
% legendLabel = string(dmult) + ‘ Years on ‘ + string(startDate);
% end
% if app.RetimeDropDown.Value ~= "Raw"
% legendLabel = legendLabel + " " + string(app.RetimeDropDown.Value);
% end
% end
% function compareWidth = getCompareWidth(app)
% Determine the difference in time from the recent data to the
% older data from the GUI inputs.
% lmult = app.plotLengthofComparison.Value;
% compareWidth = app.CompareLengthDropDown.Value * lmult;
% end
% end
% Callbacks that handle component events
methods (Access = private)
% Changes arrangement of the app based on UIFigure width
function updateAppLayout(app, event)
currentFigureWidth = app.IoTDataExplorerForThingSpeakUIFigure.Position(3);
if(currentFigureWidth <= app.onePanelWidth)
% Change to a 2×1 grid
app.GridLayout.RowHeight = {511, 511};
app.GridLayout.ColumnWidth = {‘1x’};
app.RightPanel.Layout.Row = 2;
app.RightPanel.Layout.Column = 1;
else
% Change to a 1×2 grid
app.GridLayout.RowHeight = {‘1x’};
app.GridLayout.ColumnWidth = {241, ‘1x’};
app.RightPanel.Layout.Row = 1;
app.RightPanel.Layout.Column = 2;
end
end
% Value changed function: F4CheckBox
function F4CheckBoxValueChanged(app, event)
value = app.F4CheckBox.value;
end
end
% Value changed function: ChannelIDEditField
function ChannelIDEditFieldValueChanged(app, event)
value = app.ChannelIDEditField.Value;
end
% Value changed function: ReadAPIKeyEditField
function ReadAPIKeyEditFieldValueChanged(app, event)
value = app.ReadAPIKeyEditField.Value;
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create IoTDataExplorerForThingSpeakUIFigure and hide until all components are created
app.IoTDataExplorerForThingSpeakUIFigure = uifigure(‘Visible’, ‘off’);
app.IoTDataExplorerForThingSpeakUIFigure.AutoResizeChildren = ‘off’;
app.IoTDataExplorerForThingSpeakUIFigure.Position = [100 100 710 511];
app.IoTDataExplorerForThingSpeakUIFigure.Name = ‘ThingSpeak Data Explorer’;
app.IoTDataExplorerForThingSpeakUIFigure.SizeChangedFcn = createCallbackFcn(app, @updateAppLayout, true);
% Create GridLayout
app.GridLayout = uigridlayout(app.IoTDataExplorerForThingSpeakUIFigure);
app.GridLayout.ColumnWidth = {241, ‘1x’};
app.GridLayout.RowHeight = {‘1x’};
app.GridLayout.ColumnSpacing = 0;
app.GridLayout.RowSpacing = 0;
app.GridLayout.Padding = [0 0 0 0];
app.GridLayout.Scrollable = ‘on’;
% Create LeftPanel
app.LeftPanel = uipanel(app.GridLayout);
app.LeftPanel.Layout.Row = 1;
app.LeftPanel.Layout.Column = 1;
% Create ChannelIDEditFieldLabel
app.ChannelIDEditFieldLabel = uilabel(app.LeftPanel);
app.ChannelIDEditFieldLabel.HorizontalAlignment = ‘right’;
app.ChannelIDEditFieldLabel.Position = [12 464 66 22];
app.ChannelIDEditFieldLabel.Text = ‘Channel ID’;
% Create ChannelIDEditField
app.ChannelIDEditField = uieditfield(app.LeftPanel, ‘numeric’);
app.ChannelIDEditField.ValueDisplayFormat = ‘%.0f’;
app.ChannelIDEditField.ValueChangedFcn = createCallbackFcn(app, @ChannelIDEditFieldValueChanged, true);
app.ChannelIDEditField.Position = [93 464 55 22];
app.ChannelIDEditField.Value = 543172;
% Create ReadAPIKeyEditFieldLabel
app.ReadAPIKeyEditFieldLabel = uilabel(app.LeftPanel);
app.ReadAPIKeyEditFieldLabel.HorizontalAlignment = ‘right’;
app.ReadAPIKeyEditFieldLabel.Position = [13 434 74 22];
app.ReadAPIKeyEditFieldLabel.Text = ‘ReadAPIKey’;
% Create ReadAPIKeyEditField
app.ReadAPIKeyEditField = uieditfield(app.LeftPanel, ‘text’);
app.ReadAPIKeyEditField.ValueChangedFcn = createCallbackFcn(app, @ReadAPIKeyEditFieldValueChanged, true);
app.ReadAPIKeyEditField.Position = [102 434 100 22];
app.ReadAPIKeyEditField.Value = ‘0BXYOJW16B7R6DMM’;
% Create F4CheckBox
app.F4CheckBox = uicheckbox(app.LeftPanel);
app.F4CheckBox.ValueChangedFcn = createCallbackFcn(app, @F4CheckBoxValueChanged, true);
app.F4CheckBox.Text = ‘F4’;
app.F4CheckBox.Position = [14 178 36 22];
app.F4CheckBox.Value = true;
% Create RightPanel
app.RightPanel = uipanel(app.GridLayout);
app.RightPanel.Layout.Row = 1;
app.RightPanel.Layout.Column = 2;
% Create StatusLabel
app.StatusLabel = uilabel(app.RightPanel);
app.StatusLabel.Position = [2 1 449 22];
app.StatusLabel.Text = ‘Status’;
% Show the figure after all components are created
app.IoTDataExplorerForThingSpeakUIFigure.Visible = ‘on’;
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = IoTDataExplorerForThingSpeakUsingMATLAB
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.IoTDataExplorerForThingSpeakUIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.IoTDataExplorerForThingSpeakUIFigure)
end
end
end
But when we try to run ,we get error:
Unrecognized function or variable ‘updateAppLayout’.
Error in matlab.apps.AppBase>@(source,event)executeCallback(ams,app,callback,requiresEventData,event) (line 62)
newCallback = @(source, event)executeCallback(ams, …
Error using IoTDataExplorerForThingSpeakUsingMATLAB/createComponents (line 324)
Error while evaluating Figure SizeChangedFcn.
Warning: The following error was caught while executing ‘onCleanup’ class destructor:
Brace indexing is not supported for variables of this type.
Error in cell2mat (line 36)
if isnumeric(c{1}) || ischar(c{1}) || islogical(c{1}) || isstruct(c{1})
Error in onCleanup/delete (line 25)
obj.task();
Warning: The following error was caught while executing ‘onCleanup’ class destructor:
Brace indexing is not supported for variables of this type.
Error in cell2mat (line 36)
if isnumeric(c{1}) || ischar(c{1}) || islogical(c{1}) || isstruct(c{1})
Error in onCleanup/delete (line 25)
obj.task();
Error in onCleanup/delete (line 25)
obj.task();
Error inmatlab.apps.AppBase>@(source,event)executeCallback(ams,app,callback,requiresEventData,event)
(line 62)
newCallback = @(source, event)executeCallback(ams, … > In onCleanup/delete (line 25)
In matlab.apps.AppBase>@(source,event)executeCallback(ams,app,callback,requiresEventData,event) (line 62)
In matlab.ui.internal.controller/FigureController/handleClientEvent (line 832)
In matlab.ui.internal.controller/FigureController/handleEvent (line 667)
In matlab.ui.internal.controller.WebCanvasContainerController>@(varargin)this.handleEvent(varargin{:}) (line 45)
Unrecognized function or variable ‘updateAppLayout’.
Error in matlab.apps.AppBase>@(source,event)executeCallback(ams,app,callback,requiresEventData,event) (line 62)
newCallback = @(source, event)executeCallback(ams, …
Error using matlab.ui.internal.controller.FigureController/handleClientEvent (line 832)
Error while evaluating Figure SizeChangedFcn.
>>We have sensors connected on power grid lines that will tell the status wether there is any issue . We are trying to use thingsspeak and we performed some initial working which is shown in attached snap
Relevant code view is:
classdef IoTDataExplorerForThingSpeakUsingMATLAB < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
IoTDataExplorerForThingSpeakUIFigure matlab.ui.Figure
GridLayout matlab.ui.container.GridLayout
LeftPanel matlab.ui.container.Panel
F4CheckBox matlab.ui.control.CheckBox
ReadAPIKeyEditField matlab.ui.control.EditField
ReadAPIKeyEditFieldLabel matlab.ui.control.Label
ChannelIDEditField matlab.ui.control.NumericEditField
ChannelIDEditFieldLabel matlab.ui.control.Label
RightPanel matlab.ui.container.Panel
StatusLabel matlab.ui.control.Label
end
% Properties that correspond to apps with auto-reflow
properties (Access = private)
onePanelWidth = 576;
end
% Copyright 2020 – 2020 The MathWorks, Inc.
properties (Access = private)
legendLabel1
legendLabel2
myChans
end
methods (Access = private)
function displayFields=enumerateSelectedFields(app)
% Create array of the fields for processing
displayFields = [app.F4CheckBox.Value];
end
end
methods (Access = private)
function [query] = buildQueryFromInputs(app)
% buildQueryFromInputs build the querry inputs into a struct.
% q = buildQueryFromInputs(app)
%is a list of inputs needed to
% get the appropriate data from ThingSpeak.
% The data includes the start and stop date, channel ID and API keys.
%
query = struct();
% queryRecent = struct();
% queryOld = struct();
app.StatusLabel.Text = ‘Status’;
query.APIKey = app.ReadAPIKeyEditField.Value;
query.channelID = app.ChannelIDEditField.Value;
%query.startHour = hours(str2double(app.StartHourDropDown.Value));
%query.startMinute = minutes(str2double(app.MinDropDown.Value));
% query.dmult = app.plotDuration.Value;
%query.lmult = app.plotLengthofComparison.Value;
% build the start date with date, minutes, hours and am/PM
% queryRecent.startDate = app.StartDateDatePicker.Value + …
% query.startHour + query.startMinute;
% if app.AMPMSwitch.Value == "PM"
% queryRecent.startDate = queryRecent.startDate + hours(12);
% end
%queryRecent.endDate = queryRecent.startDate + getCompareDuration(app);
%query.fieldsList = {};
%queryDisplayFields = enumerateSelectedFields(app);
% build the fields list for ThingSpeak Read
% for i= 1:8
% if queryDisplayFields(i) > 0
% query.fieldsList = [query.fieldsList, i];
% end
% end
% query.fieldsList = cell2mat(query.fieldsList);
% queryOld.startDate = queryRecent.startDate – getCompareWidth(app);
% queryOld.endDate = queryOld.startDate + getCompareDuration(app);
% end
% function myData = getDataFromQuery(app, query,queryStartEnd)
% getDataFromQueryRecent() get the recent data.
% data = getDataFromQueryRecent(app, queryRecent) is a the
% most recent data. Requires buildQueryFromInputs to get
% startDate, endDate, fieldsList, channelID, and APIKey.
%
% try
% myData = thingSpeakRead(query.channelID, …
% ‘ReadKey’, query.APIKey, …
% ‘DateRange’,[queryStartEnd.startDate queryStartEnd.endDate], …
% ‘Fields’, query.fieldsList, …
% ‘OutputFormat’,’Timetable’);
% catch readingError
% myData = timetable();
% uialert(app.IoTDataExplorerForThingSpeakUIFigure,readingError.identifier,"Data error for that time interval");
% return
% end
% if isempty(myData)
% uialert(app.IoTDataExplorerForThingSpeakUIFigure,"No Data for that time interval","Pick some different data");
% myData = timetable();
% return
% end
% if height(myData) >= 8000
% app.StatusLabel.Text = "Read limit reached. Duration may be shorter than expected";
% end
% Use retime if the user has selected that option
% if app.RetimeDropDown.Value ~= "Raw"
% myData = retime(myData,app.RetimeDropDown.Value,’linear’);
% end
% end
% function visualizeData(app,recentData, oldData, queryInfo)
% visualizeData() display the data.
% visualizeData(app,recentData, oldData) uses a the recentData and oldData to
% plot the data in a tiled layot depending on the number of
% fields selected.
% dataDisplayFields = enumerateSelectedFields(app);
% if sum(dataDisplayFields) > width(oldData)
% uialert(app.IoTDataExplorerForThingSpeakUIFigure,"Not enough Fields selected","Pick a different channel or different fields.");
% end
% Check to make sure the earlier functions produced data.
% if width(recentData) == 0
% return
% end
% if width(oldData) == 0
% return
% end
% t = tiledlayout(app.RightPanel,sum(dataDisplayFields),1,’tilespacing’,’none’);
% Change data to elapsed time
% elapsedRecent = recentData.Timestamps – recentData.Timestamps(1) + …
% queryInfo.startHour + queryInfo.startMinute;
% elapsedOld = oldData.Timestamps-oldData.Timestamps(1) + …
% queryInfo.startHour + queryInfo.startMinute;
% Determine which set is shortest.
% minLength = min(height(oldData),height(recentData));
% myTile = 0;
% for index = 1:8
% if dataDisplayFields(index)>0
% myTile = myTile + 1;
% myAxes = nexttile(t);
% plotVar = recentData.(myTile);
% plot(myAxes,elapsedRecent(1:minLength),plotVar(1:minLength),’-o’,’MarkerSize’,2); %#<ADMTHDINV>
% if app.CompareLengthDropDown.Value ~= minutes(0) % minutes(0) is the setting for don’t show the old data
% hold(myAxes,"on");
% plotVar2=oldData.(myTile);
% plot(myAxes,elapsedOld(1:minLength),plotVar2(1:minLength),’-*’,’MarkerSize’,2);
% end
% title(myAxes,recentData.Properties.VariableNames(myTile));
% if myTile > 1
% set (myAxes,’xtick’,[]);
% legend(myAxes,["Recent" ,"Old"]);
% else
% legend(myAxes,{app.legendLabel1, app.legendLabel2});
% end
% end
% end
% end
% function compareDuration = getCompareDuration(app)
% % Determine the length in time of the data analysis for each
% window from the GUI inputs.
% dmult = app.plotDuration.Value;
% compareDuration = app.DurationDropDown.Value * dmult;
% end
% function legendLabel = getLegendLabel(app,startDate)
% Format the legendLabel based on the start data and duration
% selected.
% dmult = app.plotDuration.Value;
% switch app.DurationDropDown.Value
% case minutes(1)
% legendLabel = string(dmult) + ‘ Minutes on ‘ + string(startDate);
% case hours(1)
% legendLabel = string(dmult) + ‘ Hours on ‘ + string(startDate);
% case hours(24)
% legendLabel = string(dmult) + ‘ Days on ‘ + string(startDate);
% case days(7)
% legendLabel = string(dmult) + ‘ Weeks on ‘ + string(startDate);
% case days(365)
% legendLabel = string(dmult) + ‘ Years on ‘ + string(startDate);
% end
% if app.RetimeDropDown.Value ~= "Raw"
% legendLabel = legendLabel + " " + string(app.RetimeDropDown.Value);
% end
% end
% function compareWidth = getCompareWidth(app)
% Determine the difference in time from the recent data to the
% older data from the GUI inputs.
% lmult = app.plotLengthofComparison.Value;
% compareWidth = app.CompareLengthDropDown.Value * lmult;
% end
% end
% Callbacks that handle component events
methods (Access = private)
% Changes arrangement of the app based on UIFigure width
function updateAppLayout(app, event)
currentFigureWidth = app.IoTDataExplorerForThingSpeakUIFigure.Position(3);
if(currentFigureWidth <= app.onePanelWidth)
% Change to a 2×1 grid
app.GridLayout.RowHeight = {511, 511};
app.GridLayout.ColumnWidth = {‘1x’};
app.RightPanel.Layout.Row = 2;
app.RightPanel.Layout.Column = 1;
else
% Change to a 1×2 grid
app.GridLayout.RowHeight = {‘1x’};
app.GridLayout.ColumnWidth = {241, ‘1x’};
app.RightPanel.Layout.Row = 1;
app.RightPanel.Layout.Column = 2;
end
end
% Value changed function: F4CheckBox
function F4CheckBoxValueChanged(app, event)
value = app.F4CheckBox.value;
end
end
% Value changed function: ChannelIDEditField
function ChannelIDEditFieldValueChanged(app, event)
value = app.ChannelIDEditField.Value;
end
% Value changed function: ReadAPIKeyEditField
function ReadAPIKeyEditFieldValueChanged(app, event)
value = app.ReadAPIKeyEditField.Value;
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create IoTDataExplorerForThingSpeakUIFigure and hide until all components are created
app.IoTDataExplorerForThingSpeakUIFigure = uifigure(‘Visible’, ‘off’);
app.IoTDataExplorerForThingSpeakUIFigure.AutoResizeChildren = ‘off’;
app.IoTDataExplorerForThingSpeakUIFigure.Position = [100 100 710 511];
app.IoTDataExplorerForThingSpeakUIFigure.Name = ‘ThingSpeak Data Explorer’;
app.IoTDataExplorerForThingSpeakUIFigure.SizeChangedFcn = createCallbackFcn(app, @updateAppLayout, true);
% Create GridLayout
app.GridLayout = uigridlayout(app.IoTDataExplorerForThingSpeakUIFigure);
app.GridLayout.ColumnWidth = {241, ‘1x’};
app.GridLayout.RowHeight = {‘1x’};
app.GridLayout.ColumnSpacing = 0;
app.GridLayout.RowSpacing = 0;
app.GridLayout.Padding = [0 0 0 0];
app.GridLayout.Scrollable = ‘on’;
% Create LeftPanel
app.LeftPanel = uipanel(app.GridLayout);
app.LeftPanel.Layout.Row = 1;
app.LeftPanel.Layout.Column = 1;
% Create ChannelIDEditFieldLabel
app.ChannelIDEditFieldLabel = uilabel(app.LeftPanel);
app.ChannelIDEditFieldLabel.HorizontalAlignment = ‘right’;
app.ChannelIDEditFieldLabel.Position = [12 464 66 22];
app.ChannelIDEditFieldLabel.Text = ‘Channel ID’;
% Create ChannelIDEditField
app.ChannelIDEditField = uieditfield(app.LeftPanel, ‘numeric’);
app.ChannelIDEditField.ValueDisplayFormat = ‘%.0f’;
app.ChannelIDEditField.ValueChangedFcn = createCallbackFcn(app, @ChannelIDEditFieldValueChanged, true);
app.ChannelIDEditField.Position = [93 464 55 22];
app.ChannelIDEditField.Value = 543172;
% Create ReadAPIKeyEditFieldLabel
app.ReadAPIKeyEditFieldLabel = uilabel(app.LeftPanel);
app.ReadAPIKeyEditFieldLabel.HorizontalAlignment = ‘right’;
app.ReadAPIKeyEditFieldLabel.Position = [13 434 74 22];
app.ReadAPIKeyEditFieldLabel.Text = ‘ReadAPIKey’;
% Create ReadAPIKeyEditField
app.ReadAPIKeyEditField = uieditfield(app.LeftPanel, ‘text’);
app.ReadAPIKeyEditField.ValueChangedFcn = createCallbackFcn(app, @ReadAPIKeyEditFieldValueChanged, true);
app.ReadAPIKeyEditField.Position = [102 434 100 22];
app.ReadAPIKeyEditField.Value = ‘0BXYOJW16B7R6DMM’;
% Create F4CheckBox
app.F4CheckBox = uicheckbox(app.LeftPanel);
app.F4CheckBox.ValueChangedFcn = createCallbackFcn(app, @F4CheckBoxValueChanged, true);
app.F4CheckBox.Text = ‘F4’;
app.F4CheckBox.Position = [14 178 36 22];
app.F4CheckBox.Value = true;
% Create RightPanel
app.RightPanel = uipanel(app.GridLayout);
app.RightPanel.Layout.Row = 1;
app.RightPanel.Layout.Column = 2;
% Create StatusLabel
app.StatusLabel = uilabel(app.RightPanel);
app.StatusLabel.Position = [2 1 449 22];
app.StatusLabel.Text = ‘Status’;
% Show the figure after all components are created
app.IoTDataExplorerForThingSpeakUIFigure.Visible = ‘on’;
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = IoTDataExplorerForThingSpeakUsingMATLAB
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.IoTDataExplorerForThingSpeakUIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.IoTDataExplorerForThingSpeakUIFigure)
end
end
end
But when we try to run ,we get error:
Unrecognized function or variable ‘updateAppLayout’.
Error in matlab.apps.AppBase>@(source,event)executeCallback(ams,app,callback,requiresEventData,event) (line 62)
newCallback = @(source, event)executeCallback(ams, …
Error using IoTDataExplorerForThingSpeakUsingMATLAB/createComponents (line 324)
Error while evaluating Figure SizeChangedFcn.
Warning: The following error was caught while executing ‘onCleanup’ class destructor:
Brace indexing is not supported for variables of this type.
Error in cell2mat (line 36)
if isnumeric(c{1}) || ischar(c{1}) || islogical(c{1}) || isstruct(c{1})
Error in onCleanup/delete (line 25)
obj.task();
Warning: The following error was caught while executing ‘onCleanup’ class destructor:
Brace indexing is not supported for variables of this type.
Error in cell2mat (line 36)
if isnumeric(c{1}) || ischar(c{1}) || islogical(c{1}) || isstruct(c{1})
Error in onCleanup/delete (line 25)
obj.task();
Error in onCleanup/delete (line 25)
obj.task();
Error inmatlab.apps.AppBase>@(source,event)executeCallback(ams,app,callback,requiresEventData,event)
(line 62)
newCallback = @(source, event)executeCallback(ams, … > In onCleanup/delete (line 25)
In matlab.apps.AppBase>@(source,event)executeCallback(ams,app,callback,requiresEventData,event) (line 62)
In matlab.ui.internal.controller/FigureController/handleClientEvent (line 832)
In matlab.ui.internal.controller/FigureController/handleEvent (line 667)
In matlab.ui.internal.controller.WebCanvasContainerController>@(varargin)this.handleEvent(varargin{:}) (line 45)
Unrecognized function or variable ‘updateAppLayout’.
Error in matlab.apps.AppBase>@(source,event)executeCallback(ams,app,callback,requiresEventData,event) (line 62)
newCallback = @(source, event)executeCallback(ams, …
Error using matlab.ui.internal.controller.FigureController/handleClientEvent (line 832)
Error while evaluating Figure SizeChangedFcn.
>> We have sensors connected on power grid lines that will tell the status wether there is any issue . We are trying to use thingsspeak and we performed some initial working which is shown in attached snap
Relevant code view is:
classdef IoTDataExplorerForThingSpeakUsingMATLAB < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
IoTDataExplorerForThingSpeakUIFigure matlab.ui.Figure
GridLayout matlab.ui.container.GridLayout
LeftPanel matlab.ui.container.Panel
F4CheckBox matlab.ui.control.CheckBox
ReadAPIKeyEditField matlab.ui.control.EditField
ReadAPIKeyEditFieldLabel matlab.ui.control.Label
ChannelIDEditField matlab.ui.control.NumericEditField
ChannelIDEditFieldLabel matlab.ui.control.Label
RightPanel matlab.ui.container.Panel
StatusLabel matlab.ui.control.Label
end
% Properties that correspond to apps with auto-reflow
properties (Access = private)
onePanelWidth = 576;
end
% Copyright 2020 – 2020 The MathWorks, Inc.
properties (Access = private)
legendLabel1
legendLabel2
myChans
end
methods (Access = private)
function displayFields=enumerateSelectedFields(app)
% Create array of the fields for processing
displayFields = [app.F4CheckBox.Value];
end
end
methods (Access = private)
function [query] = buildQueryFromInputs(app)
% buildQueryFromInputs build the querry inputs into a struct.
% q = buildQueryFromInputs(app)
%is a list of inputs needed to
% get the appropriate data from ThingSpeak.
% The data includes the start and stop date, channel ID and API keys.
%
query = struct();
% queryRecent = struct();
% queryOld = struct();
app.StatusLabel.Text = ‘Status’;
query.APIKey = app.ReadAPIKeyEditField.Value;
query.channelID = app.ChannelIDEditField.Value;
%query.startHour = hours(str2double(app.StartHourDropDown.Value));
%query.startMinute = minutes(str2double(app.MinDropDown.Value));
% query.dmult = app.plotDuration.Value;
%query.lmult = app.plotLengthofComparison.Value;
% build the start date with date, minutes, hours and am/PM
% queryRecent.startDate = app.StartDateDatePicker.Value + …
% query.startHour + query.startMinute;
% if app.AMPMSwitch.Value == "PM"
% queryRecent.startDate = queryRecent.startDate + hours(12);
% end
%queryRecent.endDate = queryRecent.startDate + getCompareDuration(app);
%query.fieldsList = {};
%queryDisplayFields = enumerateSelectedFields(app);
% build the fields list for ThingSpeak Read
% for i= 1:8
% if queryDisplayFields(i) > 0
% query.fieldsList = [query.fieldsList, i];
% end
% end
% query.fieldsList = cell2mat(query.fieldsList);
% queryOld.startDate = queryRecent.startDate – getCompareWidth(app);
% queryOld.endDate = queryOld.startDate + getCompareDuration(app);
% end
% function myData = getDataFromQuery(app, query,queryStartEnd)
% getDataFromQueryRecent() get the recent data.
% data = getDataFromQueryRecent(app, queryRecent) is a the
% most recent data. Requires buildQueryFromInputs to get
% startDate, endDate, fieldsList, channelID, and APIKey.
%
% try
% myData = thingSpeakRead(query.channelID, …
% ‘ReadKey’, query.APIKey, …
% ‘DateRange’,[queryStartEnd.startDate queryStartEnd.endDate], …
% ‘Fields’, query.fieldsList, …
% ‘OutputFormat’,’Timetable’);
% catch readingError
% myData = timetable();
% uialert(app.IoTDataExplorerForThingSpeakUIFigure,readingError.identifier,"Data error for that time interval");
% return
% end
% if isempty(myData)
% uialert(app.IoTDataExplorerForThingSpeakUIFigure,"No Data for that time interval","Pick some different data");
% myData = timetable();
% return
% end
% if height(myData) >= 8000
% app.StatusLabel.Text = "Read limit reached. Duration may be shorter than expected";
% end
% Use retime if the user has selected that option
% if app.RetimeDropDown.Value ~= "Raw"
% myData = retime(myData,app.RetimeDropDown.Value,’linear’);
% end
% end
% function visualizeData(app,recentData, oldData, queryInfo)
% visualizeData() display the data.
% visualizeData(app,recentData, oldData) uses a the recentData and oldData to
% plot the data in a tiled layot depending on the number of
% fields selected.
% dataDisplayFields = enumerateSelectedFields(app);
% if sum(dataDisplayFields) > width(oldData)
% uialert(app.IoTDataExplorerForThingSpeakUIFigure,"Not enough Fields selected","Pick a different channel or different fields.");
% end
% Check to make sure the earlier functions produced data.
% if width(recentData) == 0
% return
% end
% if width(oldData) == 0
% return
% end
% t = tiledlayout(app.RightPanel,sum(dataDisplayFields),1,’tilespacing’,’none’);
% Change data to elapsed time
% elapsedRecent = recentData.Timestamps – recentData.Timestamps(1) + …
% queryInfo.startHour + queryInfo.startMinute;
% elapsedOld = oldData.Timestamps-oldData.Timestamps(1) + …
% queryInfo.startHour + queryInfo.startMinute;
% Determine which set is shortest.
% minLength = min(height(oldData),height(recentData));
% myTile = 0;
% for index = 1:8
% if dataDisplayFields(index)>0
% myTile = myTile + 1;
% myAxes = nexttile(t);
% plotVar = recentData.(myTile);
% plot(myAxes,elapsedRecent(1:minLength),plotVar(1:minLength),’-o’,’MarkerSize’,2); %#<ADMTHDINV>
% if app.CompareLengthDropDown.Value ~= minutes(0) % minutes(0) is the setting for don’t show the old data
% hold(myAxes,"on");
% plotVar2=oldData.(myTile);
% plot(myAxes,elapsedOld(1:minLength),plotVar2(1:minLength),’-*’,’MarkerSize’,2);
% end
% title(myAxes,recentData.Properties.VariableNames(myTile));
% if myTile > 1
% set (myAxes,’xtick’,[]);
% legend(myAxes,["Recent" ,"Old"]);
% else
% legend(myAxes,{app.legendLabel1, app.legendLabel2});
% end
% end
% end
% end
% function compareDuration = getCompareDuration(app)
% % Determine the length in time of the data analysis for each
% window from the GUI inputs.
% dmult = app.plotDuration.Value;
% compareDuration = app.DurationDropDown.Value * dmult;
% end
% function legendLabel = getLegendLabel(app,startDate)
% Format the legendLabel based on the start data and duration
% selected.
% dmult = app.plotDuration.Value;
% switch app.DurationDropDown.Value
% case minutes(1)
% legendLabel = string(dmult) + ‘ Minutes on ‘ + string(startDate);
% case hours(1)
% legendLabel = string(dmult) + ‘ Hours on ‘ + string(startDate);
% case hours(24)
% legendLabel = string(dmult) + ‘ Days on ‘ + string(startDate);
% case days(7)
% legendLabel = string(dmult) + ‘ Weeks on ‘ + string(startDate);
% case days(365)
% legendLabel = string(dmult) + ‘ Years on ‘ + string(startDate);
% end
% if app.RetimeDropDown.Value ~= "Raw"
% legendLabel = legendLabel + " " + string(app.RetimeDropDown.Value);
% end
% end
% function compareWidth = getCompareWidth(app)
% Determine the difference in time from the recent data to the
% older data from the GUI inputs.
% lmult = app.plotLengthofComparison.Value;
% compareWidth = app.CompareLengthDropDown.Value * lmult;
% end
% end
% Callbacks that handle component events
methods (Access = private)
% Changes arrangement of the app based on UIFigure width
function updateAppLayout(app, event)
currentFigureWidth = app.IoTDataExplorerForThingSpeakUIFigure.Position(3);
if(currentFigureWidth <= app.onePanelWidth)
% Change to a 2×1 grid
app.GridLayout.RowHeight = {511, 511};
app.GridLayout.ColumnWidth = {‘1x’};
app.RightPanel.Layout.Row = 2;
app.RightPanel.Layout.Column = 1;
else
% Change to a 1×2 grid
app.GridLayout.RowHeight = {‘1x’};
app.GridLayout.ColumnWidth = {241, ‘1x’};
app.RightPanel.Layout.Row = 1;
app.RightPanel.Layout.Column = 2;
end
end
% Value changed function: F4CheckBox
function F4CheckBoxValueChanged(app, event)
value = app.F4CheckBox.value;
end
end
% Value changed function: ChannelIDEditField
function ChannelIDEditFieldValueChanged(app, event)
value = app.ChannelIDEditField.Value;
end
% Value changed function: ReadAPIKeyEditField
function ReadAPIKeyEditFieldValueChanged(app, event)
value = app.ReadAPIKeyEditField.Value;
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create IoTDataExplorerForThingSpeakUIFigure and hide until all components are created
app.IoTDataExplorerForThingSpeakUIFigure = uifigure(‘Visible’, ‘off’);
app.IoTDataExplorerForThingSpeakUIFigure.AutoResizeChildren = ‘off’;
app.IoTDataExplorerForThingSpeakUIFigure.Position = [100 100 710 511];
app.IoTDataExplorerForThingSpeakUIFigure.Name = ‘ThingSpeak Data Explorer’;
app.IoTDataExplorerForThingSpeakUIFigure.SizeChangedFcn = createCallbackFcn(app, @updateAppLayout, true);
% Create GridLayout
app.GridLayout = uigridlayout(app.IoTDataExplorerForThingSpeakUIFigure);
app.GridLayout.ColumnWidth = {241, ‘1x’};
app.GridLayout.RowHeight = {‘1x’};
app.GridLayout.ColumnSpacing = 0;
app.GridLayout.RowSpacing = 0;
app.GridLayout.Padding = [0 0 0 0];
app.GridLayout.Scrollable = ‘on’;
% Create LeftPanel
app.LeftPanel = uipanel(app.GridLayout);
app.LeftPanel.Layout.Row = 1;
app.LeftPanel.Layout.Column = 1;
% Create ChannelIDEditFieldLabel
app.ChannelIDEditFieldLabel = uilabel(app.LeftPanel);
app.ChannelIDEditFieldLabel.HorizontalAlignment = ‘right’;
app.ChannelIDEditFieldLabel.Position = [12 464 66 22];
app.ChannelIDEditFieldLabel.Text = ‘Channel ID’;
% Create ChannelIDEditField
app.ChannelIDEditField = uieditfield(app.LeftPanel, ‘numeric’);
app.ChannelIDEditField.ValueDisplayFormat = ‘%.0f’;
app.ChannelIDEditField.ValueChangedFcn = createCallbackFcn(app, @ChannelIDEditFieldValueChanged, true);
app.ChannelIDEditField.Position = [93 464 55 22];
app.ChannelIDEditField.Value = 543172;
% Create ReadAPIKeyEditFieldLabel
app.ReadAPIKeyEditFieldLabel = uilabel(app.LeftPanel);
app.ReadAPIKeyEditFieldLabel.HorizontalAlignment = ‘right’;
app.ReadAPIKeyEditFieldLabel.Position = [13 434 74 22];
app.ReadAPIKeyEditFieldLabel.Text = ‘ReadAPIKey’;
% Create ReadAPIKeyEditField
app.ReadAPIKeyEditField = uieditfield(app.LeftPanel, ‘text’);
app.ReadAPIKeyEditField.ValueChangedFcn = createCallbackFcn(app, @ReadAPIKeyEditFieldValueChanged, true);
app.ReadAPIKeyEditField.Position = [102 434 100 22];
app.ReadAPIKeyEditField.Value = ‘0BXYOJW16B7R6DMM’;
% Create F4CheckBox
app.F4CheckBox = uicheckbox(app.LeftPanel);
app.F4CheckBox.ValueChangedFcn = createCallbackFcn(app, @F4CheckBoxValueChanged, true);
app.F4CheckBox.Text = ‘F4’;
app.F4CheckBox.Position = [14 178 36 22];
app.F4CheckBox.Value = true;
% Create RightPanel
app.RightPanel = uipanel(app.GridLayout);
app.RightPanel.Layout.Row = 1;
app.RightPanel.Layout.Column = 2;
% Create StatusLabel
app.StatusLabel = uilabel(app.RightPanel);
app.StatusLabel.Position = [2 1 449 22];
app.StatusLabel.Text = ‘Status’;
% Show the figure after all components are created
app.IoTDataExplorerForThingSpeakUIFigure.Visible = ‘on’;
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = IoTDataExplorerForThingSpeakUsingMATLAB
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.IoTDataExplorerForThingSpeakUIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.IoTDataExplorerForThingSpeakUIFigure)
end
end
end
But when we try to run ,we get error:
Unrecognized function or variable ‘updateAppLayout’.
Error in matlab.apps.AppBase>@(source,event)executeCallback(ams,app,callback,requiresEventData,event) (line 62)
newCallback = @(source, event)executeCallback(ams, …
Error using IoTDataExplorerForThingSpeakUsingMATLAB/createComponents (line 324)
Error while evaluating Figure SizeChangedFcn.
Warning: The following error was caught while executing ‘onCleanup’ class destructor:
Brace indexing is not supported for variables of this type.
Error in cell2mat (line 36)
if isnumeric(c{1}) || ischar(c{1}) || islogical(c{1}) || isstruct(c{1})
Error in onCleanup/delete (line 25)
obj.task();
Warning: The following error was caught while executing ‘onCleanup’ class destructor:
Brace indexing is not supported for variables of this type.
Error in cell2mat (line 36)
if isnumeric(c{1}) || ischar(c{1}) || islogical(c{1}) || isstruct(c{1})
Error in onCleanup/delete (line 25)
obj.task();
Error in onCleanup/delete (line 25)
obj.task();
Error inmatlab.apps.AppBase>@(source,event)executeCallback(ams,app,callback,requiresEventData,event)
(line 62)
newCallback = @(source, event)executeCallback(ams, … > In onCleanup/delete (line 25)
In matlab.apps.AppBase>@(source,event)executeCallback(ams,app,callback,requiresEventData,event) (line 62)
In matlab.ui.internal.controller/FigureController/handleClientEvent (line 832)
In matlab.ui.internal.controller/FigureController/handleEvent (line 667)
In matlab.ui.internal.controller.WebCanvasContainerController>@(varargin)this.handleEvent(varargin{:}) (line 45)
Unrecognized function or variable ‘updateAppLayout’.
Error in matlab.apps.AppBase>@(source,event)executeCallback(ams,app,callback,requiresEventData,event) (line 62)
newCallback = @(source, event)executeCallback(ams, …
Error using matlab.ui.internal.controller.FigureController/handleClientEvent (line 832)
Error while evaluating Figure SizeChangedFcn.
>> thingsspeak, iot MATLAB Answers — New Questions