⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sptool.m

📁 有关matlab的电子书籍有一定的帮助希望有用
💻 M
📖 第 1 页 / 共 5 页
字号:
%   Set preference structure for panel with panelName
%   Inputs:
%     panelName - string
%     p - value structure for this panel
%     fig (optional) - figure of SPTool; uses findobj if not given
case 'setprefs'
    panelName = varargin{2};
    p = varargin{3};
    if nargin > 3
        fig = varargin{4};
    else
        fig = findobj(0,'Tag','sptool');
    end
    ud = get(fig,'userdata');
    allPanels = {ud.prefs.panelName};
    i = findcstr(allPanels,panelName);
    if isempty(i)
        error(sprintf('Sorry, no panel with name ''%s''; available: %s',...
                     panelName, sprintf('\n   ''%s'' ',allPanels{:})))
    end
    ud.prefs(i).currentValue = struct2cell(p);
    setsigpref(ud.prefs(i).panelName,p);
    set(fig,'userdata',ud)
        
case 'resize'
    sptoolfig = findobj(0,'Tag','sptool');
    ud = get(sptoolfig,'UserData');
    numComponents = length(ud.components);
    fp = get(sptoolfig,'position');
    upperlefty = fp(2)+fp(4);
    if fp(3) < 208 & fp(4) < 193,
       fp(2) = upperlefty - 193;
       fp(3) = 208;
       fp(4) = 193;
       % warndlg('Restoring SPTool figure to its minimum height and width.','Too Small');
    elseif fp(3) < 208,
       fp(3) = 208;
       % warndlg('Restoring SPTool figure to its minimum width.','Too Thin');
    elseif fp(4) < 193.
       fp(2) = upperlefty - 193;
       fp(4) = 193;
       % warndlg('Restoring SPTool figure to its minimum height.','Too Short');
    end
    set(sptoolfig,'Position',fp);
    d1 = 3;
    d2 = 3;
    d3 = d2;
    uw = (fp(3)-(numComponents+1)*d1)/numComponents;
    uh = 20;  % height of buttons and labels
    lb = d2+(d2+uh)*(ud.maxVerbs);  % list bottom
    for i=1:numComponents
        set(ud.list(i),'position',[d1+(d1+uw)*(i-1) lb uw fp(4)-lb-2*d2-uh])
        set(ud.label(i),'position',[d1+(d1+uw)*(i-1) fp(4)-d2-uh uw uh])
        for j=1:length(ud.components(i).verbs)
            set(ud.buttonHandles(i).h(j),'position',...
                    [d1+(d1+uw)*(i-1) lb-j*(d2+uh) uw uh]);
        end
    end

case 'open'
%  sptool('open')   <-- uses uigetfile to get filename and path
%  sptool('open',f) <-- uses file with filename f on the MATLAB path
%  sptool('open',f,p) <-- uses file with filename f and path p
    sptoolfig = findobj(0,'Tag','sptool');
    ud = get(sptoolfig,'UserData');
    if ud.unchangedFlag == 0
        if ~saveChangesPrompt(ud.sessionName,'opening')
            return
        end
    end

    switch nargin 
    case 1
        matlab_wd = pwd;
        cd(ud.wd)
        [f,ud.sessionPath]=uigetfile('*.spt','Open Session');
        cd(matlab_wd)
        loadName = fullfile(ud.sessionPath,f);
        if ~isequal(f,0)
            ud.wd = ud.sessionPath;
            set(gcf,'userdata',ud)
        end
    case 2
        f = varargin{2};
        loadName = which(f);
        if isempty(loadName)
            error(['File ' f ' not found.'])
        end
        ud.sessionPath = '';
    case 3
        f = varargin{2};
        ud.sessionPath = varargin{3};
        loadName = fullfile(ud.sessionPath,f);
        if exist(loadName)~=2
            error(['File ' loadName ' not found.'])
        end
    end

    if ~isequal(f,0)
        load(loadName,'-mat')
        if ~exist('session','var')  % variable
            waitfor(msgbox('Sorry, this file is not a valid SPT file.',...
               'Missing Session Info','error','modal'))
            return
        end
        ud = get(gcf,'userdata');
        ud.sessionName = f;
        ud.session = struct2cell(session);
        [ud.session,msgstr] = sptvalid(ud.session,ud.components);
        figname = prepender(['SPTool: ' ud.sessionName]);
        set(gcf,'name',figname)
        set(gcf,'userdata',ud)
        for k = 1:length(ud.session)
            if ~isempty(ud.session{k})
                set(ud.list(k),'Value',1)
            end
        end
        updateLists
        selectionChanged(sptoolfig,'new')
        for idx = [4 6]
           set(ud.filemenu_handles(idx),'Enable','on');
        end
        set(ud.filemenu_handles(5),'Enable','off');
        ud = get(gcf,'userdata');
        if isempty(msgstr)
            ud.savedFlag = 1;
            ud.unchangedFlag = 1;
        else
            ud.savedFlag = 0;
            ud.unchangedFlag = 0;
        end
        ud.changedStruc = [];
    end
    set(sptoolfig,'UserData',ud);
    
