- connect to excel
- add chart and empty series
- add axis title
- set Text to just one whitespace
- loop backwards through desired string
- add character before the current title string
- change baseline offset
how to adress a single character in a chart axis title via Matlab COM/ActiveX
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
Julian Hapke
2014-5-9
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:
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 :)
0 个评论
Julian Hapke
2014-6-30
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...
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!