Store and read MAT-file as embedded file in an Excel spreadsheet

2 vues (au cours des 30 derniers jours)
Staffan
Staffan le 1 Fév 2011
Manually, I can insert a MAT-file in an Excel sheet and store the MAT-file by storing the Excel sheet. I can also load the MAT-file to the MATLAB workspace by double-clicking the symbol of the embedded file in Excel.
Is there a possibility to programatically store and load a MAT-file which is embedded in an XLS file?
  2 commentaires
Jiro Doke
Jiro Doke le 1 Fév 2011
I'm not sure I understand your question. What do you mean by "embedded"? There is no way to embed a mat file (a physical file) inside an Excel file, like you would embed an image. You can insert the contents of a mat file (or a matrix) in an Excel file. Is that what you mean?
Staffan
Staffan le 1 Fév 2011
What i mean is (in Excel):
Insert > Object > Generate from file
(translated from German Excel)
This gives me a symbol of a mat file in the spreadsheet. Therefore I think, its embedded like an image?!
I would like to store a part of my data in Excel but the other part, a couple of structs and 3D matrix which I wouldn´t need to access in Excel, in the mat format. I need a lot of these 'datasets' and I want to have them in one file only and not as a couple.

Connectez-vous pour commenter.

Réponse acceptée

Jiro Doke
Jiro Doke le 1 Fév 2011
Another option is to use the COM interface. You would need to be familiar with some Visual Basic to fully utilize it. I'm not an expert but here's something that could get you started. I used MSDN Library for Excel to learn about the APIs.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Embedding MAT file
% Connect to Excel
Excel = actxserver('excel.application');
% Open an Excel workbook (false means not read-only)
Workbook = Excel.Workbooks.Open(fullfile(pwd, 'Book1.xlsx'),0,false);
% Select Sheet 1, cell B15
Workbook.Sheets.Item(1).Range('B15').Activate;
% Embedd an object
Workbook.ActiveSheet.OLEObjects.Add([], fullfile(pwd, 'mydata.mat'))
% Save workbook
Workbook.Save();
% Quit Excel
Excel.Quit();
delete(Excel);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%Loading from embedded MAT file in Excel
% Connect to Excel
Excel = actxserver('excel.application');
% Open an Excel workbook (false means not read-only)
Workbook = Excel.Workbooks.Open(fullfile(pwd, 'Book1.xlsx'),0,false);
% Get the (first) embedded object from Sheet 1
obj = Workbook.Sheets.Item(1).OLEObjects.Item(1);
% "Activate" - same as double-clicking
obj.Activate
% After this command, in my Excel 2007, a dialog pops up in Excel
% asking whether it's okay to open. Click OK.
% Quit Excel
Excel.DisplayAlerts = false;
Excel.Quit();
delete(Excel);
  4 commentaires
Staffan
Staffan le 2 Fév 2011
Im gonna try and report here... Thanks again!
Ravi
Ravi le 19 Juin 2013
how to use xlsread function in Embedded Matlab Function to plot the data in excel file... i need some sample code for that...

Connectez-vous pour commenter.

Plus de réponses (1)

Siddharth Shankar
Siddharth Shankar le 1 Fév 2011
If you are familiar with C++, then you could consider using the MAT-File API. You could use these functions within a MEX file to:
1. Return the value of a variable in the MAT file.
2. Modify the value of a variable in the MAT file.
This will get you started. Having done this, you will then need to work either with Spreadsheet Link EX or MATLAB Builder EX to interface with this MEX file.
  1 commentaire
Staffan
Staffan le 1 Fév 2011
Many thanks, unfortunately I´m not familar with C++. Is it very difficult, to get started with such a problem?

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by