【发布时间】:2012-05-09 19:27:34
【问题描述】:
我的任务是“在 excel 中创建饼图,然后在 matlab 中显示”。
我认为,我有两个麻烦:
1) 此图表是否正确创建图表? (A1-A6 是名称,B1-B6 - 数字)。
好的,这个功能起作用了。
Function CreateChart() As Excel.Chart
Dim title As String
title = "One"
Dim Book As Workbook
Set Book = ThisWorkbook
Dim new_sheet As Excel.Worksheet
Set new_sheet = Book.Sheets(1)
Dim new_chart As Excel.Chart
Set new_chart = Charts.Add()
ActiveChart.ChartType = xlPie
ActiveChart.SetSourceData Source:=new_sheet.Range("A1:B6"), _
PlotBy:=xlColumns
ActiveChart.Location Where:=xlLocationAutomatic, Name:=title
With ActiveChart
.HasTitle = True
.ChartTitle.Characters.Text = title
End With
Set CreateChart = new_chart
End Function
2) 如何与这个过程进行交互(未来 - 函数,返回图表)
使用matlab并在matlab中绘制这个饼图?
function chart = CreateChart( DataMatrix )
pie = actxserver('Excel.Chart');
all_pies = actxserver('Excel.Charts');
pietype = actxserver('Excel.XlChartType');
pie = all_pies.Add();
pie.ChartType = pietype.xlPie;
% here is a trouble to put data from matrix
pie.SetSourceData Source DataMatrix %hm.. strange
end
此代码不起作用! (我不知道如何重写字符串
ActiveChart.SetSourceData Source:=new_sheet.Range("A1:B6"), PloBy = xlColumns
)
P.S:我认为最好从 excel 文件加载脚本并返回 Chart。
但是如何在 matlab 中使用这个图表呢? (并画出来)
【问题讨论】:
-
永远不要说“此代码不起作用”。相反,请解释如何它不起作用。它实际上是做什么的?你希望它会做什么,为什么?如果有错误消息,整条错误消息到底说了什么?
-
您的第一段代码引用了一个名为
new_sheet的东西,它似乎没有在任何地方定义。是new_chart或Sheet还是什么? -
@GarethMcCaughan。我正确的问题。但是我对 vba 脚本有一个问题:如何分配
setSourceData Source一个数组变量?可能吗? (来源定义为范围)