Azzera filtri
Azzera filtri

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

13 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Manikanta Aditya
Manikanta Aditya il 4 Mar 2024
Spostato: Angelo Yeo il 4 Mar 2024
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 Commenti
Johannes
Johannes il 4 Mar 2024
Spostato: Angelo Yeo il 4 Mar 2024
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 il 5 Mar 2024
"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.

Accedi per commentare.

Più risposte (0)

Tag

Prodotti


Release

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by