Export Timetable data to Excel
1 Comment
Accepted Answer
Hi @chris weedon ,
After reviewing the “writetimetable” function at the link provided below
https://www.mathworks.com/help/matlab/ref/writetimetable.html
Based on your requirements, let me clarify the code you have provided and make sure it works effectively for your needs. The key steps in your approach involve reading data from ThingSpeak, displaying it, and writing it to an Excel file. Below is an updated version of your MATLAB code, along with explanations.
% Define your channel ID and API key readChannelID = ******; readAPIKey = '******';
% Specify the field ID for rainfall data RainFieldID = 1;
% Read the rainfall data into a timetable format for the last 7 days RainFall = thingSpeakRead(readChannelID, 'Fields', RainFieldID, ... 'NumDays', 7, 'ReadKey', readAPIKey, 'OutputFormat', 'TimeTable');
% Display the retrieved rainfall data display(RainFall, 'Rainfall');
% Specify the filename for the output Excel file outputFileName = 'RainfallData.xlsx';
% Write the timetable to an Excel spreadsheet writetimetable(RainFall, outputFileName);
% Display a message indicating where the file has been saved fprintf('The rainfall data has been saved to %s\n', outputFileName);
Now, let me explain the key component used in above modified code,
Reading Data
The thingSpeakRead function retrieves rainfall data from your specified ThingSpeak channel over a period of 7 days. The OutputFormat parameter is set to TimeTable, which formats your data as a timetable.
Displaying Data
The display function shows the retrieved rainfall data in your MATLAB command window.
Writing to Excel
The writetimetable function which was used in your provided code is being called with two arguments: the timetable variable (`RainFall`) and the desired filename (‘RainfallData.xlsx'). This makes sure that your data is saved correctly. Also, make sure that you have write permissions in your current working directory or specify an absolute path if needed (e.g., C:\path\to\your\folder\RainfallData.xlsx)
Output Confirmation
A simple fprintf statement at the end confirms where the file has been saved. This message will help you locate your file easily.
Please bear in mind that by default, MATLAB saves files in its current working directory. You can check or change this directory using the pwd command to print the current directory or cd('desired_path') to change it.
Hope this helps resolve your problem. Please let me know if you have any further questions for us.
4 Comments
More Answers (1)
3 Comments
See Also
Categories
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!