It's been over six years since I've written any serious MATLAB code, so I thought it would be fun to see how easily ChatGPT could help me out. While others have probably already used ChatGPT to generate MATLAB code, I didn't find any evidence of it when I searched through the ThingSpeak forum. That inspired me to post an example to get people thinking about it.
This example reads four temperature fields from the same channel and plots them on a single graph.
My ChatGPT prompt:
The prompt is pretty straightforward and essentially walks through all the elements of the chart that I wanted. It's also important to consider any filtering or "data cleansing" that should be done. Since this was my first time doing this, I decided to use an existing plot I was already familiar with as my "target state".
The prompt: "I would like you to generate some MATLAB code to create what is called a MATLAB Visualization. Its purpose will be to generate a chart of 4 fields found in a ThingSpeak channel. The ThingSpeak channel name is "Nest Nanny 2 (NN-02)" and its channel id (needed for the code) is xxxxxxx. The read api key is XXXXXXXXXXXXXXXX. The chart title should be "Nest Nanny 02 (NN-02) Todays Temperature Readings" It should plot data from 4 fields (field 3 - 6) who's names are "Surface Temp", "1/4 Temp", "3/4 Temp", and "Deep Temp". I would like the plot lines for these 4 fields to be different colors (red, orange, cyan, and blue) but on the same chart. I would like Temperature in Fahrenheit to be on the Y axis and Time on the x axis. I would like it to display the last 100 data points. I'd like to use this colororder. colororder(["#FF0000","#FFBB00","#00BBFF", "#0000FF"]) I would like the legend to be to the right of the chart, not contained within the chart. No temperature data should have a value of 50 or less and we can assume it's a sensor issue when this is observed. If a data value is 50 or less, use the value of it's nearest (in the time series) non-zero, non-negative value for that field. The data is stored in the channel as Fahrenheit and does not need to be converted from Celsius. "
Because I did this exercise using an existing plot and had a clear objective in mind, it was relatively quick to iterate four or five times with ChatGPT to refine my prompt. In just a few minutes, the generated plot almost perfectly matched my existing plot. (I xxxx'ed out the channel ID and API key before posting.) For someone who now only occasionally uses MATLAB, this approach saves time and will hopefully allow me to tap into elements of MATLAB that are now beyond my skill level and memory. ;)
ChatGPT generated MATLAB code:
channelID = xxxxxxx;
readAPIKey = 'XXXXXXXXXXXXXXXX';
numPoints = 100;
[data, time] = thingSpeakRead(channelID, ...
'Fields', [3 4 5 6], ...
'NumPoints', numPoints, ...
'ReadKey', readAPIKey);
for i = 1:size(data, 2)
badIndices = find(data(:,i) <= 50);
validIndices = find(data(:,i) > 50);
if ~isempty(validIndices)
data(badIndices, i) = interp1(validIndices, data(validIndices, i), badIndices, 'linear', 'extrap');
end
end
figure;
colororder(["#FF0000","#FFBB00","#00BBFF", "#0000FF"]);
plot(time, data(:,1), 'LineWidth', 2); hold on;
plot(time, data(:,2), 'LineWidth', 2);
plot(time, data(:,3), 'LineWidth', 2);
plot(time, data(:,4), 'LineWidth', 2);
hold off;
xlabel('Time');
ylabel('Temperature (°F)');
title('Nest Nanny 02 (NN-02) Todays Temperature Readings');
legend({'Surface Temp', '1/4 Temp', '3/4 Temp', 'Deep Temp'}, ...
'Location', 'eastoutside');
datetick('x', 'HH:MM:SS', 'keepticks');
grid on;
set(gca, 'FontSize', 12);
Plot Comparison: