Borrar filtros
Borrar filtros

How I can save workspace variables to an excel file, storing them coloumn by coloumn with their respective name in the first row?

212 visualizaciones (últimos 30 días)
I have workspace variables (numbers) out of a simulation by Simulink. I'd like to save them in an excel file, each one with its name on the first row, then the values in coloumn. Than I'd like to automatically open the saved file in excel. Here below my draft code:
filename='workspace_variable.xls';
save(filename,'time','Temperature','-ASCII','-double');
winopen('workspace_variable.xls');
I get error in excel that the file format and extension of the "workspace_variable.xls" don't match, then i click ok and it opens the file anyway. The cells are filled just in the first coloumn with "time" values, but not the coloumn for Temperature values.
Thank you.

Respuesta aceptada

Geoff Hayes
Geoff Hayes el 26 de Sept. de 2016
eugenio - your code seems to be writing text data to a file that has an Excel extension, but this doesn't mean that you are creating an Excel file which may explain the format and extension error. You would need to use xlswrite to write your MATLAB data to an Excel file. See the examples from this link which show how you can do this.
As for your temperature values not appearing, I suspect that they are being appended beneath the time values. Again, see the xlswrite documentation to get an idea on how to write your variable data to specific columns in the Excel spreadsheet.
Or perhaps a text file is sufficient?
  3 comentarios
Geoff Hayes
Geoff Hayes el 27 de Sept. de 2016
eugenio - your Results is trying to combine characters with numbers
Results=['A','B';A,B;];
and so is producing unpredictable results (which you can verify by looking at this array in the MATLAB workspace). If you want to write a header for each column, then I would do this separately with its own call to xlswrite or just use the default column headers in Excel.
eugenio penazzi
eugenio penazzi el 29 de Sept. de 2016
Editada: Geoff Hayes el 29 de Sept. de 2016
Finally done:
clear all; close all; clc;
filename = 'testdata.xls';
A = [12.7 5.02 -98 63.9 0 -.2 56]';
B = [1.1 -2.3 3 40.05 5.003 6 7]';
Results_Names={'A','B'};
Results_Values=[A,B];
sheet=1;
xlRange='A2';
xlswrite(filename,Results_Values,sheet,xlRange);
sheet=1;
xlRange='A1';
xlswrite(filename,Results_Names,sheet,xlRange);
winopen('testdata.xls');
Thank you.

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by