How to interrupt MATLAB IDE when it hangs on displaying very large array?
Asked Answered
A

2

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.

Ayotte answered 2/8, 2012 at 14:24 Comment(2)
use commandline matlab, so you can't click on the matrices :pYingyingkow
Yeah, I suppose that behavior modification would be useful, but I do like the IDEAyotte
Y
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...

Yingyingkow answered 2/8, 2012 at 16:14 Comment(3)
I'm adding these links for future reference: #6409259 and #6201772 which has the same solution altough it's for a build in functionYingyingkow
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!Ayotte
@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)Yingyingkow
S
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.

Spec answered 2/8, 2012 at 14:42 Comment(10)
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.Ayotte
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!"Yingyingkow
@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.Ayotte
@DanielKessler I'm looking for that right now, should be possible imoYingyingkow
@GuntherStruyf there was a post on undocumentedmatlab.com describing how to do it. I'll see if I can find it.Spec
@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).Geologize
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'Yingyingkow
@GuntherStruyf I figured it out. I'll post it as a separate question and answer and then link to here.Spec
see this for how I did it #11782134Spec
Thanks for the link @slayton! Looks like both helpful question and answers there (both upvoted).Ayotte
Y
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...

Yingyingkow answered 2/8, 2012 at 16:14 Comment(3)
I'm adding these links for future reference: #6409259 and #6201772 which has the same solution altough it's for a build in functionYingyingkow
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!Ayotte
@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)Yingyingkow

© 2022 - 2024 — McMap. All rights reserved.