Clear Filters
Clear Filters

Empfangene Daten von Workspace in AppDesigner schieben und plotten

1 view (last 30 days)
Hallo,
Ich möchte Daten die ich von einem Arduino empfange in Live-Grafiken umwandeln (in AppDesigner).
Der Arduino hat eine Bluetooth Verbindung zum Computer.
Mein Plan war die empfangenen Daten in einem Array im Workspace nur mit Spalten zu wandeln, dass ich weis die erste Zeile ist Temperatur, die zweite Luftdruck usw.. Dann die jeweiligen Zeilen zu Daten zu einer Variable hinzufügen und dann im AppDesigner plotten. Das ganze in einer while-Schleife, sodass es eine Live Grafik ist.
Jedoch kenne ich nicht so viele Codes und weis nicht genau wie ich das realisieren kann.
Über eine Hilfe würde ich mich sehr freuen.
Grüße Eric

Answers (1)

Nivedita
Nivedita on 27 May 2024
Hallo Eric,
Ich verstehe, dass Sie eine Live-Datendarstellung in MATLAB App Designer mithilfe von Daten erreichen möchten, die von einem Arduino über Bluetooth empfangen wurden. Sie können diese Schritte ausführen. Die Schritte umfassen das Lesen von Daten aus der Bluetooth-Verbindung, deren Analyse und die anschließende Aktualisierung der Diagramme in Echtzeit.
Ich werde die Schritte auf Englisch beantworten.
You can first refer to the following link for getting started with MATLAB App Designer: https://www.mathworks.com/help/matlab/app-designer.html
1. Ensure your Arduino is programmed to send data over Bluetooth in a consistent format, e.g., comma-separated values (CSV) like "temperature,pressure\n". Before you jump into MATLAB App Designer, test the Bluetooth connection in MATLAB in the following manner:
% Replace 'name' with the name of your Bluetooth device and
% 'channel' with the channel your device uses (often 1)
bt = bluetooth(name, channel);
You can refer to the following documentation link for the usage of the "bluetooth" function: https://www.mathworks.com/help/matlab/ref/bluetooth.html
2. For plotting temperature and pressure, add two UI Axes components to your app by dragging them from the left side pane. You can name them TemperatureAxes and PressureAxes.
3. In the Code View, add properties to your app for the Bluetooth connection and for storing data:
properties (Access = private)
BluetoothDevice
Timer % Timer for updating plots
end
4. Just below the properties, implement a function (updatePlot) to read data from the Bluetooth device, parse it, and update the plots:
function updatePlot(app)
% Check if data is available
if app.BluetoothDevice.NumBytesAvailable > 0
% Read data from Bluetooth device
dataStr = readline(app.BluetoothDevice);
% Parse the CSV data
data = str2double(split(dataStr, ','));
% Assuming the first value is temperature, the second is pressure
temperature = data(1);
pressure = data(2);
% Get current time
currentTime = datetime('now');
% Update plots
plot(app.TemperatureAxes, currentTime, temperature, 'o');
hold(app.TemperatureAxes, 'on');
plot(app.PressureAxes, currentTime, pressure, 'o');
hold(app.PressureAxes, 'on');
end
end
5. In the startupFcn of your app, initialize the Bluetooth connection and a timer for periodically updating the plots:
function startupFcn(app)
% Initialize Bluetooth connection
app.BluetoothDevice = bluetooth('BluetoothDeviceName', ChannelNumber);
% Setup Timer for periodic updates
app.Timer = timer(...
'ExecutionMode', 'fixedRate', ...
'Period', 1, ... % Update rate in seconds
'TimerFcn', @(~,~) updatePlot(app));
start(app.Timer);
end
6. At the end stop the timer and close the Bluetooth connection when the app is closed in CloseRequestFcn callback:
function closeRequest(app)
stop(app.Timer); % Stop the timer
delete(app.Timer); % Delete the timer
clear app.BluetoothDevice; % Close Bluetooth connection
delete(app); % Delete the app
end
Someimportant things to note here are:
  • Data Format: Ensure your Arduino sends data in the expected format. Adjust the parsing logic in updatePlot as needed.
  • Timer Period: Adjust the timer's Period property according to how frequently your data is updated.
  • Plotting Efficiency: For more efficient plotting, consider using animatedline and addpoints instead of plot and hold on.
Ich hoffe, diese Schritte helfen Ihnen!
Grüße,
Nivedita.

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!