function [CombinedData,legendString]=CombineChannels(h,rectStruc,mapto)

%Paolo Antonelli
%Fri Mar 30 18:32:42 UTC 2001
%updated by Kevin Baggett
%Thu Jul 26 20:00:03 UTC 2001

if nargin<1
  fullImage=1;
else
  fullImage=0;
end
clear channels

switch mapto
  case 'x'
      stChn=1;
  case 'y'
      stChn=5;
  case 'r'
      stChn=1;
  case 'g'
      stChn=5;
  case 'b'
      stChn=9;
end


i=stChn;
j=1;
disp('Selected Channels');
while strcmp(get(h.EditText(i),'Enable'),'on')
  channels(j)= str2num(get(h.EditText(i),'String'));
  if channels(j)> 0 | channels(j)<35
     disp(channels(j))
  else
     disp(['... ' channels(j) ' ... Bad selection'])
     return
  end
  i=i+1;
  j=j+1;
end
  
if FileName(4:5)=='02'
  for i=1:length(channels)
     [data] = modis_level1b_read([h.PathName h.FileName],channels(i));
     if fullImage
       chn=data.mimage;
     else
       chn=data.mimage(rectStruc.x1:rectStruc.x2,rectStruc.y1:rectStruc.y2);
     end 
     [nrows,ncols]=size(chn)
     sdata.ch(i,:)=reshape(chn,1,nrows*ncols);
     if i==1
       strg=['(sdata.ch(' num2str(i) ',:) '];
       legendString=['(Ch ' num2str(i) ];
     elseif i==3
       if popupstr(h.popup(i-1))=='*' | popupstr(h.popup(i-1))=='/'
         strg=[strg '.' popupstr(h.popup(i-1)) '(sdata.ch(' num2str(i) ',:) '];
       else
         strg=[strg popupstr(h.popup(i-1)) '(sdata.ch(' num2str(i) ',:) '];
       end
       legendString=[legendString popupstr(h.popup(i-1)) '(Ch ' num2str(i) ];
     else
       if popupstr(h.popup(i-1))=='*' | popupstr(h.popup(i-1))=='/'
         strg=[strg '.' popupstr(h.popup(i-1)) 'sdata.ch(' num2str(i) ',:)'];
       else
         strg=[strg popupstr(h.popup(i-1)) 'sdata.ch(' num2str(i) ',:) '];
       end
       legendString=[legendString popupstr(h.popup(i-1)) 'Ch ' num2str(i) ];
     end
     clear chn
  end
  strg=[strg ')'];
  legendString=[legendString ')'];
  CombinedData=eval(strg);
elseif FileName(4:5)=='03'
  %insert code for 03 here
end


return
