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?
0 件のコメント
採用された回答
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 件のコメント
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 Exchange で Spreadsheets についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!