1

sometimes I load a .m file to work in the workspace, but I usually forget what is the recent file I´ve opened. So in the same way that you write who and you can see the variables of the workspace I suppose there should be a command to know what is the .m file in which you are working. Does anyone know a command like this? Thank you so much.

1

3 Answers 3

2

For newer MATLAB versions, there is a way to get the fullpath of the file currently being edited in the Editor:

if verLessThan('matlab', '7.10')
    %# not supported
    fname = '';
elseif verLessThan('matlab', '7.12')
    %# R2010a,R2010b: editorservices
    fname = editorservices.getActiveFilename;
else
    %# R2011a: matlab.desktop.editor API
    fname = matlab.desktop.editor.getActiveFilename;
end
1

mfilename seemed to be the right choice, but it returns an empty string when called from the command line. Therefore you may check the 'command history' provided by the IDE.

For larger projects it most often makes sense to use MATLAB's object model or at least functions in order to structure your work. Working in the 'workspace' often results in unwanted side effects.

1

I do something like this to bootstrap runs:

%%
params.run = 'hairy.m';
params.hair = 10;

setup_defaults(params);

run_lots_of_code(params);

This has advantages as it hides the 'globals' in a single global (params), and the global tells you where they came from.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.