I am using Matlab R2011b. I want to get the text of the first line of the active mfile in the Editor. I know I can use the following code to get all the text of the mfile as a 1xn character array (not broken into lines). However I only want the first line.
activeEditor = matlab.desktop.editor.getActive ;
activeEditor.Text ;
Any suggestions?
You can search for the first "newline" character, and return everything from the beginning to that position:
activeEditor = matlab.desktop.editor.getActive;
pos = find(activeEditor.Text==char(10), 1, 'first');
firstLineStr = activeEditor.Text(1:pos-1)
One way to do this is to select all of the text on the first line and then access the SelectedText
property:
>> activeEditor = matlab.desktop.editor.getActive ; >> activeEditor.Selection = [1 1 1 Inf]; >> activeEditor.SelectedText ans = This is the first line of this file
You could improve on this by storing the current selection before selecting the entire first line and then restoring the selection after the selected text is accessed. This way the cursor position is not lost.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With