2

Suppose I'm using the MATLAB IDE and happen to have some very large objects in my workspace (e.g. arrays of 500k+ elements). Now, suppose that I stupidly and accidentally double click on one of these very large variables, which triggers a load to the array editor. Unfortunately, with arrays this big, MATLAB just hangs.

I've tried CTRL+C, CTRL+BREAK, CTRL+D, but none seem able to interrupt the behavior of the IDE. I know I can force matlab to quit, but reading all of those variables into the workspace in the first place takes a lot of time, and I may have unsaved changes in an editor window, etc.

Daniel Kessler
  • 1,172
  • 17
  • 21

2 Answers2

5

The variable editor is launched using the command openvar. To solve your problem you can take advantage of a Matlab quirk that causes functions to be masked by variables with the same name. For example if you create a variable named plot the plot() function stops working.

The solution, although hackish, is to simply create an empty variable named openvar. Then anytime attempt to open the variable editor will fail because the function openvar is being hidden by the variable.

If you want to use the variable editor again simple call clear openvar to delete the variable and the function will be unmasked.

slayton
  • 20,123
  • 10
  • 60
  • 89
  • So the idea is to basically make it harder to open the variable editor in the first place, thus preventing a single double click from crashing things down? Clever approach :), worked for me. – Daniel Kessler Aug 02 '12 at 14:44
  • 7
    why not write another openvar function (your own) that wraps the real openvar function. This way you can add a check in there for large matrices and ask the user "are you sure? this is gonna take a while!" – Gunther Struyf Aug 02 '12 at 14:44
  • @GuntherStruyf If I want my custom openvar function to run several checks, and if it passes them, then call the default openvar, how do I go about the call so that it accesses the built-in openvar, rather than getting stuck in a loop. – Daniel Kessler Aug 02 '12 at 14:51
  • @DanielKessler I'm looking for that right now, should be possible imo – Gunther Struyf Aug 02 '12 at 14:55
  • @GuntherStruyf there was a post on http://undocumentedmatlab.com/ describing how to do it. I'll see if I can find it. – slayton Aug 02 '12 at 15:01
  • 2
    @DanielKessler: the easiest solution most likely is to simply copy the contents of `openvar` into your modified function (`edit openvar`, the rest should be obvious). – Jonas Aug 02 '12 at 15:53
  • 1
    If it was builtin, things would be easier, tried a lot of things, but next to this problem, `openvar` can't find the original variable when called not from the commandline. So best solution here imo is to copy everything out of the original `openvar.m` to your new file.. Anyways, feature request pending: 'calling any shadowed function' – Gunther Struyf Aug 02 '12 at 15:53
  • @GuntherStruyf I figured it out. I'll post it as a separate question and answer and then link to here. – slayton Aug 02 '12 at 16:11
  • 2
    see this for how I did it http://stackoverflow.com/questions/11781634/how-to-wrap-a-built-in-funciton-with-a-new-function-of-the-same-name – slayton Aug 02 '12 at 16:30
  • Thanks for the link @slayton! Looks like both helpful question and answers there (both upvoted). – Daniel Kessler Aug 02 '12 at 19:32
2

I found a way, but it's not the best, it requires a change of path and back once to get a handle to the original openvar

function openvar(name,array)
    persistent org_openvar
    if isempty(org_openvar)
        curdir=pwd;
        cd(fullfile(matlabroot,'toolbox/matlab/codetools'));
        org_openvar = @openvar;
        cd(curdir);
    end

    if numel(array)>1e5
        if strcmp(questdlg(sprintf('Opening ''%s'' which has %d elements.\n\nAre you sure? This is gonna take a while!',name,numel(array)), ...
        'Variable editor','Yes','Cancel','Cancel') , 'Yes')
                org_openvar(name,array)
            end
    else
        org_openvar(name,array)
    end
end

getting that handle is the biggest problem, calling it is just fine. If openvar would be built in, you could use the function builtin:

builtin('openvar',name,array)

but this is unfortunately not the case :(
str2func in combination with the complete path also doesn't work, at least I don't get it to work...

Gunther Struyf
  • 11,158
  • 2
  • 34
  • 58
  • 1
    I'm adding these links for future reference: http://stackoverflow.com/questions/6408759/avoid-matlab-startup-warning-when-overloading-buildin-functions and http://stackoverflow.com/questions/6201272/how-to-avoid-matlab-crash-when-opening-too-many-figures which has the same solution altough it's for a build in function – Gunther Struyf Aug 03 '12 at 09:26
  • 2
    Finally went to test your solution. Nice work! I suggested a small edit so that you build up fullfile in line five using no slashes, otherwise it won't work on Linux systems (where I tested it). This will definitely save me some lost matlab sessions! – Daniel Kessler Aug 28 '12 at 14:57
  • 1
    @DanielKessler You're right, I once looked in `fullfile` and noticed the `f = strrep(f,'/','\');` call at the bottom, but from then on I only remembered there was a strrep, not that that line only fixes things from unix to pc. Thanks for making me fix this! I rely too much on `fullfile` for these kind of things and also use both unix and windows :p (It seems your edited was wrongfully rejected, fixed it myself) – Gunther Struyf Sep 05 '12 at 20:36