From: Mohammed on
> As you know, if you've been reading this group for any length of time, using
> that construct is strongly discouraged (see question 4.6 in the newsgroup
> FAQ if you haven't been reading the group for an explanation as to why it's
> discouraged) but in this case we can get around that. Note that in the code
> below I'm assuming all the x* matrices are the same size and that you know
> that size ahead of time.
>
> % Get the list of files
> D = dir('x*.mat');
>
> % Assume all the x's are 100-by-200
> rowsInX = 100;
> colsInX = 200;
>
> % Preallocate
> storedData = zeros(rowsInX, colsInX, numel(D));
>
> for k = 1:numel(D)
> % x* is a field of dataFromFile
> dataFromFile = load(D(k).name);
>
> % strip off the .mat extension to get the name of the variable
> fieldname = D(k).name(1:end-4);
>
> % use dynamic field name indexing to refer to the variable/field
> storedData(:, :, k) = dataFromFile.(fieldname);
> end
>
> If they're not the same size, use a cell array.
>
> storedData = cell(1, numel(D));
>
> storedData{k} = dataFromFile.(fieldname);
>
> --
> Steve Lord
> slord(a)mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>

A big thanks to everyone for the help and suggestions. I believe I have what I need now, here is my code:
=========================================
% Get the list of files
D = dir('d6000_c_2_*.TIF');

% variables
rowsInX = 256;
colsInX = 320;
N_Images = 383;

% Preallocate
storedData = zeros(rowsInX*N_Images,colsInX);

for k = 1:numel(D)
dataFromFile = imread(D(k).name);
dataFromFile = dataFromFile(:,:,1:1);
% strip off the .TIF extension to get the name of the variable
fieldname = D(k).name(1:end-4);

storedData(k*256-255:k*256,:) = dataFromFile;
end
=============================================
From: James Tursa on
"Mohammed " <mohammed98221(a)gmail.com> wrote in message <hrcf29$3d$1(a)fred.mathworks.com>...
>
> A big thanks to everyone for the help and suggestions. I believe I have what I need now, here is my code:
> =========================================
> % Get the list of files
> D = dir('d6000_c_2_*.TIF');
>
> % variables
> rowsInX = 256;
> colsInX = 320;
> N_Images = 383;
>
> % Preallocate
> storedData = zeros(rowsInX*N_Images,colsInX);
>
> for k = 1:numel(D)
> dataFromFile = imread(D(k).name);
> dataFromFile = dataFromFile(:,:,1:1);

I would suggest deleting the above line. It simply forces you to copy a slice of memory from dataFromFile into new memory, which you later then copy again anyway.

> % strip off the .TIF extension to get the name of the variable
> fieldname = D(k).name(1:end-4);
>
> storedData(k*256-255:k*256,:) = dataFromFile;

So delete the line mentioned earlier and then change the above line to:

storedData(k*256-255:k*256,:) = dataFromFile(:,:,1:1);

That will eliminate one unnecessary copy of your data.

I will add that for your basic image size of 256 x 320 this will probably not make much of a difference in speed or memory use, but if you use this technique later on with much larger images it could make a worthwhile difference.

James Tursa
From: James Tursa on
"James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <hrcm9b$t6u$1(a)fred.mathworks.com>...
> > dataFromFile = dataFromFile(:,:,1:1);
>
> I would suggest deleting the above line. It simply forces you to copy a slice of memory from dataFromFile into new memory, which you later then copy again anyway.
> >
> > storedData(k*256-255:k*256,:) = dataFromFile;
>
> ... and then change the above line to:
>
> storedData(k*256-255:k*256,:) = dataFromFile(:,:,1:1);
>
> That will eliminate one unnecessary copy of your data.

I am retracting that statement. I was thinking that the MATLAB accelerator made this optimization when doing assignments but I just ran some tests and it doesn't. You still get the two copies either way you do it. You would have to resort to a mex routine to avoid it.

James Tursa