MATLAB Answers

Switching between open files in editor by searching

43 views (last 30 days)
I know this has been asked before, but among the questions I found, no one mentioned the already implemented search which is the closest match.
I want to quickly switch to an open file in the matlab editor by starting to type the name of the file, e.g. "myFu" would suggest "myFunction.m" and any other matching "myFu*". I find this functionality when the file "tab-bar" on top of the editor is full, and the dropdown menu for selecting files is displayed. This dropdown menu has the search functionality that I am looking for (see attached imaged). I find this search only available when the file-tab bar is full. This is ok as the need for a search is often related to many simultaneously open files. My question is:
Is there a action for this search which i can bind with a shortcut?
(I am familiar with the "Find files" functionality and "Ctrl + Page Up/Down". I am currently on windows and running R2020b Update 5)

Accepted Answer

Steven Lord
Steven Lord on 3 Mar 2021
There is an API you could potentially use for this sort of task. I have my installation of release R2019b open, so I'll just open a couple files. The getAll function lists all files open in the Editor.
>> D = matlab.desktop.editor.getAll
D =
1×3 Document array with properties:
Filename
Opened
Language
Text
Selection
SelectedText
Modified
Editable
>> {D.Filename}.'
ans =
3×1 cell array
{'C:\Program Files\MATLAB\R2019b\toolbox\matlab\general\bench.m'}
{'C:\Temp\example300062.m' }
{'C:\Program Files\MATLAB\R2019b\license_agreement.txt' }
This is one MATLAB function file included in MATLAB, one MATLAB code file I created, and one plain text file. The getActive function lists just the one that is the active document. In this case that's D(3).
>> A = matlab.desktop.editor.getActive
A =
Document with properties:
Filename: 'C:\Program Files\MATLAB\R2019b\license_agreement.txt'
Opened: 1
Language: 'Plain'
Text: 'The MathWorks, Inc. Software License Agreement ↵↵IMPORTANT NOTICE↵↵THIS IS THE SOFTWARE LICENSE AGREEMENT (THE "AGREEMENT") OF THE MATHWORKS, INC.↵("MATHWORKS") FOR THE PROGRAMS. THE PROGRAMS ARE LICENSED, NOT SOLD. READ THE↵TERMS AND CONDITIONS OF THIS AGREEMENT CAREFULLY BEFORE COPYING, INSTALLING, OR↵USING THE PROGRAMS. FOR INFORMATION ABOUT YOUR LICENSE OFFERING, CONSULT THE↵PROGRAM OFFERING GUIDE PRESENTED AFTER THE AGREEMENT.↵↵THE AGREEMENT REPRESENTS THE ENTIRE AGREEMENT BETWEEN YOU (THE "LICENSEE") AND↵MATHWORKS CONCERNING YOUR RIGHTS TO INSTALL AND USE THE PROGRAMS UNDER THE↵LICENSE OFFERING YOU ACQUIRE. ↵↵YOU MUST ACCEPT THE TERMS OF THIS AGREEMENT TO COPY, INSTALL, OR USE THE↵PROGRAMS. IF YOU DO NOT ACCEPT THE LICENSE TERMS, THEN YOU MUST IMMEDIATELY↵STOP USING THE PROGRAMS.↵↵IF YOU TERMINATE THIS LICENSE FOR ANY REASON WITHIN THIRTY (30) DAYS OF PROGRAM↵DELIVERY (THE "ACCEPTANCE PERIOD") YOU WILL RECEIVE A FULL REFUND FROM THE↵AUTHORIZED DISTRIBUTOR FROM WHOM YOU ACQUIRED THE PROGRAMS, OR FROM MATHWORKS↵IF YOU ACQUIRED THE PROGRAMS DIRECTLY FROM MATHWORKS. ↵↵↵1. CAPITALIZED TERMS. Capitalized terms used in this Agreement and not↵otherwise defined in this Agreement shall have the definitions ascribed to them↵in Article 23 below.↵↵2. LICENSE GRANT. MathWorks hereby grants to Licensee, subject to the terms of↵this Agreement, a nonexclusive license (the "License") to:↵↵ 2.1. install and use the Programs solely on Computers controlled by↵ Licensee, in accordance with the License Offering acquired, and solely for↵ Internal Operations; ↵↵ 2.2. use the Programs as expressly allowed in the Program Offering Guide,↵ for instance to deploy Applications, consistent with the terms of the↵ License Offering acquired; ↵↵ 2.3. provide reasonable access to online Documentation on Licensee's intranet, provided it is not accessible over the open Internet; and↵↵ 2.4. print portions of the online Documentation for reasonable use by↵ Licensed Users.↵↵3. LICENSE RESTRICTIONS. The License granted is subject to the express↵restrictions set forth below and any other restrictions set forth in any signed↵agreement between MathWorks and Licensee. Licensee shall not, and shall not↵cause or permit any other individual or entity to, directly or indirectly:↵↵ 3.1. modify or create any derivative work of a Program, except as expressly↵ permitted in the Program Offering Guide;↵↵ 3.2. use a Program for any act which infringes copyright of a Program, for instance, by developing, producing, or testing a computer program↵ containing a feature or functionality that is substantially similar in its↵ expression to the expression contained in a Program;↵↵ 3.3. disassemble, decompile, or reverse engineer a Program, or attempt to↵ gain access to its method of operation or source code;↵↵ 3.4. adapt, translate, copy, convert, use, test, benchmark, or determine↵ the expression or design of, a Program or any materials derived from or↵ generated by a Program, in order to make or distribute an application or↵ software, a principal purpose of which, as reasonably determined by↵ MathWorks, is to perform the same or similar functions as a Program or↵ other MathWorks product or service or which replaces any component of a↵ Program or other MathWorks product or service. Licensee shall not use a↵ Program to compete with the products or businesses of MathWorks, including by distributing libraries or any form of an entire Program or Program↵ Component;↵↵ 3.5. rent, lease, or loan the Programs, use the Programs for supporting↵ Third Parties' use of the Programs, time share the Programs, or provide↵ service bureau or similar service use;↵↵ 3.6. sell, license, sublicense, provide access, publish, display,↵ distribute, disseminate, assign, or otherwise transfer to a Third Party the↵ Programs or any copy thereof, or any license or other rights thereto, in↵ whole or in part, without MathWorks' prior written consent, except as↵ expressly permitted in the Program Offering Guide; ↵↵ 3.7. alter, remove, or obscure any copyright, trade secret, patent, trademark, logo, proprietary and/or other legal notices or license files on↵ or in copies of a Program or Program Component, including copies made for↵ backup purposes;↵↵ 3.8. provide access (directly or indirectly) to any Program via a web or↵ network interface, except as expressly permitted in the License Offering; ↵↵ 3.9. copy, make available for copy, or otherwise reproduce the Programs↵ except either (a) as may be required for their installation for the purpose↵ of executing the Program in accordance with the License Offering, or (b) to make a reasonable number of copies solely for back-up purposes; ↵↵ 3.10. access or use MathWorks products or services that Licensee is not↵ currently licensed to access or to use; ↵↵ 3.11. allow the Programs to be accessed or used by a Third Party except as↵ expressly permitted in the Program Offering Guide; ↵↵ 3.12. disclose or transfer the activation key, login credentials, and/or license file to a Third Party, or allow them to be used by a Third Party↵ without written authorization from MathWorks or as permitted by the License↵ Offering acquired; ↵↵ 3.13. republish the Documentation, except as expressly permitted in Article↵ 2 of this Agreement;↵↵ 3.14. create a server or automated system for the Transformation Programs↵ without obtaining a license from MathWorks that expressly allows such use↵ (see the Program Offering Guide);↵↵ 3.15. disable, circumvent, or workaround any technical limitations in the↵ Programs;↵↵ 3.16. access, enable access to, modify, translate, use or otherwise make↵ available temporary intermediate files produced by a Program;↵↵ 3.17. make any use of the License on behalf of or for the benefit of a↵ Third Party or Affiliate in any manner that would constitute a violation of↵ the License if such use were directly made by such person, or cause any↵ Third Party or Affiliate to make any use of the License on behalf of or for↵ the benefit of Licensee or an Affiliate in any manner that would constitute↵ a violation of this License if such use were directly made by Licensee; ↵↵ 3.18. acquire the License if a principal purpose of the acquisition is to↵ transfer or assign the License to a Third Party or Affiliate unless↵ expressly permitted by MathWorks;↵↵ 3.19. mechanize or automate the process of checking out or in license keys↵ for one or more Programs, including by running a second session of a↵ Program, running a Program to serve multiple users, or rebooting the↵ License Manager, for a principal purpose of minimizing the License check↵ out time of any Programs, or otherwise circumventing the intended License↵ Manager operation;↵↵ 3.20. use a License Manager other than the one which was provided with the↵ most recent version of the Programs Licensee is using; ↵↵ 3.21. take any action that requires any portion of the Programs to be↵ subject to end-user rights incompatible or inconsistent with the↵ restrictions set forth in this Agreement; ↵↵ 3.22. allow access to the Programs by an individual who is under 13 years↵ old or otherwise is under the lawful age for providing consent without↵ obtaining the advance written consent of a parent or guardian and storing↵ such consent in a retrievable way;↵↵ 3.23. create an Application (a) providing capability similar to the↵ capabilities of any of the Programs; (b) providing functionality or↵ behavior similar to that of the MATLAB command line or scripting and↵ programming capabilities; (c) allowing operation of the code generation↵ capabilities of Programs; (d) providing access to an entire Program or a↵ substantial portion of a Program; or (e) enabling evaluation of end-user↵ code or expressions; ↵↵ 3.24. distribute an Application that includes library header files from any↵ of the Programs or any Program Component;↵↵ 3.25. distribute an Application that reads MAT-files (i.e., files in the↵ MATLAB MAT data file format) using the MAT file API to Third Parties, unless such Application also allows such Third Parties to write MAT-files↵ for the same data;↵↵ 3.26. distribute an Application without an Application License, unless the↵ Application is solely for Internal Operations or is part of an embedded↵ system that has no provision for licensing to its end-users; ↵↵ 3.27. distribute an Application unless all copyright and proprietary↵ notices for the Programs are duplicated and included with Licensee's own↵ copyright notices for the Application, wherever they appear; and↵↵ 3.28. make copies of the MATLAB Runtime accessible to any Third Party↵ separate from or for use other than with an Application, or provide the↵ MATLAB Runtime for service bureau or similar service use. ↵↵4. PROGRAM TRIAL EVALUATION; COMPLIMENTARY LICENSES. Licensee agrees to use↵any Program provided to Licensee as a trial evaluation only for the period of↵the trial evaluation which may be controlled by a license key code, only to↵evaluate it individually for potential purchase of a license to the Program as↵an end-user, to conduct no business with it, to remove it and all result files↵produced from any of Licensee's Computers at the end of the trial evaluation↵period and to comply with all other obligations and restrictions in this↵Agreement. Licensee agrees to use any Program awarded to Licensee on a↵complimentary basis for student engineering competitions and similar↵competitions, course support, research, fellowship participation, and teaching↵assistance only for the specific purpose and period of the award, to conduct no↵business with it, and to comply with all other obligations and restrictions in↵the award letter and in…'
Selection: [1 1 1 1]
SelectedText: ''
Modified: 0
Editable: 1
Let's make the first file, bench.m, active using makeActive.
>> makeActive(D(1))
>> A2 = matlab.desktop.editor.getActive
A2 =
Document with properties:
Filename: 'C:\Program Files\MATLAB\R2019b\toolbox\matlab\general\bench.m'
Opened: 1
Language: 'MATLAB'
Text: 'function times = bench(count)↵%BENCH MATLAB Benchmark↵% BENCH times six different MATLAB tasks and compares the execution↵% speed with the speed of several other computers. The six tasks are:↵%↵% LU LAPACK. Floating point, regular memory access.↵% FFT Fast Fourier Transform. Floating point, irregular memory access.↵% ODE Ordinary diff. eqn. Data structures and functions.↵% Sparse Solve sparse system. Sparse linear algebra.↵% 2-D 2-D Lissajous plot. Animating line plot.↵% 3-D 3-D SURF(PEAKS)and HGTransform. 3-D surface animation.↵%↵% A final bar chart shows speed, which is inversely proportional to↵% time. Here, longer bars are faster machines, shorter bars are slower.↵%↵% BENCH runs each of the six tasks once.↵% BENCH(N) runs each of the six tasks N times.↵% BENCH(0) just displays the results from other machines.↵% T = BENCH(N) returns an N-by-6 array with the execution times.↵%↵% The comparison data for other computers is stored in a text file:↵% fullfile(matlabroot, 'toolbox','matlab','general','bench.dat')% Updated versions of this file are available from MATLAB Central↵% Note the link above opens your system web browser as defined by WEB.↵%↵% Fluctuations of five or 10 percent in the measured times of repeated↵% runs on a single machine are not uncommon. Your own mileage may vary.↵%↵% This benchmark is intended to compare performance of one particular↵% version of MATLAB on different machines. It does not offer direct↵% comparisons between different versions of MATLAB. The tasks and↵% problem sizes change from version to version.↵%↵% The 2-D and 3-D tasks measure graphics performance, including software↵% or hardware support for OpenGL. The command↵% opengl info↵% describes the OpenGL support available on a particular machine.↵↵% Copyright 1984-2015 The MathWorks, Inc.↵↵if nargin < 1, count = 1; end;↵times = zeros(count,6);↵fig1 = figure;↵set(fig1,'pos','default','menubar','none','numbertitle','off', ...↵ 'name',getString(message('MATLAB:bench:MATLABBenchmark')));↵hax1 = axes('pos',[0 0 1 1],'parent',fig1);↵axis(hax1,'off');↵text(.5,.6,getString(message('MATLAB:bench:MATLABBenchmark')),'parent',hax1,'horizontalalignment','center','fontsize',18)↵task = text(.50,.42,'','parent',hax1,'horizontalalignment','center','fontsize',18);↵drawnow↵pause(1);↵↵% Use a private stream to avoid resetting the global stream↵stream = RandStream('mt19937ar');↵↵problemsize = zeros(1, 4);↵↵bench_lu(stream);↵bench_fft(stream);↵bench_ode;↵bench_sparse;↵bench_2d('Off');↵bench_3d(false); ↵↵for k = 1:count↵ ↵ % LU, n = 2400.↵ set(task,'string',getString(message('MATLAB:bench:LU')))↵ drawnow↵ [times(k,1), problemsize(1)] = bench_lu(stream);↵ ↵ % FFT, n = 2^21. ↵ set(task,'string',getString(message('MATLAB:bench:FFT')))↵ drawnow↵ [times(k,2), problemsize(2)] = bench_fft(stream);↵ ↵ % ODE. van der Pol equation, mu = 1↵ set(task,'string',getString(message('MATLAB:bench:ODE')))↵ drawnow↵ [times(k,3), problemsize(3)] = bench_ode;↵ ↵ % Sparse linear equations↵ set(task,'string',getString(message('MATLAB:bench:Sparse')))↵ drawnow↵ [times(k,4), problemsize(4)] = bench_sparse;↵ ↵ % 2-D graphics↵ set(task,'string',getString(message('MATLAB:bench:twoD')))↵ drawnow↵ pause(1)↵ times(k,5) = bench_2d('On');↵ ↵ % 3-D graphics. Vibrating logo.↵ set(task,'string',getString(message('MATLAB:bench:threeD')))↵ drawnow↵ pause(1)↵ times(k,6) = bench_3d(true); ↵ ↵end % loop on k↵↵% Compare with other machines. Get latest data file, bench.dat, from↵% MATLAB Central at the URL given in the help at the beginning of bench.m↵↵if exist('bench.dat','file') ~= 2↵ warning(message('MATLAB:bench:noDataFileFound'))↵ return↵end↵fp = fopen('bench.dat', 'rt');↵↵% Skip over headings in first four lines.↵for k = 1:4↵ fgetl(fp);↵end↵↵% Read the comparison data↵↵specs = {};↵T = [];↵details = {};↵g = fgetl(fp);↵m = 0;↵desclength = 61;↵while length(g) > 1↵ m = m+1;↵ specs{m} = g(1:desclength); %#ok<AGROW>↵ T(m,:) = sscanf(g((desclength+1):end),'%f')'; %#ok<AGROW>↵ details{m} = fgetl(fp); %#ok<AGROW>↵ g = fgetl(fp);↵end↵↵% Close the data file↵fclose(fp);↵↵% Determine the best 10 runs (if user asked for at least 10 runs)↵if count > 10↵ warning(message('MATLAB:bench:display10BestTrials', count));↵ totaltimes = 100./sum(times, 2);↵ [~, timeOrder] = sort(totaltimes, 'descend'); ↵ selected = timeOrder(1:10);↵else↵ selected = 1:count;↵end↵↵meanValues = mean(T, 1);↵↵% Add the current machine and sort↵T = [T; times(selected, :)];↵this = [zeros(m,1); ones(length(selected),1)];↵if count==1↵ % if a single BENCH run↵ specs(m+1) = {getString(message('MATLAB:bench:ThisMachine', repmat(' ', 1, desclength-12)))};↵ details{m+1} = getString(message('MATLAB:bench:YourMachine', version));↵else↵ for k = m+1:size(T, 1)↵ ind = k-m; % this varies 1:length(selected)↵ sel = num2str(selected(ind));↵ specs(k) = {getString(message('MATLAB:bench:ThisMachineRunN', sel, repmat(' ', 1, desclength-18-length(sel))))}; %#ok<AGROW>↵ details{k} = getString(message('MATLAB:bench:YourMachineRunN', version, sel)); %#ok<AGROW>↵ end↵end↵scores = mean(bsxfun(@rdivide, T, meanValues), 2);↵m = size(T, 1);↵↵% Normalize by the sum of meanValues to bring the results in line with↵% earlier implementation ↵speeds = (100/sum(meanValues))./(scores);↵[speeds,k] = sort(speeds);↵specs = specs(k);↵details = details(k);↵T = T(k,:);↵this = this(k);↵↵% Horizontal bar chart. Highlight this machine with another color.↵↵clf(fig1)↵↵% Stretch the figure's width slightly to account for longer machine↵% descriptions↵units1 = get(fig1, 'Units');↵set(fig1, 'Units', 'normalized');↵pos1 = get(fig1, 'Position');↵set(fig1, 'Position', pos1 + [-0.1 -0.1 0.2 0.1]);↵set(fig1, 'Units', units1);↵↵hax2 = axes('position',[.4 .1 .5 .8],'parent',fig1);↵barh(hax2,speeds.*(1-this),'y')↵hold(hax2,'on')↵barh(hax2,speeds.*this,'m')↵set(hax2,'xlim',[0 max(speeds)+.1],'xtick',0:10:max(speeds))↵title(hax2,getString(message('MATLAB:bench:RelativeSpeed')))↵axis(hax2,[0 max(speeds)+.1 0 m+1])↵set(hax2,'ytick',1:m)↵set(hax2,'yticklabel',specs,'fontsize',9)↵set(hax2,'OuterPosition',[0 0 1 1]);↵↵% Display report in second figure↵fig2 = figure('pos',get(fig1,'pos')+[50 -150 50 0], 'menubar','none', ...↵ 'numbertitle','off','name',getString(message('MATLAB:bench:MATLABBenchmarkTimes')));↵↵% Defining layout constants - change to adjust 'look and feel'↵% The names of the tests↵TestNames = {getString(message('MATLAB:bench:LU')), ...↵ getString(message('MATLAB:bench:FFT')), ...↵ getString(message('MATLAB:bench:ODE')), ...↵ getString(message('MATLAB:bench:Sparse')), ...↵ getString(message('MATLAB:bench:twoD')), ...↵ getString(message('MATLAB:bench:threeD'))};↵↵testDatatips = {getString(message('MATLAB:bench:LUOfMatrix', problemsize(1), problemsize(1))),...↵ getString(message('MATLAB:bench:FFTOfVector', problemsize(2))),...↵ getString(message('MATLAB:bench:SolutionFromTo', problemsize(3))),...↵ getString(message('MATLAB:bench:SolvingSparseLinearSystem', problemsize(4), problemsize(4))),... ↵ getString(message('MATLAB:bench:BernsteinPolynomialGraph')),...↵ getString(message('MATLAB:bench:AnimatedLshapedMembrane'))};↵% Number of test columns↵NumTests = size(TestNames, 2);↵NumRows = m+1; % Total number of rows - header (1) + number of results (m)↵TopMargin = 0.05; % Margin between top of figure and title row↵BotMargin = 0.20; % Margin between last test row and bottom of figure↵LftMargin = 0.03; % Margin between left side of figure and Computer Name↵RgtMargin = 0.03; % Margin between last test column and right side of figure↵CNWidth = 0.40; % Width of Computer Name column↵MidMargin = 0.03; % Margin between Computer Name column and first test column↵HBetween = 0.005; % Distance between two rows of tests↵WBetween = 0.015; % Distance between two columns of tests↵% Width of each test column↵TestWidth = (1-LftMargin-CNWidth-MidMargin-RgtMargin-(NumTests-1)*WBetween)/NumTests;↵% Height of each test row↵RowHeight = (1-TopMargin-(NumRows-1)*HBetween-BotMargin)/NumRows;↵% Beginning of first test column↵BeginTestCol = LftMargin+CNWidth+MidMargin;↵% Retrieve the background color for the figure↵bc = get(fig2,'Color');↵YourMachineColor = [0 0 1];↵↵% Create headers↵↵% Computer Name column header↵uicontrol(fig2,'Style', 'text', 'Units', 'normalized', ...↵ 'Position', [LftMargin 1-TopMargin-RowHeight CNWidth RowHeight],...↵ 'String', getString(message('MATLAB:bench:LabelComputerType')), 'BackgroundColor', bc, 'Tag', 'Computer_Name','FontWeight','bold');↵↵% Test name column header↵for k=1:NumTests↵ uicontrol(fig2,'Style', 'text', 'Units', 'normalized', ...↵ 'Position', [BeginTestCol+(k-1)*(WBetween+TestWidth) 1-TopMargin-RowHeight TestWidth RowHeight],...↵ 'String', TestNames{k}, 'BackgroundColor', bc, 'Tag', TestNames{k}, 'FontWeight', 'bold', ...↵ 'TooltipString', testDatatips{k});↵end↵% For each computer↵for k=1:NumRows-1↵ VertPos = 1-TopMargin-k*(RowHeight+HBetween)-RowHeight;↵ if this(NumRows - k)↵ thecolor = YourMachineColor;↵ else↵ thecolor = [0 0 0];↵ end↵ % Computer Name row header↵ uicontrol(fig2,'Style', 'text', 'Units', 'normalized', ...↵ 'Position', [LftMargin VertPos CNWidth RowHeight],...↵ 'String', specs{NumRows-k}, 'BackgroundColor', bc, 'Tag', specs{NumRows-k},...↵ 'TooltipString', details{NumRows-k}, 'HorizontalAlignment', 'left', ...↵ 'ForegroundColor', thecolor);↵ % Test results for that computer↵ for n=1:NumTests↵ uicontrol(…'
Selection: [1 1 1 1]
SelectedText: ''
Modified: 0
Editable: 1
  2 Comments
Tord Fjordheim Onstein
Tord Fjordheim Onstein on 4 Mar 2021
Thank you Steven. I second what Mario said. This was neat. I will dig into the API and see if I am able to come up with something which satisfy my desire. I will leave the question open for another day to see if there is any more suggestions.

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!