Matlab - 使用xlsread从excel获取公式?

时间:2015-08-05 18:46:10

标签: excel matlab xlsread

在文档doc xlsread中,我们可以使用[num,txt,raw] = xlsread('example.xls')将数据从Excel工作表中提取到matlab中。

我有一张包含要复制到其他工作表的公式的工作表,但是xlread将采用解释的公式而不是公式本身。例如,一个公式是=AVERAGE(B8:V8),这是我想以编程方式从工作表中提取的,而是excel返回值0.810,这是公式将返回的。

是否可以使用matlab以任何方式提取公式?

2 个答案:

答案 0 :(得分:4)

xlsread无效。

使用COM Excel对象的一个​​示例:

让我们使用一个简单的Excel工作表,例如,包含文本,值和公式:

excel snippet

然后是以下代码:

xlfile  = 'test1.xlsx' ;
xlRange = 'B3:C6' ;

exl = actxserver('excel.application');                  %// Create a COM server
exlFile    = exl.Workbooks.Open( [pwd '\' xlfile] );    %'// Open the file
exlSheet1  = exlFile.Sheets.Item('Sheet1');             %// Choose the worksheet
strFormula = exlSheet1.Range(xlRange).Formula           %// Read the full range

产生一个漂亮的单元格数组:

strFormula = 
    'This is text'       'hello'          
    'this is value'      '12.5'           
    'this is value'      '29'             
    'this is formula'    '=AVERAGE(C4:C5)'

如果直接知道特定单元格的地址,则返回一个简单的字符串:

cellFormula = exlSheet1.Range('C6').Formula             %// Read a single cell

cellFormula =
=AVERAGE(C4:C5)

答案 1 :(得分:1)

使用xlsread是不可能的,您必须使用其中一个API来读取Excel文件或访问Excel。据我所知,选择是: