Code covered by the BSD License  

Highlights from
uhelp

image thumbnail

uhelp

by

 

display a function's HELP or a CELL array in text window

uhelp.m
% uhelp: display a function's HELP or a CELL array in text window.
% uhelp might be useful as a helper function for guis/toolboxes
%
% 
% FEATURES:
% HTML-style to display text (words in uppercase and the name of the
%    function within the text appear highlighted
% use mouse wheel to scroll up/down the text
% use mouse wheel + control button to change the fontsize
% change fontstyle and backgroundcolor
% set window to 'always on top' mode ([aot.m] required from matlab file exchange)
% multiple window mode possible 
% save fontstyle/color/windowsize parameter within this function for the
%  next call
% 
%SYNTAX:   uhelp(function) ;%one window mode
%          uhelp(cell)
%          uhelp(function,1) ;%multiple windows mode
%          uhelp(cell,1)
%          
% DESCRIPTION:     
% uhelp('regexprep');   %replace window mode (default)
% uhelp('regexprep',1) ;%multiple windows mode
% [mousewheel up/down]:             scroll text
% [control]+ [mousewheel up/down]:  decrease/increase fontsize
% [arrow left]/[arrow right]:       decrease/increase fontsize
% 
% checkbox    :  to set window as 'always on top'
% contextmenu : [font]      -> choose font-size/weight/style from menu
%               [backgroundcolor]
%               [saveconfig]-> save following parameter in THIS matlab-function:
%                    - font-size/weight/style
%                    - used  'always on top' mode
%                    - figure position
%               [copy selection]-> copy selection to clipboard
%               [evaluate selection]-> ..
% help         : opens another textwindow (replace window mode) to depict help                          
% 
%EXAMPLES:
% uhelp uhelp
% uhelp('fft')
% 
%NOTE: the following file is required
%  FINDOBJ by Yair Altman -->   
%  http://www.mathworks.com/matlabcentral/fileexchange/14317-findjobj-find-java-handles-of-matlab-graphic-objects
% 
% optional: to set window to 'allways on top' download AOT --> 
% http://www.mathworks.com/matlabcentral/fileexchange/20694-aot
% --------------------------------------------------
% paul koch,BNIC Berlin 2010



function uhelp(fun,varargin)

if length(varargin)==0 %dirty: 2 inputs means 'multiple windows mode'
    try; close(335);end %otherwise replace an existing window
end

if ~iscell(fun) %call help of a function
    e=help(fun);
    e2={};
    ix=strfind(e,char(10));%replace Carriage return
    ix=[1 ix length(e)];
    for i=1:length(ix)-1 %make a cell
        e2(i,1)={e(ix(i):ix(i+1)-1)};
    end
else %CELL as input
    e2=fun;
end

%%%
x.aot=0;
x.evntModifier=0;
%%%


e0=e2;%backup
e2{1}=[ '<html><b><font  color="navy"> '  e2{1}]; %colorcode first line
 

% s1=[1 regexp(str, ['\s']) ]
% bol=[]
% for i=1:length(s1)-1
%    v= str(s1(i) :s1(i+1)-1)
%    if strcmp(upper(v),v)
%        bol(end+1,:)=[s1(i) s1(i+1)-1 ]
%    end
% end
%    


%colorcode
try
     try
         cap1=fun(1):strfind(fun,'.')-1;
     catch
         cap1=fun;
     end
     cap2=['<html><b><font color="navy">' upper(cap1) ' </b>' '</font>'];
     for i=2:length(e2)
         e2{i,1}= [ '<html> '   e2{i} ' </font>' ];
         e2(i)= regexprep(e2(i),cap1,cap2,'ignorecase');      
     end
%      e2{end+1,1}='<html><a  href="matlab:axl@uxl.de"> axl@uxl.de </a> ' ;%'<a href="matlab:magic(4)">Generate magic square</a>'
end
 
try
    for j=2:length(e2)
        str=e2{j};
        s1=[1 regexp(str, [ '\s']) ];          %['\s']
        bol={};
        for i=1:length(s1)-1
            v= str(s1(i) :s1(i+1) );
%             v=regexprep(v,'[,.]','');
%             v=regexprep(v,'[\[\]]','');
            if strcmp(upper(v),v) & length(regexp(v, '[A-Za-z]'))>=2 & isempty(regexp(v, '[.]'))...
                    &  isempty(regexp(v,fun))
                bol{end+1,1}=str([s1(i)+1:s1(i+1)-1 ]);
            end
        end

        dum=str;
        for i=1:length(bol)
            dum= regexprep(dum,bol{i},...
                ['<html><b><U><font color="maroon">'  bol{i}  '</b></U>' '</font>' ],...
                'ignorecase','once');
        end
        %     char(dum)
        e2{j}=dum;

    end
end



 if length(varargin)==0
     figure(335);
 else
     figure;
 end
 set(gcf,'tag','uhelp','color',[1 1 1]);
set(gcf,'units','normalized','menubar','none',...
    'WindowScrollWheelFcn',{@gwheel,1});
ui=uicontrol('Style','listbox','units','normalized', 'Position',[0 0,1 1],...
    'String', ...
    e2, 'tag','txt','max',2,...
    'backgroundcolor',[1 1 1],'fontsize',8);%,'fontname','Courier New');




try
  x=setup(x);
  x.fun=fun;
  x.e0=e0;
    
  set(ui,'FontName',  x.FontName,'FontAngle',x.FontAngle,...  
         'FontWeight',x.FontWeight,'fontsize',x.fontsize,...
         'backgroundcolor',x.colbg);
  set(gcf,'position',x.fgpos);    
end
drawnow;
try;     aot(x.aot);aot(x.aot);  end %AOT
set(gcf,'toolbar','none');
%----------------------------------
%           AOT-button
%----------------------------------
b1 = uicontrol('Style', 'checkbox', 'String', '','fontsize',8,...%'pushbutton'
    'units','normalized','TooltipString','toggle: alway on top',...
    'Position', [0 0 .05 .05], 'Callback', @ontop);
if x.aot==1; set(b1,'value',1); else; set(b1,'value',0);end

%----------------------------------
%         context
%----------------------------------
cmenu = uicontextmenu;
set(ui,'UIContextMenu', cmenu);
item1 = uimenu(cmenu, 'Label','font',     'Callback', {@gcontext, 1});
item2 = uimenu(cmenu, 'Label','backgroundcolor', 'Callback', {@gcontext, 2});
item3 = uimenu(cmenu, 'Label','saveConfig', 'Callback', {@gcontext, 3});
item4 = uimenu(cmenu, 'Label','copy  selection', 'Callback', {@gcontext, 4});%copy
item5 = uimenu(cmenu, 'Label','evaluate selection', 'Callback', {@gcontext, 5});% evaluate
item6 = uimenu(cmenu, 'Label','help',   'Callback', {@gcontext, 6});
%----------------------------------
%           keys
%----------------------------------
set(gcf,'KeyPressFcn',@fontsize); %figure and..
set(ui,'KeyPressFcn',@fontsize); %listbox is keysensitive
%----------------------------------
%           scroll: 
%here use findjobj
%----------------------------------
x.scroll=findjobj(gcf,'Class', 'UIScrollPane$1');%scroll
set(gcf,'userdata',x);


%%%
% embedded functions
%%%
%----------------------------------
%         AOT/ontop
% here use aot
%----------------------------------
function ontop(src,evnt)
    
if exist('aot.m')==0;%if aot.m is not in the matlabpath
    disp('[figure on top] is enabled, download and add [aot.m] to the matlabpath');
    disp('http://www.mathworks.com/matlabcentral/fileexchange/20694-aot');
else
    x=get(gcf,'userdata');
    if x.aot==0
        try; x.aot=1; aot(1);end;  set(gcf,'menubar','none','toolbar','none');
    else
        try; x.aot=0; aot(0);end;  set(gcf,'menubar','none','toolbar','none');

    end
    set(gcf,'userdata',x);
end



%----------------------------------
%           arrow keys
%----------------------------------
function fontsize(src,evnt)
x=get(gcf,'userdata');
 if strcmp(evnt.Modifier,'control'); %[control]+mousewheel
      x.evntModifier=1; %if [control]-key is pressed..
             %code this for mousewheel-specifications
 else  
     x.evntModifier=0;
     tx= findobj(gcf,'tag','txt');
     if strcmp(evnt.Key,'leftarrow')
         set(tx,'fontsize', get(tx,'fontsize')-1)  ;
     elseif strcmp(evnt.Key, 'rightarrow')
         set(tx,'fontsize', get(tx,'fontsize')+1)  ;
     end
 end
 set(gcf,'userdata',x);

%----------------------------------
%           mousewheel
%----------------------------------
function gwheel(obj, event, mode)

