renaming large variables programmatically

15 Ansichten (letzte 30 Tage)
Jay Kirsch
Jay Kirsch am 20 Sep. 2011
Kommentiert: Steven Lord am 4 Nov. 2016
Hello,
I would like to rename a variable programmatically, and without copying it.
I work with large audio files, so "y=x; clear x" may blow up the memory.
I would like to do this within a program instead of having to click in the Workspace.
Saving it to a binary file, clearing the variable, then reading and deleting the binary file is a bit of a hack, so I'd rather do something more elegant.
Thanx in advance!

Akzeptierte Antwort

Jan
Jan am 20 Sep. 2011
Are you sure that "y=x" creates a copy? In all cases I've tested, a shared dat copy is created, which creates just about 100 bytes overhead to the header of the variable. Try this:
format debug
x = 1:100
y = x
clear('x');
The shown "pr=xyz" is the pointer to the data array. It is identical for x and y, because both point to the same data. Only this would create a deep copy of the data:
x = 1:100
y = x % x and y use the same data
y(2) = 27; % now x and y must be different
clear('x');
But:
x = 1:100
y = x % x and y use the same data
clear('x');
y(2) = 27; % y still points to the same data as x did
This means that renaming a large array works directly without any tricks as long as the original is cleared before any values are changed.
But: I cannot imagine a situation which demands for renaming a variable! It is only a name, so what might be the advantage of using another one??? If it concerns EVAL, EVALIN or ASSIGNIN: Do not use them if you want to work efficiently with large arrays! If it concerns LOAD and SAVE, there are smarter solutions by using LOAD with an output argument instead of writing directly to the workspace.

Weitere Antworten (2)

Sean de Wolski
Sean de Wolski am 20 Sep. 2011
You could try doing it in chunks though there's no guarantee that'll be faster. You could also convert to single precision if necessary.
Loading the variable and immediately assigning it a name on load is probably your best bet.

Matlabber 1.0
Matlabber 1.0 am 4 Nov. 2016
Dear all, to pick up on exact the same topic abouth "why renaming??": i have a few large chunks of image data, each set has a different amount of images. Imagine a the biggest set of 2000 images of 1200x2000 pixel, resulting in ~35gb with double representation (required).
since for processing these data i cannot load it into memory at once (16gb machine), i slice it up in ~4gb chunks and save them to disk. here some pseudocode:
for i=1:number_of_4gb_chunks_required % iterate the 4gb chunks
Chunk=zeros(SizeOf4gbChunk); % initialize 4gb chunk
for j=[ImageNumbersThatGoToChunk_i] % iterate images
ImData=ReadInFileNr_i();
PutImDataintoChunk()
end
renameChunkToChunk_i
save(Chunk_i.mat,Chunk_i) % save the current block as Chunk_i.mat
end
later i want to load any of those block programmatically and apply repetitive image processing on them. How do you recommend me to do this? My current solution involves eval(). i would like to get rid of it but i don't know how. can you help me? thanks!

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by