convert_units

PURPOSE ^

CONVERT_UNITS change image data units

SYNOPSIS ^

function img = convert_units(img,new_unit)

DESCRIPTION ^

CONVERT_UNITS change image data units 
  img = convert_units(img,new_unit) converts img.elem_data or img.node_data
  expressed in img.current_physics into different units for the same 
  physical property
 
 Example: 
  img = mk_image(mdl, 2, 'resisitivity');
  img = convert_units(img, 'conductivity');

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function img = convert_units(img,new_unit)
0002 %CONVERT_UNITS change image data units
0003 %  img = convert_units(img,new_unit) converts img.elem_data or img.node_data
0004 %  expressed in img.current_physics into different units for the same
0005 %  physical property
0006 %
0007 % Example:
0008 %  img = mk_image(mdl, 2, 'resisitivity');
0009 %  img = convert_units(img, 'conductivity');
0010 
0011 % (C) 2012 Bartlomiej Grychtol, License: GPL version 2 or 3
0012 % $Id: convert_units.m 4039 2013-05-22 14:00:36Z aadler $
0013 
0014 try 
0015     cur_unit = img.current_physics;
0016 catch
0017     error('Image must contain the current_physics field');
0018 end
0019 
0020 if strcmp(cur_unit, new_unit)
0021     return %nothing to do
0022 end
0023 
0024 f = str2func(sprintf('%s2%s',cur_unit,new_unit));
0025 try
0026     img = feval(f,img);
0027 catch err
0028     if strcmp(err.identifier,'MATLAB:UndefinedFunction')
0029         error('EIDORS:ConversionNotSupported', ...
0030             'Unit conversion from %s to %s is not supported', cur_unit, new_unit);
0031     else     
0032         rethrow(err);
0033     end
0034 end
0035 end
0036 
0037 function img = resistivity2conductivity(img)
0038     img = apply_func(img,@(x)1./x);
0039     img.current_physics = 'conductivity';
0040 end
0041 
0042 function img = log_conductivity2conductivity(img)
0043     img = apply_func(img,@(x)exp(x));
0044     img.current_physics = 'conductivity';
0045 end
0046 
0047 function img = log_resistivity2conductivity(img)
0048     img = apply_func(img,@(x)exp(x));
0049     img = apply_func(img,@(x)1./x);
0050     img.current_physics = 'conductivity';
0051 end
0052 
0053 function img = conductivity2resistivity(img)
0054     img = apply_func(img,@(x)1./x);
0055     img.current_physics = 'resistivity';
0056 end
0057 
0058 function img = conductivity2log_conductivity(img)
0059     img = apply_func(img,@(x)log(x));
0060     img.current_physics = 'log_conductivity';
0061 end
0062 
0063 function img = conductivity2log_resistivity(img)
0064     img = apply_func(img,@(x)1./x);
0065     img = apply_func(img,@(x)log(x));
0066     img.current_physics = 'log_resistivity';
0067 end
0068 
0069 function img = apply_func(img,func)
0070     try 
0071         img.elem_data = feval(func,img.elem_data);
0072     catch
0073         try 
0074             img.node_data = feval(func,img.node_data); 
0075         catch
0076             error('No elem_data or node_data found');
0077         end
0078     end
0079 end

Generated on Wed 29-May-2013 17:11:47 by m2html © 2005