Removing a huge matrix after passing its local copy of the function to Matlab

Given that we have a huge matrix called A and pass it to the func (A) function, where funcI perform a set of calculations, such as:

func(A):

B=A;

%% a lot of processes will happen on B here 

return B;

end

The thing is, as soon as I pass A to B, I would have nothing to do with A in my Matlab session, so it takes up unnecessary memory space. Is it possible to delete its instance in the script area called func?

+4
source share
2 answers

Using evalin with an option caller, you can evaluate the expression clear A:

function A = func(A)
    evalin('caller', 'clear A')
    A(1) = 5;
end

, inputname, :

function A = func(A)
    name = inputname(1);
    if ~isempty(name)
        evalin('caller', ['clear ' name])
    end
    A(1)=4;
end

1.Here inputname(1) .

2. A, , A B, A.

+4

function A = func(A)
% Do lots of processing on A here

A = func(A);

MATLAB , A , , . A .

, , . . , MATLAB -.

+4

Source: https://habr.com/ru/post/1692613/


All Articles