David
David

Reputation: 1694

Shortcut for saving file in current directory with correct name?

Is there any shortcut for saving an open file into the current directory with the current name? If I download multiple files with the same name they go into my Downloads folder and they end up with names like function (1).m instead of function.m.

At this point it's easy to open the file and see the contents by opening the file from my web browser- MATLAB sees the file extension and opens it. However, if it's a function, I have to Save As and move/rename the file before I'm able to use the code.

Edit: Since MATLAB insists that the file name be the same as the function name I'm hoping that there's a shortcut that saves an open file directly to the current MATLAB path and names it appropriately.

Since MATLAB convention is that the file name is the same as the function name I'm hoping that there's a shortcut that saves an open file directly to the current MATLAB path and names it according to the function name specified in the file.

Upvotes: 1

Views: 312

Answers (1)

Wolfie
Wolfie

Reputation: 30046

You will need a directory which is always on the Matlab path. This can be achieved by adding one in the startup.m script.

Then you should save the below function savefunc.m to that directory, so you can always call it.

function savefunc(FuncName, Directory)
    % Set directory if not given, default is working directory
    if nargin < 2; Directory = pwd; end
    % Get active document
    ActiveDoc = matlab.desktop.editor.getActive;
    % Set FuncName if not given, or if FuncName was empty string
    if nargin < 1 || strcmp(FuncName, '');
        % Get function name
        FuncName = ActiveDoc.Text;          % Entire text of document
        % Could use indexing to only take first n characters, 
        %   and not load entire string of long function into variable. 
        %   FuncName = ActiveDoc.Text(1:min(numel(ActiveDoc.Text), n));  
        FuncName = strsplit(FuncName, '('); % Split on parenthesis
        FuncName = FuncName{1};             % First item is "function [a,b,c] = myFunc"
        FuncName = strsplit(FuncName, ' '); % Split on space (functions don't always have equals)
        FuncName = strtrim(FuncName{end});  % Last item (without whitespace) is "myFunc"
    end
    % Save the file
    saveAs(ActiveDoc, fullfile(Directory, [FuncName, '.m']));    
end

Now, say you have just created the following Untitled.m:

function [a,b,c] = mytest()
    a = 1; b = 1; c = 1;
end

The shortcut: just have Untitled.m open, and type into the Command Window

savefunc()

Untitled.m will be saved as mytest.m in the current working directory. Notice you can also pass a different function name and save-as directory if you wish, making this useful for other occasions.

You can pass an empty string as the FuncName if you want to specify the directory but use the auto-naming.


You could extend this using matlab.desktop.editor.getAll to get all open documents, then loop through them for saving.

For more info, type help matlab.desktop.editor in the Command Window, online documentation seems lacking.

matlab.desktop.editor: Programmatically access the MATLAB Editor to open, change, save, or close documents.


Finally, note there is no need for a "Save successful" type message, since you will see the file name change, and also receive an error if it fails (from the saveAs docs):

If any error occurs during the saveAs operation, MATLAB throws a MATLAB:Editor:Document:SaveAsFailed exception. If the operation returns without an exception being thrown, assume that the operation succeeded.

Upvotes: 2

Related Questions