Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

editor

matlab

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?

like image 551
KAE Avatar asked Sep 26 '11 17:09

KAE


2 Answers

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)
like image 187
Amro Avatar answered Oct 17 '22 22:10

Amro


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.

like image 34
b3. Avatar answered Oct 17 '22 22:10

b3.