Matlab app designer error related to an array

I'm developing a GUI to show DC-motor current and speed signals, now I'm experiencing some code difficulties.
When running the code there's an error msg related to a point used in an array which actually runs perfectly in matlab .m code.
Does anyone know how to get rid of this error?
properties (Access = private)
current % Current in mA
speed % Speed in rad/s
time % Time in s
current_temp = [];
speed_temp = [];
time_temp = [];
Data
end

 Accepted Answer

The table appears to be empty (running from m-file)
MQTTSignal = mqttclient('tcp://broker.hivemq.com','Port',1883)
MQTTSignal =
Client with properties: BrokerAddress: "tcp://broker.hivemq.com" Port: 1883 ClientID: "" Timeout: 5 KeepAliveDuration: 60 Subscriptions: [0×3 table] Connected: 1
dataTT = read(MQTTSignal)
dataTT = 0×0 empty timetable
dataTT.Data
Error using .
Unrecognized table variable name 'Data'.

6 Comments

Yes you were right, now I run the DC motor connected to the MQTT broker, and at the beginning it starts plotting data, but later on it stops and send me the same error.
It seems what is inside the while doesn't reconigze the .Data variable.
I don't have experience with the Industrial Communication Toolbox so I'm not sure what the issue is. If the value of MQTTSignal or read(MQTTSignal) changes over time, you can include this snippet below to skip loops where dataTT is empty. However, if dataTT is continually empty, this will enter an infinite loop in which case it would be wise to build in an exit.
while ____
dataTT = read(MQTTSignal);
if isempty(dataTT)
continue
end
str = dataTT.Data;
end
Here are the results
In the video you will see that there's a gap between data probably due to the "pause(3)" command.
Any other suggestion to improve the plotting view?
Changes made to the code based on your suggestions
function StartButtonPushed(app, event)
set(app.StartButton,'Enable','off')
%app.StopButton.Text='Stop'
% Connection to MQTT
MQTTSignal = mqttclient('tcp://broker.hivemq.com','Port',1883);
Topic = "ESP32/data";
Data = subscribe(MQTTSignal,Topic);
pause(3);
%while strcmpi(app.StartButton.Enable,'off')
while strcmpi(app.StartButton.Enable,'off')
dataTT = read(MQTTSignal);
str = (dataTT.Data);
str = strrep(str, '{', ''); % remove opening bracket
str = strrep(str, '}', ''); % remove closing bracket
str = split(str,",");
app.current_temp = str2double(str(:,1));
app.speed_temp = str2double(str(:,2));
app.time_temp = str2double(str(:,3));
app.current = [app.current;app.current_temp];
app.speed = [app.speed;app.speed_temp];
app.time = [app.time;app.time_temp];
plot(app.UIAxes,app.time,app.current);
plot(app.UIAxes2,app.time,app.speed);
if strcmpi(app.StartButton.Enable,'off')
MQTTSignal = mqttclient('tcp://broker.hivemq.com','Port',1883);
Topic = "ESP32/data";
Data = subscribe(MQTTSignal,Topic);
pause(3)
end
end
Thanks for sharing the video!
> Any other suggestion to improve the plotting view?
Yes, the plotting can be more efficient. Instead of creating a new line object every iteration, you can update an existing line object.
% Create line objects
hCurrent = plot(app.UIAxes,nan,nan);
hSpeed = plot(app.UIAxes,nan,nan);
% update line objects within the loop
while strcmpi(...)
...
app.current = [app.current;app.current_temp];
app.speed = [app.speed;app.speed_temp];
app.time = [app.time;app.time_temp];
set(hCurrent,'XData',app.time,'YData',app.current)
set(hSpeed,'XData',app.time,'YData',app.speed)
end
I already test it, and it worked though I still need to run it with this "if sentence" to make current & speed graphs run continuosly.
function StartButtonPushed(app, event)
set(app.StartButton,'Enable','off')
%app.StopButton.Text='Stop'
% Connection to MQTT
MQTTSignal = mqttclient('tcp://broker.hivemq.com','Port',1883);
Topic = "ESP32/data";
Data = subscribe(MQTTSignal,Topic);
pause(3);
%while strcmpi(app.StartButton.Enable,'off')
% Create line objects
hCurrent = plot(app.UIAxes,nan,nan);
hSpeed = plot(app.UIAxes2,nan,nan);
while strcmpi(app.StartButton.Enable,'off')
dataTT = read(MQTTSignal);
str = (dataTT.Data);
str = strrep(str, '{', ''); % remove opening bracket
str = strrep(str, '}', ''); % remove closing bracket
str = split(str,",");
app.current_temp = str2double(str(:,1));
app.speed_temp = str2double(str(:,2));
app.time_temp = str2double(str(:,3));
app.current = [app.current;app.current_temp];
app.speed = [app.speed;app.speed_temp];
app.time = [app.time;app.time_temp];
set(hCurrent,'XData',app.time,'YData',app.current)
set(hSpeed,'XData',app.time,'YData',app.speed)
% plot(app.UIAxes,app.time,app.current);
% plot(app.UIAxes2,app.time,app.speed);
if strcmpi(app.StartButton.Enable,'off')
MQTTSignal = mqttclient('tcp://broker.hivemq.com','Port',1883);
Topic = "ESP32/data";
Data = subscribe(MQTTSignal,Topic);
pause(3)
end
end

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2022b

Community Treasure Hunt

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

Start Hunting!