%----------------------------------------------------------------------
%err = sptool('save')
%    save session, using known file name
%    If the session has never been saved, calls sptool('saveas') 
%  CAUTION: saves userdata on exit (to save ud.unchangedFlag)
%  Outputs:
%    err - ==1 if cancelled, 0 if save was successful.
case 'save'
    sptoolfig = findobj(0,'Tag','sptool');
    ud = get(sptoolfig,'UserData');
    if ~ud.savedFlag
        err = sptool('saveas');
        ud = get(sptoolfig,'UserData');
    else
        session = cell2struct(ud.session,{ud.components.name});
        save(fullfile(ud.sessionPath,ud.sessionName),'session')
        err = 0;
    end
    ud.unchangedFlag = ~err;
    if ud.unchangedFlag
        set(ud.filemenu_handles(5),'Enable','off')
    end
    set(sptoolfig,'UserData',ud)
    varargout{1} = err;
%----------------------------------------------------------------------
%err = sptool('saveas')
%    save session, prompting for file name.
%  CAUTION: saves userdata on exit (to save ud.unchangedFlag)
%  Outputs:
%    err - ==1 if cancelled, 0 if save was successful.
case 'saveas'
    sptoolfig = findobj(0,'Tag','sptool');
    ud = get(sptoolfig,'UserData');
    matlab_wd = pwd;
    cd(ud.wd)
    [f,p] = sptuiputfile(ud.sessionName,'Save Session');
	
    cd(matlab_wd)
    if ~isequal(f,0)
        if (length(f)<4) | ~isequal(f(end-3:end),'.spt')
            errstr = {'Sorry, the filename must end in ".spt".'};
            waitfor(msgbox(errstr,'Bad Extension','error','modal'))
        else
            session = cell2struct(ud.session,{ud.components.name});
            ud.sessionName = f;
            ud.sessionPath = p;
            save(fullfile(p,f),'session')
            
            ud.sessionName = f;
            set(sptoolfig,'userdata',ud)
        
            figname = prepender(['SPTool: ' ud.sessionName]);
            set(sptoolfig,'name',figname)
            ud.unchangedFlag = 1;
        end
        ud.wd = p;
    else
        % ud.unchangedFlag = ud.unchangedFlag;  % value doesn't change
    end
    if ud.unchangedFlag
        set(ud.filemenu_handles(5),'Enable','off')
    end
    ud.savedFlag = ud.savedFlag | ud.unchangedFlag;
    set(sptoolfig,'UserData',ud)
    varargout{1} = ~ud.unchangedFlag;
    
case 'pref'
    fig = gcf;
    ud = get(fig,'userdata');
    [ud.prefs ud.panelInd] = sptprefs(ud.prefs,ud.panelInd);
    set(fig,'userdata',ud)

