Access to the fits file channels

Hello Folks,
I have this fits file of size(300,300,3) I would like to acces to each channel alone and write it. Using this code I can access only to the first channel but not to the second and third one. Any idea please.
stokes = fitsread('stokes1.fits');
Iu = stokes(:,:,1);
Ip = stokes(:,:,2);
Theta = stokes(:,:,3);
fitswrite([Iu,Ip,Theta],'stokes.fits')

 Accepted Answer

info = fitsinfo('stokes1.fits');
Size = info.PrimaryData.Size;
nchan = Size(3);
parts = cell(1,nchan);
for channel = 1 : nchan
parts{channel} = fitsread(info, 'pixelregion', {[1 Size(1)], [1 Size(2)], [channel channel]})'
end

4 Comments

Thank you for your feedback, but I have an error:
Expected FILENAME to be one of these types:
char, string
Instead its type was struct.
Error in fitsinfo (line 212)
fid = openFile(filename);
Error in fitsread (line 103)
info = fitsinfo(filename);
Error in PSNR_SSIM (line 18)
parts{channel} = fitsread(info, 'pixelregion', {[1 Size(1)], [1 Size(2)], [channel channel]})'
I add a line to your code and it's working now.
for the moment I would like to write and visualize each channel . I used this line but it writes for me only the last channel not the two previous one also.
stokes = fitsread('StokesParameters_Synthetic_Star_Gain_10.fits');
info = fitsinfo('StokesParameters_Synthetic_Star_Gain_10.fits')
Size = info.PrimaryData.Size
nchan = Size(3)
parts = cell(1,nchan);
for channel = 1 : nchan
parts{channel} = fitsread('StokesParameters_Synthetic_Star_Gain_10.fits','PixelRegion', {[1 Size(1)], [1 Size(2)], [channel channel]})
end
fitswrite(parts{channel},'stokes5.fits','Compression','rice')
I had misread about the option to use an info structure instead of a file name.
As usual I recommend making the filename a variable.
fitsfile = 'StokesParameters_Synthetic_Star_Gain_10.fits';
info = fitsinfo(fitsfile);
Size = info.PrimaryData.Size;
nchan = Size(3);
parts = cell(1,nchan);
for channel = 1 : nchan
parts{channel} = fitsread(fitsfile,'PixelRegion', {[1 Size(1)], [1 Size(2)], [channel channel]});
end
outdata = horzcat(parts{:});
fitswrite(outdata, 'stokes5.fits', 'Compression', 'rice');
Thank you for your feedback.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!