How can I export data to specific cells in a prepared Excel file?
13 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Johannes
el 4 de Mzo. de 2024
Comentada: Dyuman Joshi
el 5 de Mzo. de 2024
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 comentarios
Respuesta aceptada
Manikanta Aditya
el 4 de Mzo. de 2024
Movida: Angelo Yeo
el 4 de Mzo. de 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 comentarios
Dyuman Joshi
el 5 de Mzo. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Spreadsheets en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!