From: Joao Henriques on
How about this:

if isempty(evalin('caller','mfilename'))
%from command line
else
%from function or script
end

It will let you see if the caller is the command line or something else.

---

I'm also a fan of Python, but when in Rome... I handle this problem "the Matlab way", so I make some effort to differentiate variable names when they will be used in the same project.

To handle different projects with overlapping names, I think most people have an "init" script in each project that calls addpath repeatedly to set up all the needed folders (I've seen many files in the File Exchange with this system). This requires some effort on the user's part (type "init" at the console), but an easy way to make it more automatic is to change the "init.m" to be something like this:

if exist('initialized_project_xyz','var')
addpath abc
addpath def
...
initialized_project_xyz = true
end

Then "init" can be called at the beginning of all your main scripts, with negligible overhead. "addpath" already checks if you're adding a path that already exists, but is considerably slower.

I'm interested in this topic, so any improvement suggestion is very welcome :)
From: Joao Henriques on
"Joao Henriques" <remove_this_part_jotaf98(a)hotmail.com> wrote in message <hqakp5$fmp$1(a)fred.mathworks.com>...
> I'm also a fan of Python, but when in Rome... I handle this problem "the Matlab way", so I make some effort to differentiate variable names when they will be used in the same project.

And by variable names, I obviously meant "function names"...

---

I also found a neat trick to group many small functions inside the same M-file, which may interest you as it allows you to reduce the number of files and thus have less folders to mess with. Put this in one M-file:

function some_module = get_some_module()
some_module.func_a = func_a;
some_module.func_b = func_b;
end

function func_a()
disp a
end

function func_b()
disp b
end

Now you can "import" all the functions in that group:
some_module = get_some_module();

And call them:
some_module.func_a()
some_module.func_b()

It's like an importable module in a single file; useful when you have a group of many small but related functions; or as a generic module if you don't mind having a rather large M-file.