-1

I am trying to read a raw file in Matlab (float64, which is a deformation vector field, i.e. result of image registration), with 3 dimensions 304 x 224 x 52.

Then I want to change all the values within this file by dividing them by 10.

After that I want to save the modified file again as a raw file with the same specifications. I wrote a code but I am not able to save the file. I want to save it again in raw format. I think I might be missing something. I am a beginner in Matlab so I would appreciate your help and patience. Thank you.

fid = fopen('I:\PatientData\patient1\out_2_to_1_us\deformationField_test.raw') dvf =     fread(fid);

length(div)
div = (0.1) * ones(42491904,1); dvf_cm = dvf.* div;

count = fwrite(fid,dvf_cm,'float64'); 
fclose(fid);
tmlen
  • 8,533
  • 5
  • 31
  • 84
Hoda
  • 5
  • 5

1 Answers1

0

You need to open the file in read/write mode and rewind it after reading:

fid   = fopen('I:\PatientData\patient1\out_2_to_1_us\deformationField_test.raw', 'r+');
data  = fread(fid, Inf, 'float64')/10;
        frewind(fid);
count = fwrite(fid, data, 'float64'); 
        fclose(fid);
  • I changed the input file into a 3D matrix using "reshape". Is this correct? – Hoda Feb 12 '16 at 16:25
  • @Hoda The shape of the data in memory has no effect on how the data is written on the file. No matter what shape you will give the data, it will be written in the linear index order. The linear indexing is independent of the shape of your data (i.e. the subscript indexing). http://www.mathworks.com/help/matlab/math/matrix-indexing.html The code that I wrote should work well for the purpose that you stated in your question. –  Feb 12 '16 at 16:32
  • Thank you very much this was real helpful. I appreciate your help. – Hoda Feb 12 '16 at 16:44
  • @Hoda Glad that I could help. –  Feb 12 '16 at 16:47