case 'close'
    sptoolfig = findobj(0,'Tag','sptool');
    ud = get(sptoolfig,'UserData');     
    
    for i = 1:length(ud.prefs)
        [p{i},p_default{i}] = sptool('getprefs',...
		                      ud.prefs(i).panelName,sptoolfig);
    end
    if ~isequal(p,p_default) 
        setsigpref({ud.prefs.panelName},p,1);
    end    
 
    if ud.unchangedFlag == 0
        if ~saveChangesPrompt(ud.sessionName,'closing')
            return
        end
    end
    
    for i=1:length(ud.components)
        for j=1:length(ud.components(i).verbs)
            feval(ud.components(i).verbs(j).owningClient,'SPTclose',...
                  ud.components(i).verbs(j).action);
        end
    end
    if ~isempty(ud.importSettings)
        if ishandle(ud.importSettings.fig)
            delete(ud.importSettings.fig) 
        end
    end
    delete(sptoolfig)

case 'create'
% Create Signal, Filter or Spectrum structure from the command line:
% sptool('create',...)
%

    error(nargchk(2,9,nargin))
    shh = get(0,'ShowHiddenHandles');
    set(0,'ShowHiddenHandles','on')
    sptoolfig = findobj(0,'Tag','sptool');
    set(0,'ShowHiddenHandles',shh);
    
    if isempty(sptoolfig)  % SPTool is not open - only get required info
        ud.components = [];
        ud.components = sptcompp(ud.components); % calls one in signal/private
        ud.prefs = sptprefp;
        allPanels = {ud.prefs.panelName};
        plugInd = findcstr(allPanels,'plugins');
        plugPrefs = cell2struct(ud.prefs(plugInd).currentValue,...
                                ud.prefs(plugInd).controls(:,1));
        if plugPrefs.plugFlag
            % now call each one found on path:
            ud.components = sptool('callall','sptcomp',ud.components);
        end
    else                   % SPTool is open; get the info from SPTool
        ud = get(sptoolfig,'UserData');
    end
    compNames = {ud.components.structName};
    
    if ~isstr(varargin{2}) & ~isnumeric(varargin{2})
        set(0,'showhiddenhandles',shh)
        errstr = sprintf(['Second argument must be a string indicating'...
                ' the component name, \nor a double containing data for'...
                ' the first component.']);
        error(errstr)
    end
        
    compIndx = [];
    if isnumeric(varargin{2})
        % No component name specified; use default component (1st one)
        compIndx = 1;
        varargin{end+1} = varargin{end}; % Make room to insert comp name
        mvIndx = 2:nargin; 
        varargin(mvIndx+1) = varargin(mvIndx);
        varargin{2} = compNames{compIndx}; % Insert component name
    else
        % Since the 2nd arg is not data for the default (1st) component
        % then it must be a string containing the component name; check it!
        for i = 1:length(compNames)
            if strcmp(lower(varargin{2}),lower(compNames{i}))
                % Index into compNames which matches compnt name entered
                compIndx = i; 
                break
            end
        end
        if isempty(compIndx)
            % Component name was specified incorrectly
            set(0,'showhiddenhandles',shh)
            str = ['Sorry, no component with name ''%s''; '...
                    'must be one of the following:\n%s'];
            errstr = sprintf(str, varargin{2},...
                     sprintf('      ''%s''\n',compNames{:}));
            error(errstr);
        end
    end

    [popupString,fields,FsFlag,defaultLabel] = ...
        feval(ud.components(compIndx).importFcn,'fields');
    
    [formIndx, formTags] = formIndex(fields,varargin{3});
    if isempty(formIndx)
        set(0,'showhiddenhandles',shh)
        error(['Sorry, the form must be one of the following: ',...
                formTags])
    elseif length(fields) ~= 1  % A valid 'form' was specified
        varargin(3) = [];       % Remove 'form' from input argument list
    end 
    
    if isstr(varargin{end})
        compCell = varargin(3:end-1); % Cell array containing the component
    else
        compCell = varargin(3:end);
    end
    numCompFields = length(fields(formIndx).fields);
    
    if length(compCell) < numCompFields
        % Padd with []s in case some default arguments were left out
        compCell{numCompFields} = [];
    elseif length(compCell) > numCompFields+1
        errstr = sprintf(['Too many input arguments to import a ',...
                '''%s''. '], compNames{compIndx});
        if ~isstr(varargin{end})
            errstr = [errstr, 'Last argument must be a string.'];
        end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -