how to adress a single character in a chart axis title via Matlab COM/ActiveX

1 view (last 30 days)
Hello,
I'm trying to adress a single character of an axistitle of a chart in Excel/Powerpoint. I recorded a macro in excel, that shows how to adress one character via vba.
Here's a little snippet of matlab code that generates a chart in excel, adds a title and then fails to change a character.
xls = actxserver('Excel.Application');
xls.Workbook.Add;
xls.Visible=true;
crt = xls.ActiveSheet.Shapes.AddChart(75);
series = crt.Chart.SeriesCollection.NewSeries;
crt.Chart.Axes(1,1).HasTitle=true;
crt.Chart.Axes(1,1).AxisTitle.Format.TextFrame2.TextRange.Characters(1,1).Font.BaselineOffset = -0.25
The problem is, that the 'Characters' object is not an object for matlab, only a double value, so it has no methods or members. Has someone an idea how to adress a single character from matlab to set it so sub- or superscript?
I tried the code above with Excel 2010 and Matlab 2010b, but I also work with matlab 2012a, same problem there.
Thanks in advance!
Julian

Answers (2)

Julian Hapke
Julian Hapke on 9 May 2014
Finally, I found a workaround! Here's an example
str = 'foobar';
subscr = round(rand(length(str),1));
xls = actxserver('Excel.Application');
xls.Workbook.Add;
xls.Visible=true;
crt = xls.ActiveSheet.Shapes.AddChart(75);
series = crt.Chart.SeriesCollection.NewSeries;
crt.Chart.Axes(1,1).HasTitle=true;
pplab = crt.Chart.Axes(1,1).AxisTitle.Format.TextFrame2.TextRange;
pplab.Text = ' ';
for ii = length(str):-1:1
ch = invoke(pplab,'InsertBefore',str(ii));
set(ch.font,'BaselineOffset',-0.25*subscr(ii));
end
What it does:
  1. connect to excel
  2. add chart and empty series
  3. add axis title
  4. set Text to just one whitespace
  5. loop backwards through desired string
  6. add character before the current title string
  7. change baseline offset
I only could get the necessary character handle by using the 'InsertBefore' method. 'InsertAfter' would do the trick, too, but then you have a leading whitespace in the string, that you can't delete, because you can't get the handle of that character. Leading whitespaces, in contrary to the trailing ones, change the horizontal alignment of the text, so the trailing ones don't matter. I hope someone can use this advice, at least I'm happy now :)

Julian Hapke
Julian Hapke on 30 Jun 2014
If I had known, how to use the get command properly, I would by then have known, that get(pplab,'Characters',n,1) gives me the handle of the nth character of the string. So my workaround is obsolete...

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!