I would like to create a new toolbar shortcut that would perform some simple operation on a variable marked with a mouse in a workspace. I already have a shortcut for imagesc taken from available Matlab shortcuts. When I click on a variable and then click on the imagesc shortcut, the variable is displayed. This shortcut knows which variable to display. So my question is: what argument should I give in a script for new shortcut to pick the variable that is "clicked" in a workspace.
MATLAB: How to refer to workspace variable in Matlab toolbar shortcut
argumentstoolbar shortcut
Related Solutions
On the tool bar, click Layout then default
function Matlab_extendEditorFunctionality(activateFlg) %#ok<*NASGU>
%#ok<*AGROW>
%%--------------------------------------------------------------------------------------------
if nargin < 1 || isempty(activateFlg) activateFlg = false; activateFlg = true; end %%-------------------------------------------------------------------------------------------- jMainPane = []; jEditor = com.mathworks.mde.desk.MLDesktop.getInstance.getGroupContainer('Editor'); for childIdx = 1 : jEditor.getComponentCount componentClassName = regexprep(jEditor.getComponent(childIdx-1).class,'.*\.',''); if any(strcmp(componentClassName,{'DTMaximizedPane','DTFloatingPane','DTTiledPane'})) jMainPane = jEditor.getComponent(childIdx-1); break; end end if isa(jMainPane,'com.mathworks.mwswing.desk.DTFloatingPane') jMainPane = jMainPane.getComponent(0); % a com.mathworks.mwswing.desk.DTFloatingPane$2 object
end for jj = 1:jMainPane.getComponentCount hEditorPane = []; jSyntaxTextPaneView = getDescendent(jMainPane.getComponent(jj-1),[0,0,1,0,0,0,0]); if isa(jSyntaxTextPaneView,'com.mathworks.widgets.SyntaxTextPaneMultiView$1') hEditorPane(1) = handle(getDescendent(jSyntaxTextPaneView.getComponent(1),[1,0,0]),'CallbackProperties'); hEditorPane(2) = handle(getDescendent(jSyntaxTextPaneView.getComponent(2),[1,0,0]),'CallbackProperties'); else jEditorPane = getDescendent(jSyntaxTextPaneView,[1,0,0]); hEditorPane = handle(jEditorPane,'CallbackProperties'); end if activateFlg set(hEditorPane ,'KeyTypedCallback',@(src,evnt)Editor_KeyTypedCallback(src,evnt)) else set(hEditorPane ,'KeyTypedCallback','') end hEditorPaneArray(jj) = hEditorPane; end function child = getDescendent(child,listOfChildrenIdx) if ~isempty(listOfChildrenIdx) child = getDescendent(child.getComponent(listOfChildrenIdx(1)),listOfChildrenIdx(2:end)); end function Editor_KeyTypedCallback(~,evnt, varargin) keyPress = get(evnt,'keyChar'); if strcmp(keyPress,'(') || strcmp(keyPress,'"') || ... strcmp(keyPress,'[') || strcmp(keyPress,'''') || ... strcmp(keyPress,'{') aE = matlab.desktop.editor.getActive; pos = aE.Selection; switch keyPress case '(' putIn = ')'; case '"' putIn = '"'; case '[' putIn = ']'; case '{' putIn = '}'; case '''' putIn = ''''; end cursorPos = matlab.desktop.editor.positionInLineToIndex(aE, pos(1),pos(2)); % MyFunction(Input1,input2) if you missed a '(' it disables the auto input otherwise you would have to delete the
% closing bracket.
if any(strcmp(aE.Text(cursorPos-1),{'(','[','{','''','"'})) && ... ~isempty(regexp(aE.Text(cursorPos),['(\s|',char(13),')'],'match')) else putIn = ''; end aE.insertTextAtPositionInLine(putIn,pos(1),pos(2)); aE.goToPositionInLine(pos(1),pos(2)); end
Best Answer