x=get(gcf,'userdata');
if  x.evntModifier==0 %[scroll text]
    x=get(gcf,'userdata'); scroll=x.scroll;
    set(scroll,'value',   get(scroll,'value')    +event.VerticalScrollCount*25     );
 
else %define fontsize with mouse wheel
    tx= findobj(gcf,'tag','txt');
    
    fs= get(tx,'fontsize');
    fs2=fs+event.VerticalScrollCount;
    if fs2>1
        set(tx,'fontsize', fs2)  ;
    end
        x.evntModifier=0;
        set(gcf,'userdata',x);
end



%----------------------------------
%           contextmenu
%----------------------------------
function gcontext(obj, event, mode)
x= get(gcf,'userdata');
if mode==1 
    tx= findobj(gcf,'tag','txt');
    r.FontName=get(tx,'FontName');
    r.FontWeight=get(tx,'FontWeight');
    r.FontAngle=get(tx,'FontAngle');
    r.FontSize=get(tx,'fontsize');
    r.FontUnits= 'points' ;
    
    try;aot(0);end  
    set(gcf,'menubar','none','toolbar','none');
     ix= uisetfont(r );
    set(tx,ix);drawnow;
elseif mode==2
    p=colorui;
    tx= findobj(gcf,'tag','txt');
    set(tx,'backgroundcolor',p);
elseif mode==3
    tx= findobj(gcf,'tag','txt');
    s.colbg=get(tx,'backgroundcolor');
    s.fontsize=get(tx,'fontsize');
    s.FontName=get(tx,'FontName');
    s.FontAngle=get(tx,'FontAngle');
    s.FontWeight=get(tx,'FontWeight');
    s.fgpos=get(gcf,'position');
    x=get(gcf,'userdata') ;
    s.aot=x.aot;
    
    fi=which('uhelp.m');     
    fil={};
    fid=fopen(fi);    j=1;
    while 1
        tline = fgetl(fid);
        if ~ischar(tline), break, end
        fil{j,1}=tline;     j=j+1;
    end
    fclose(fid);
        
    for i=1:length(fil)
        if ~isempty( findstr(fil{i},'%***setup-start'))
            w1=i;
        end
        if ~isempty( findstr(fil{i},'%***setup-end'))
            w2=i;
        end
    end
    
    fil1=fil(1:w1);
    fil2=fil(w2:end);
    v=fieldnames(s);
    filset={};
    for i=1:length(v)
        dum0=['dum1=s.' v{i} ';']; eval(dum0);
        if ischar( dum1)
            filset{i,1}=['x.' v{i} '='''   num2str(dum1)  ''';' ];
        else
            filset{i,1}=['x.' v{i} '=['   num2str(dum1)  '];' ];
        end
    end
%      char(filset)
     filnew=[fil1; filset; fil2];
     
  fid = fopen([   fi ],'w','n');%SAVE
    for i=1:size(filnew,1)
        dumh=char(  (filnew(i,:)) );
        fprintf(fid,'%s\n',dumh);
    end
elseif mode==4 | mode==5%copy or evaluate
    tx= findobj(gcf,'tag','txt');
    x=get(gcf,'userdata');%get(tx,'string');
    li=x.e0;
    va=get(tx,'value');
    e0=[];
    for i=1:length(va)
        e0=[e0 li{va(i)}];
    end
    if mode==4
        warning off;
        clipboard('copy',e0)
        warning on;
    elseif mode==5
         try; 
            eval(e0)
          ;end
    end
    
elseif mode==6
     uhelp('uhelp',1);
     ch=findobj(0,'tag','uhelp');
      x=get(gcf,'userdata'); 
      if x.aot==1
          for i=length(ch):-1:1
              figure(ch(i)); aot(0) ;
              if x.aot==1
                 try; aot(1); end
                 set(ch(i),'menubar','none','toolbar','none');
              end
          end
      end    
end

%----------------------------------
% cofig is written HERE
%----------------------------------
function x=setup(x)
%***setup-start  %ANCHOR- DO NOT REMOVE THIS
x.colbg=[1  1  1];
x.fontsize=[9];
x.FontName='Monospaced';
x.FontAngle='normal';
x.FontWeight='normal';
x.fgpos=[0.15391     0.66992     0.53203     0.23047];
x.aot=[0];
%***setup-end   %ANCHOR-DO NOT REMOVE THIS












Contact us