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

13 ビュー (過去 30 日間)
Johannes
Johannes 2024 年 3 月 4 日
コメント済み: Dyuman Joshi 2024 年 3 月 5 日
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 件)

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

タグ

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by