How to get the first line of text M Matlab file?

I am using Matlab R2011b. I want to get the text of the first line of the active mfile in the editor. I know that I can use the following code to get all mfile text as an array of 1xn characters (not line break). However, I only need the first line.

activeEditor = matlab.desktop.editor.getActive ;    
activeEditor.Text ;

Any suggestions?

+5
source share
2 answers

One way to do this is to select all the text in the first line and then access the property SelectedText:

>> activeEditor = matlab.desktop.editor.getActive;
>> activeEditor.Selection = [1 1 1 Inf];
>> activeEditor.SelectedText

ans =

This is the first line of this file

, , , . , .

+2

" " :

activeEditor = matlab.desktop.editor.getActive;
pos = find(activeEditor.Text==char(10), 1, 'first');
firstLineStr = activeEditor.Text(1:pos-1)
+4

All Articles