How to save image stacks to a .mat file that is readable

2 views (last 30 days)
Hi,
I have 20 images that are being stacked (512x512x20) where all the information is in gray scale intensities, and I want the actual data saved into a .mat file. But the file it should be writing into is blank (absolutely no text or numbers). How can I accomplish this as this is pre-processing for a different program? My input file format is a 262144x2 matrix, where I only want the second column of information. I know the stack is being created correctly because I can use the implay function ( implay(ImageStack) ) and see the images.
The file written is as follows (after several prompts of FileSave, i, and j):
ImageStack = zeros(512,512,j-i); %%initiates image stack
FS = fopen(FileSave,'a+'); %%open file for writing
for k = i:j
FileName = ['BC_' num2str(k) '_1.txt']; %%open textfile
A = load (FileName); %%load file to matrix A
B = A(:,2); %%Takes only one column from Matrix A into Matrix B
C = reshape(B,512,512)'; %%reshapes Matrix B (262144x1) to Matrix C (512x512)
I = mat2gray(C); %%converts pixel intensity to grays cale (0-1)
ImageStack(:,:,k) = I; %%places image into to stack
fwrite(FS,ImageStack); %%saves file for reading
end
I've tried the save function, but it only saves the variables (i,j,FileName, etc..) Not the actual data within the files. How do I save the actual data to a .mat file? The file File specified is blank after save; and after using the save function it only returns the variables, not the data. I'm using version R2014a.
Thanks

Accepted Answer

Image Analyst
Image Analyst on 20 Aug 2014
Get rid of the fopen() and fwrite(). Just use save():
After your for loop (where you create ImageStack), just write ImageStack out all in one line:
fullFileName = fullfile(folder, 'ImageStack.mat');
save(fullFileName, 'ImageStack');
% Note: save() overwrites/destroys any existing file (does not "add/append").

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!