How can I export data to specific cells in a prepared Excel file?

13 次查看(过去 30 天)
I would like to save some variables (results from calculations) to an Excel sheet. The Excel sheet already exists and has a certain structure. What I want to do is to save the values of the variables to specific cells (e.g. D4) without altering the structure of the Excel sheet. I have defined some variables in a test sheet to get a minimum working code example as shown below:
%Product parameters
prod_no = "01.01";
product = "Beer";
Temperature = 9;
%transfer to Excel result sheet
xlswrite("MK_Matlab.xlsx", prod_no, 'Sheet1', 'D4');
xlswrite("MK_Matlab.xlsx", Temperature , 'Sheet1', 'D10');
xlswrite("MK_Matlab.xlsx", product, 'Sheet1', 'D8');
When I execute this code, I get a warning:
%Warning: unable to write to Excel format, attempting to write file to CSV
%format.
The CSV looks as shown in the screenshot attached
.
How can I fix this?

采纳的回答

Manikanta Aditya
Manikanta Aditya 2024-3-4
移动:Angelo Yeo 2024-3-4
Hi Johannes,
Try using the 'writematrix' function instead of 'xlswrite' function: https://www.mathworks.com/help/matlab/ref/writematrix.html which supports writing to Excel files on all platforms.
This should write your data to the specified cells in the Excel file without altering its existing structure. Please note that writematrix function requires that the file is not open in Excel during the write operation. If the file is open, MATLAB may not be able to write to it.
If you’re using an older version of MATLAB that does not support writematrix, you might need to use a third-party MATLAB library that supports writing to Excel files on your platform, such as xlwrite.
Thanks!
  2 个评论
Johannes
Johannes 2024-3-4
移动:Angelo Yeo 2024-3-4
Hi, I just made it work. Thank you very much for your answer. Saving the variables in a Matrix worked. Also avoids excessive use of the 'writematrix' function
Dyuman Joshi
Dyuman Joshi 2024-3-5
"If you’re using an older version of MATLAB that does not support writematrix ..."
OP is using R2023b (as mentioned in the sidebar), so writematrix() is available to them.
"... you might need to use a third-party MATLAB library that supports writing to Excel files on your platform, such as xlwrite."
I wouldn't recommend using 3rd party tools.
A robust method, if writematrix() is not available, would be to convert the array to a table using array2table and then use readtable.

请先登录,再进行评论。

更多回答(0 个)

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by