Using other data in Excel add in
2 次查看(过去 30 天)
显示 更早的评论
This question is about using the Library compiler to create add ins for Excel. Any help or suggestions would be much appreciated.
The library compiler creates a class (default called Class1). Any functions that you add become methods of that class. My question is about the best way to use other data (not stored in Excel) in those functions.
Here's a simplified example that illustrates my issue. I have a Matlab function (Function1) that takes a single input of a column vector (n x 1). Function1 multiplies this input column vector by a (n x n) square matrix and outputs the result (which is another column vector of size n x 1). The input column vector is always multiplied by the same square matrix. The user regularly changes the input column vector and then looks at the output. At the moment, I have the square matrix saved in a workspace. When Function1 runs, it loads the workspace in order to used the square matrix.
[In reality, the data loaded from the workspace is large and varied: multi-dimensional arrays of numbers, as well as various categorical arrays, so reading it all from excel isn't my preferred option]
My issue with the system above is that every time the function runs, it loads the data, which seems to add an overhead. Is there any way for the data to be loaded once at the start and then accessed when the function runs on subsequent times?
One option that would solve my issue is if the data could be saved as class members of Class1. Is there anyway to add members/properties to Class1? The library complier dialogue only seems to let you add methods.
Thanks in advance!
2 个评论
Jayaram Theegala
2017-12-15
When you say, " At the moment, I have the square matrix saved in a workspace" do you mean loading a MAT file that loads all the Workspace variables or some other file that has the nxn matrix that you are multiplying with the input?
If that is not correct, please do elaborate on what you mean by loading workspace and the exact steps you are following to load the nxn matrix, because the term "Workspace" can easily be confused with MATLAB Workspace, which is not accessible while running an Excel Addin created using MATLAB Compiler.
采纳的回答
Jayaram Theegala
2017-12-18
If I understand the use-case correctly, you want to avoid loading the big matrix from the MAT file for every function call. You can certainly do that using one of the following ways:
1) Since the Excel Addin uses the Singleton MATLAB Runtime by default, you can have the big matrix as a global variable and only load the data from MAT file if the variable is empty. To illustrate this workflow, take a look at the following MATLAB code:
global bigMatrix;
if(isempty(bigMatrix))
%temp.mat file has all the data you would like to initialize your variables with
data = load('temp.mat');
bigMatrix = data.temp; %
else
%making changes to the big matrix and this will reflect in subsequent function calls
%It may not be applicable in your case as you are not modifying the matrix
bigMatrix = bigMatrix+1;
end
For more information about the Singleton MATLAB Runtime instance, click on the following URL: https://www.mathworks.com/matlabcentral/answers/14633-mcr-shared-option-in-deploytool
2) If all the users of the Excel Addin, have access to MATLAB, you can consider using Spreadsheet link instead of Excel Addin. Unlike Excel Addin which uses MATLAB Compiler Runtime (MCR), Spreadsheet Link uses a single MATLAB instance. It also allows you to view the MATLAB instance and run MATLAB commands directly on the MATLAB Command Window and this will allow you to see what the current variables are and their values. For more information about Spreadsheet Link, click on the following URL:
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Excel Add-Ins 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!