从 Matlab 修改 Excel 工作表
是否可以从 matlab 打开 Excel 工作表并编辑公式? 这个想法是通过创建第二个工作表来自动化不确定性分析,其中每个单元格中的不确定性来自前一个单元格的值。 本质上,我想将单元格视为变量,并对每个单元格执行 SQRT(SUM(Partials(xi)^2)) 。 Matlab计算应该没有问题,但是它可以编辑sheet中的公式吗?
目前的过程是从excel复制粘贴到matlab。 这是一个小函数,它在 matlab 中针对方程组计算不确定性:
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
顺便说一句,方程看起来像这样(为什么我不手动执行此操作):
=(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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您应该能够通过 COM/ActiveX/Automation 来完成此操作。 查看外部接口文档; 有一个示例介绍如何通过 Excel 的自动化界面访问 Excel 文档。
我几乎没有以这种方式操作 Excel 的经验,但我知道您可以通过自动化在 Excel 中执行任何操作,并且编辑单元格公式听起来并不难。
编辑:我找不到对 Excel 对象模型的引用,但这是另一个示例: http:// support.microsoft.com/kb/301982
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
这不是一个非常优雅的解决方案,但如果您保存一个新的
.xls
电子表格,该电子表格只是一个制表符分隔(或 CSV)文件,您可以让 Matlab 生成公式,并且当 Excel 打开该文档时,值将会填充。在 Perl 中,我的处理方式如下:
当在 Excel 中打开 tmpfile.xls 时,单元格 C1 将显示为 3,如果 < code>A1 或
B1
已更改。(我不擅长Matlab,所以我不知道任何类型的插件)
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:
And when
tmpfile.xls
is opened in Excel, cellC1
will display as 3, which will dynamically update appropriately ifA1
orB1
are changed.(I'm not good with Matlab, so I have no knowledge of any sort of plugins)
编辑:我之前的假设 XLSWRITE 不会工作是错误的。 我刚刚在 MATLAB 中尝试了以下操作:
当我在 excel 中打开文件时,该函数实际上就在那里! 这样做的限制是您只能使用 Excel 中的函数。
不幸的是,我不相信 XLSREAD 可以阅读将公式输入 MATLAB(看起来只是得到结果)。
先前建议的选项:
您可能需要查看电子表格链接 EX MathWorks 网站上的软件,尽管我对它有点不熟悉,并且不确定它是否可以满足您的需求。 您还应该研究一下 MATLAB Builder EX,它“可以让您集成 MATLAB®应用程序作为宏函数或加载项添加到组织的 Excel® 工作簿中”。 听起来很有希望...
EDIT: My previous assumption that XLSWRITE wouldn't work was wrong. I just tried the following in MATLAB:
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...
使用 COM/ActiveX。 您可以通过以下命令打开 Excel 实例:
然后结合使用代码完成和 Excel 本身中的 VBA 帮助来完成其余的工作。
记得用 关闭 Excel
附带说明一下,所谓的 CSE(Control-Shift-Enter)公式可能会有所帮助? 请参阅 Google。
Use COM/ActiveX. You can open an Excel instance via the following command:
Then use a combination of code completion and the VBA help in Excel itself to work out the rest.
Remember to close Excel with
On a side note, so-called CSE (Control-Shift-Enter) formulae may help? See Google.
作为替代方案,请参阅下面的代码 (xlswrite),了解如何使用 Matlab 中的 ActiveX:
http://www .mathworks.com/matlabcentral/fileexchange/2855
As an alternative, see the code below (xlswrite) for using ActiveX from Matlab:
http://www.mathworks.com/matlabcentral/fileexchange/2855