1

My task is 'create pie chart in excel and then show it in matlab'.

as i think, i have two troubles:

1) is this chart correctly create chart? (A1-A6 are names, B1-B6 - numbers).

Ok, this function work.

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) How to interact with this procedure ( in future - function, returning Chart )

using matlab and draw this pie chart in 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

This code doesn't work! (i don't know how to rewrite string

ActiveChart.SetSourceData Source:=new_sheet.Range("A1:B6"), PloBy = xlColumns )

P.S: I think it is better to load script from excel file and return Chart.

But how to work with this chart in matlab? (and draw it)

gaussblurinc
  • 3,642
  • 9
  • 35
  • 64
  • 1
    Never ever say "This code doesn't work". Instead, explain *how* it doesn't work. What does it actually do? What did you hope it would do instead, and why? If there was an error message, what exactly did the whole error message say? – Gareth McCaughan May 09 '12 at 10:55
  • Your first piece of code refers to something called `new_sheet` which doesn't appear to be defined anywhere. Was it meant to be `new_chart` or `Sheet` or something? – Gareth McCaughan May 09 '12 at 10:57
  • @GarethMcCaughan. I correct question. But i have one question to vba script: how to assign `setSourceData Source` an Array-variable? is it possible? (Source defined as Range) – gaussblurinc May 09 '12 at 11:32

1 Answers1

0

you can always load the excel values to matlab with xlsread and draw the pie-chart in matlab itself using pie or pie3.

Gunther Struyf
  • 11,158
  • 2
  • 34
  • 58