Insert figure/image to Excel from MATLAB - excel

I'm building my own Excel Add-in using MATLAB Library Compiler.
I'm trying to build a function/add-in which takes two columns of data and plots it on a graph.
I can not figure out how to insert the figure from the MATLAB function into Excel.
Example
We have a MATLAB function called plot2D, which looks like this:
function [fig] = plot2D(DataX,DataY)
fig = figure;
plot(DataX, DataY,'r--');
end
We now use MATLAB Library Compiler and compile it into a xla-file and import it into Excel.
What I want is whenever I use this add-in it should generate a graph inside the current Excel sheet, given two data entries.
Now this doesn't work since Excel can't read the datatype figure of MATLAB.
But how would I go about this? Any suggestions?
Thank you.

Related

How to get the rand() function in excel to rerun when accessing an excel file through python

I am trying to access an excel file using python for my physics class. I have to generates data that follows a function but creates variance so it doesn’t line up perfectly to the function(simulating the error experienced in experiments). I did this by using the rand() function. We need to generate a lot of data sets so that we can average them together and eliminate the error/noise creates by the rand() function. I tried to do this by loading the excel file and recording the data I need, but then I can’t figure out how to get the rand() function to rerun and create a new data set. In excel it reruns when i change the value of any cell on the excel sheet, but I don’t know how to do this when I’m accessing the file with Python. Can someone help me figure out how to do this? Thank You.
Excel formulas like RAND(), or any other formula, will only refresh when Excel is actually running and recalculating the worksheet.
So, even though you may be access the data in an Excel workbook with Python, you won't be able to run Excel calculations that way. You will need to find a different approach.

Get charts/graphs from Excel to Matlab Report Generator

I know most of you would advise me to just get the data with xlsread() and plot it in Matlab. However I still want to know if it's possible to get a chart from excel to the matlab report generator as an image (or any other format) by coding it so it's automated.
The reason is I am supposed to make a report using multiple excel files that I did not create and that contains graphs that I would like to put in my report. So that would be more convenient if I could just copy the graphs & charts into my report.
Most of the things I found were saying to plot it with Matlab and nothing else. As i understand with xlsread you can only get [NUM,TXT,RAW]. So is there a function in the Matlab Report Generator to import the Excel graphs or should I just resign myself into reploting everything?
To do what I wanted I followed #CrisLuengo's suggestion and wrote a VBA macro in my spreadsheet that swipes in all the workbooks I have and exports all my charts as jpg.

Importing data from excel into matlab with both numbers and characters in the same cells

The data exists of Bank statements including descriptions. I've imported these bank statements from a .swi file into an excel file, which worked fine.
When imported to matlab with the:
[NUM, TXT, RAW] = xlsread(filename)
function, it worked but it did give the following warning:
Warning: Could not start Excel server for import, 'basic' mode will be used.
Refer to
HELP XLSREAD for more information.
In xlsread at 186
Which is strange because the university computer i'm using has excel. After importing, the data does show itself correctly in the RAW-matrix only it has high comma's in each cell, for example:
'C150331EUR000000001352,14'
When trying to make a function work with the data it does not function properly with a range of different errors, while the functions i'm using do work on random teststrings.
Anyone any idea how to make this data work?

How to put matrix from specific sheet and cell from excel to matlab

I have problem with my code. I have been doing my task using VBA Excel and Matlab. and then I want to put my matrix from Excel to Matlab. But my code does not work as expected.
MLPutMatrix "jum_kel", Range("D17")
MLPutMatrix "prob_mut", Range("D18")
MLPutMatrix "prob_co", Range("D19")
MLPutMatrix "uk_pop", Range("D20")
How do you code to put a matrix from a specific sheet and cell?
Is MLPutMatrix is a command you run from Excel? I'm not familiar with it. Moving data from Excel to Matlab can be done simply from within Matlab using the xlsread command. The docs explain how to use it. http://www.mathworks.com/help/techdoc/ref/xlsread.html
Might this work for you?

Modify an Excel sheet from Matlab

