代码之家  ›  专栏  ›  技术社区  ›  Brandon Dube

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

  •  0
  • Brandon Dube  · 技术社区  · 9 年前

    在文档中 doc xlsread 人们可以使用 [num,txt,raw] = xlsread('example.xls') 将excel表格中的数据拉入matlab。

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

    可以用matlab以任何方式提取公式吗?

    2 回复  |  直到 9 年前
        1
  •  4
  •   Hoki    9 年前

    这是不可能的 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)
    
        2
  •  1
  •   Community CDub    7 年前

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