フィルターのクリア

Is it possible to programmatically add a file extension for syntax highlighting?

2 ビュー (過去 30 日間)
Patrick Mboma
Patrick Mboma 2015 年 10 月 24 日
編集済み: Jan 2015 年 10 月 24 日
Hi,
Is it possible to programmatically add a file extension for syntax highlighting?
Just for the sake of argument, suppose I have a file with extension "xtn". It is possible to go into
preference-->editor/Debugger-->language
and from there add extension xtn under the block "file extensions". Then matlab will apply the same syntax coloring it uses for files with extension .m to all files with extension xtn.
Is there any way to do this programmatically instead of manually?
Thanks

回答 (1 件)

Jan
Jan 2015 年 10 月 24 日
編集済み: Jan 2015 年 10 月 24 日
You can do it the hard way:
Open the file prefdir\matlab.prf. Search for the variable "Editor.Language.MATLAB.Extensions" and append 'xtn'.
Ext = 'xtn';
PrefFile = fullfile(prefdir, 'matlab.prf');
copyfile(PrefFile, [PrefFile, '.bak']); % To be secure
fid = fopen(PrefFile, 'r');
if fid == -1
error('Cannot open file for reading: %s', PrefFile);
end
C = textscan(fid, '%s', 'Delimiter', '\n');
Data = C{1};
fclose(fid);
key = 'Editor.Language.MATLAB.Extensions';
index = strncmp(Data, key, length(key));
if sum(index) ~= 1
error('Cannot identify key line.');
end
keyLine = Data{index};
keyData = keyLine(length(key) + 3:end); % <key>=S...
keyValue = regexp(keyData, ';', 'split');
if ~any(strcmpi(keyValue, Ext))
keyData = [keyData, ';', Ext];
end
Data{index} = [key, '=S', keyData];
% Save the file:
fid = fopen(PrefFile, 'w');
if fid == -1
error('Cannot open file for writing: %s', PrefFile);
end
fprintf(fid, '%s\n', Data{:});
fclose(fid);
Afterwards restart Matlab:
!matlab
quit
Or you use the smart, but undocumented methods:
key = 'Editor.Language.MATLAB.Extensions';
keyData = com.mathworks.services.Prefs.getPref(key);
keyValue = regexp(keyData, ';', 'split');
if ~any(strcmpi(keyValue, Ext))
keyData = [keyData, ';', Ext];
com.mathworks.services.Prefs.setStringPref(key, keyData);
end
This worked 2007 (see http://mathworks.com/matlabcentral/newsreader/view_thread/154608) and in R2011b. But as all undocumented methods this might change in the future.
Finally, let me mention, that this does not look like a useful method to me. I guess that it is easier to change the file extension from the concerned files.
  2 件のコメント
Patrick Mboma
Patrick Mboma 2015 年 10 月 24 日
Dear Jan,
Thanks for the answer. What I am hoping for is something simple that I can turn on and off straight from the command window.
It could be something like
add_extension('xtn')
remove_extension('xtn')
Of course if it is too complicated, which would be a pity, I would simply have to revert to the textbook way of doing it.
Jan
Jan 2015 年 10 月 24 日
編集済み: Jan 2015 年 10 月 24 日
Too complicated? I've posted 2 solutions already. So simply copy&paste one of them into a function called "add_extension". All you have to modify for "remove_extension" is:
match = strcmpi(keyValue, Ext);
if any(match)
keyValue(match) = [];
keyData = sprintf('%s;', keyValue{:});
keyData(end) = [];
com.mathworks.services.Prefs.setStringPref(key, keyData);
end

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by