Is it possible to open a worksheet in excel from matlab and edit the formulas? The idea is to automate an uncertainty analysis by creating a second sheet with the uncertainty in each cell for the value from the previous cell. Essentially, I want to treat the cells as variables and do SQRT(SUM(Partials(xi)^2)) for each cell. Matlab should have no problem with the calc, but can it edit the formulas in sheets?
The process currently is to copy and paste from excel to matlab. Here's a small function that does the uncertainty in matlab against on array of equations:
function [f_u_total f_u] = uncertAnalysis(f, vars, vars_u)
f_u = [];
f_u_total = [];
for(i=1:length(f))
f(i)
item = uncertAnalysisi(f(i), vars, vars_u);
f_u = [f_u; item(1)];
f_u_total = [f_u_total; item(1)];
end
end
function [f_u_total f_u] = uncertAnalysisi(f, vars, vars_u)
f_u = [];
% take the partials and square them
for i=1:length(vars)
f_u = [f_u; vars(i) (diff(f, vars(i)).*vars_u(i)).^2];
end
% calculate the RSS
f_u_total = (sum(f_u(:,2))).^.5;
end
As an aside, the equations look something like this (why I'm not doing this by hand):
=(9*C!S3^2/C!V3^4*C!W3*(C!O3-
C!P3)/C!X3*C!Q3^6*C!F3^4/C!Y3^6/(C!U3^C!Z3)^6*F3^2+1/4*C!S3^2/C!V3^4*C!W3/(C!O3-
C!P3)/C!X3*C!Q3^6*C!F3^6/C!Y3^6/(C!U3^C!Z3)^6*O3^2+1/4*C!S3^2/C!V3^4*C!W3/(C!O3-
C!P3)/C!X3*C!Q3^6*C!F3^6/C!Y3^6/(C!U3^C!Z3)^6*P3^2+9*C!S3^2/C!V3^4*C!W3*(C!O3-
C!P3)/C!X3*C!Q3^4*C!F3^6/C!Y3^6/(C!U3^C!Z3)^6*Q3^2+1/C!V3^4*C!W3*(C!O3-
C!P3)/C!X3*C!Q3^6*C!F3^6/C!Y3^6/(C!U3^C!Z3)^6*S3^2+9*C!S3^2/C!V3^4*C!W3*(C!O3-
C!P3)/C!X3*C!Q3^6*C!F3^6/C!Y3^6/(C!U3^C!Z3)^6*C!Z3^2/C!U3^2*U3^2+4*C!S3^2/C!V3^6*C!W3*(C!O
3-C!P3)/C!X3*C!Q3^6*C!F3^6/C!Y3^6/(C!U3^C!Z3)^6*V3^2+1/4*C!S3^2/C!V3^4/C!W3*(C!O3-
C!P3)/C!X3*C!Q3^6*C!F3^6/C!Y3^6/(C!U3^C!Z3)^6*W3^2+1/4*C!S3^2/C!V3^4*C!W3*(C!O3-
C!P3)/C!X3^3*C!Q3^6*C!F3^6/C!Y3^6/(C!U3^C!Z3)^6*X3^2+9*C!S3^2/C!V3^4*C!W3*(C!O3-
C!P3)/C!X3*C!Q3^6*C!F3^6/C!Y3^8/(C!U3^C!Z3)^6*Y3^2+9*C!S3^2/C!V3^4*C!W3*(C!O3-
C!P3)/C!X3*C!Q3^6*C!F3^6/C!Y3^6/(C!U3^C!Z3)^6*LOG(C!U3)^2*Z3^2)^(1/2)
You should be able to do it through COM/ActiveX/Automation. Look at the External Interfaces document; there's an example for how to access Excel documents through Excel's Automation interfaces.
I have next-to-no experience manipulating Excel in this manner, but I know you can do just about anything in Excel through Automation and editing cell formulas doesn't sound that hard.
edit: I can't find a reference to the Excel object model, but here's another example: http://support.microsoft.com/kb/301982
This isn't a terribly elegant solution, but if you save a new .xls spreadsheet that's simply a tab-delimited (or CSV) file, you can have Matlab generate formulas and when Excel opens the document the values will populate.
In Perl, I've handled it something like this:
open(OUTPUT,'>tmpfile.xls');
print OUTPUT "1\t2\t=A1+B1\n";
close(OUTPUT);
And when tmpfile.xls is opened in Excel, cell C1 will display as 3, which will dynamically update appropriately if A1 or B1 are changed.
(I'm not good with Matlab, so I have no knowledge of any sort of plugins)
EDIT: My previous assumption that XLSWRITE wouldn't work was wrong. I just tried the following in MATLAB:
xlswrite('xltest.xls',{'1' '2' '=SUM(A1,B1)'});
and when I opened the file in excel, the function was in fact there! The limitation on this would be that you would have to use only the functions that are in Excel.
Unfortunately, I don't believe XLSREAD can read the formulae into MATLAB (it appears to just get the result).
PREVIOUSLY SUGGESTED OPTIONS:
You may want to check out the Spreadsheet Link EX software on the MathWorks website, although I'm a bit unfamiliar with it and am not sure if even that can do what you need. Something else that you should look into is MATLAB Builder EX, which "lets you integrate MATLAB® applications into your organization's Excel® workbooks as macro functions or add-ins". Sounds promising...
Use COM/ActiveX. You can open an Excel instance via the following command:
xlApp = COM.Excel.Application;
Then use a combination of code completion and the VBA help in Excel itself to work out the rest.
Remember to close Excel with
xlApp.Quit;
delete(xlApp);
On a side note, so-called CSE (Control-Shift-Enter) formulae may help? See Google.
As an alternative, see the code below (xlswrite) for using ActiveX from Matlab:
http://www.mathworks.com/matlabcentral/fileexchange/2855

Resources