function Tconvert(h,ID,val)
%TCONVERT
%
% Tconvert(h,ID,val)
%
% Performs unit conversions for converttemp GUI. 
% h is the matrix of handles to the UIcontrosls.
% val is the temperature entered by  the user,
% andID indicates the units of val:
%
%    1=Kelvin
%    2=Celsius
%    3=Fahrenheit
%
%Converts val to the other two temperature units, and updates
%the edits and sliders for those units
%

%The temperature units are all related linearly.  This is
%described by 2 3-by-3 matrices M (slopes) and B (intercepts).
%To convert from units indicated by ID to units j:
%
%    val_in_j_units=M(j,ID)*val+B(j,ID);
%
M=ones(3,3);
M(1:2,3)=1/1.8;
M(3,1:2)=1.8;

B=[0      273.15  255.3722;...
  -273.15    0    -17.7779;...
  -459.67   32        0    ];

%units to update (j's in example above)
%
% If ID=1, then updateID=[2 3]
% If ID=2, then updateID=[3 1]
% If ID=3, then updateID=[1 2]
%
% The correct ID's can be computed with mod function:
updateID=ID+(1:2);
updateID=mod(updateID-1,3)+1;%columns to update

updatevals=M(updateID,ID)*val+B(updateID,ID);%new values

%set the values and strings of the sliders and edits respectively
for j=1:2;
  set(h(3,updateID(j)),'value',updatevals(j)); %slider
  set(h(2,updateID(j)),'string',num2str(updatevals(j)));%edit
end
