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

📄 sptimport.m

📁 有关matlab的电子书籍有一定的帮助希望有用
💻 M
📖 第 1 页 / 共 3 页
字号:
function varargout = sptimport(varargin)
%SPTIMPORT Import Dialog box for importing data into the SPTool.
%   [componentNum,struc,importSettings,wd] = ...
%                  sptimport(components,labelList,importSettings,wd)
%     puts up a dialog box allowing a user to create a SPTool structure
%     from MATLAB variables, or to select an exported SPTool structure.
%     Inputs:
%        components - data structure created by sptregistry functions,
%          contains information about the components of this SPTool session.
%        labelList - list of strings - current variable labels in the SPTool.
%          sptimport will prompt user if he specifies a label which already
%          exists in this list
%        importSettings - if [], the default settings are chosen
%          otherwise uses importSettings from last call to this function.
%          this is a way to remember the settings the user last typed into
%          this dialog box.
%        wd - working directory - location to begin browsing for files
%     Outputs:
%        componentNum - 0 if user hit cancel, 1..N if success; value indicates
%         which component this structure belongs to
%        struc - new SPTool structure (may have same name as existing object)
%        importSettings - use this to call this dialog box next time.
%        wd - working directory - end location for file browsing

%   Copyright (c) 1988-98 by The MathWorks, Inc.
% $Revision: 1.14 $

if nargin == 0
    action = get(gcbo,'Tag');
elseif isstr(varargin{1})
    action = varargin{1};
else
    action = 'init';
end

switch action
  case 'init'  
    if isempty(varargin{3})|~ishandle(varargin{3}.fig)
      % Open figure and create ui objects
      initImport
      ud = get(gcf,'userdata');
      ud.fullFileName = '';
      ud.fileNameString = '';
      ud.FsString = num2str(sptool('commonFs'));
      set(ud.h.editFs,'string',ud.FsString);
      ud.fig = gcf;
        ud.labelList = varargin{2};
      ud.struc = [];
      
      set([ud.h.filename ud.h.filenameLabel ud.h.browse],'enable','off')
      minwidth([ud.h.SourceLabel ud.h.ContentsLabel ud.h.labelLabel])

      % More initialization code here

      components = varargin{1};
      ud.wd = varargin{4};  % working directory
      numComponents = length(components);
      importAsString = cell(1,numComponents);
      for i=1:numComponents
        [popupString,fields,FsFlag,defaultLabel] = ...
            feval(components(i).importFcn,'fields');
        ud.FsFlag(i) = FsFlag;
        importAsString{i} = popupString;
        ud.fields{i} = fields;
        ud.importFcn{i} = components(i).importFcn;
        ud.defaultLabel{i} = uniqueDefaultLabel(ud.labelList,defaultLabel);
        ud.formValue(i) = 1;
        for j = 1:length(fields)
          ud.fieldStrings{i}{j} = cell(1,length(fields(j).fields));
          for k = 1:length(fields(j).fields)
              ud.fieldStrings{i}{j}{k} = '';
          end
        end
      end
      set(ud.h.importas,'string',importAsString,'value',1)
      set(gcf,'userdata',ud)
      changeComponent
    
      getContentsOfWorkspace

    else
      ud = varargin{3};
      figure(ud.fig)
      ud.labelList = varargin{2};
      components = varargin{1};
      ud.wd = varargin{4};  % working directory
      numComponents = length(components);
      for i = 1:numComponents
          [popupString,fields,FsFlag,defaultLabel] = ...
                      feval(components(i).importFcn,'fields');
          ud.defaultLabel{i} = uniqueDefaultLabel(ud.labelList,defaultLabel);
      end
      currentComponent = get(ud.h.importas,'value');
      set(ud.h.editLabel,'string',ud.defaultLabel{currentComponent})
      set(ud.fig,'userdata',ud)
      if strcmp(computer,'PCWIN') | strcmp(computer,'MAC2')
      % on UNIX, already modal
          set(ud.fig,'windowstyle','modal')
      end
      set(ud.fig,'visible','on')
      if get(ud.h.radio1,'value')==1
          getContentsOfWorkspace
      else
          getContentsOfFile
      end
    end

    % Initialization done ... now wait for OK or Cancel buttons:
    set(ud.h.OKButton,'userdata','')
    waitfor(ud.h.OKButton, 'userdata')
    
    ud = get(ud.fig,'userdata');
    
    switch get(ud.h.OKButton,'userdata')
    case 'OK'
       struc = ud.struc;
       componentNum = get(ud.h.importas,'value');
    case 'Cancel'
       componentNum = 0;
       struc = [];
    end
    
    set(ud.fig,'visible','off')
    if strcmp(computer,'PCWIN') | strcmp(computer,'MAC2')
    % prevent modal focus on invisible window when not on UNIX
        set(ud.fig,'windowstyle','normal')
    end
    
    % delete help objects if they are there:
    delete(findobj(ud.fig,'tag','importhelp'))
    
    varargout = {componentNum,struc,ud,ud.wd};
    
  case 'radio1'
    % Callback code for radiobutton with Tag "radio1"
    % Source: from Workspace
    ud = get(gcf,'userdata');
    val = get(ud.h.radio1,'value');
    if val==0   % User has clicked on this radio even though it was
                % already on - so leave it on and exit
        set(ud.h.radio1,'value',1)
    else
        set(ud.h.radio2,'value',0)
        set([ud.h.filename ud.h.filenameLabel ud.h.browse],'enable','off')
        set(ud.h.ContentsLabel,'string','Workspace Contents')
        minwidth(ud.h.ContentsLabel)
        getContentsOfWorkspace
    end
    
  case 'radio2'
    % Callback code for radiobutton with Tag "radio2"
    % Source: from Disk
    ud = get(gcf,'userdata');
    val = get(ud.h.radio2,'value');
    if val==0   % User has clicked on this radio even though it was
                % already on - so leave it on and exit
        set(ud.h.radio2,'value',1)
    else
        set(ud.h.radio1,'value',0)
        set([ud.h.filename ud.h.filenameLabel ud.h.browse],'enable','on')
        set(ud.h.ContentsLabel,'string','File Contents')
        minwidth(ud.h.ContentsLabel)
        getContentsOfFile
    end

  case 'filename'
    % Callback code for edit with Tag "filename"
    ud = get(gcf,'userdata');
    filename = get(ud.h.filename,'string');
    if isempty(find(filename=='.')) 
        if (length(filename)<4) | ~strcmp(lower(filename(end-3:end)),'.mat')  
            filename = [filename '.mat'];
            set(ud.h.filename,'string',filename)
        end
    end
    fullFileName = which(filename);
    if ~isempty(fullFileName)
        ud.fullFileName = fullFileName;
        ud.fileNameString = get(ud.h.filename,'string');
        set(gcf,'userdata',ud)
        getContentsOfFile
    else
        if (length(filename)<4) | ~strcmp(lower(filename(end-3:end)),'.mat'),
            errstr = 'The MAT-file''s name must end in ".mat".';
        else
            errstr = ['Sorry, I can''t find the file "' filename '".'];
        end
        waitfor(msgbox(errstr,'File Error','error','modal'))
        set(ud.h.filename,'string',ud.fileNameString)
    end
    
  case 'browse'
    % Callback code for pushbutton with Tag "browse"
    ud = get(gcf,'userdata');
    matlab_wd = pwd;
    cd(ud.wd)
    [f,p]=uigetfile('*.mat');
    cd(matlab_wd)
    if ~isequal(f,0)
        ud.fullFileName = fullfile(p,f);
        ud.fileNameString = f;
        ud.wd = p;
        set(ud.h.filename,'string',ud.fileNameString)
        set(gcf,'userdata',ud)
        getContentsOfFile
    end
  
  case 'listbox'
    % Callback code for listbox with Tag "listbox"
    ud = get(gcf,'userdata');
    val = get(ud.h.listbox,'value');
    str = get(ud.h.listbox,'string');
    types = get(ud.h.listbox,'userdata');
    currentComponent = get(ud.h.importas,'value');
    currentForm = get(ud.h.formPopup,'value');
    hands = [ud.h.arrow1 ud.h.edit1 
             ud.h.arrow2 ud.h.edit2 
             ud.h.arrow3 ud.h.edit3 
             ud.h.arrow4 ud.h.edit4 
             ud.h.arrow5 ud.h.editFs];

    switch types(val)
    case -1   % selection not a SPT data type and not "transferrable" to the
              % edit boxes by using the arrows
        set(hands(:,1),'enable','off')
        set(hands(:,2),'enable','on')
        % set edit strings
        strings = ud.fieldStrings{currentComponent}{currentForm}';
        set(hands(1:length(strings),2),{'string'},strings)
        set(ud.h.editFs,'string',ud.FsString)
        
    case 0    % selection is transferrable
        set(hands(:,1:2),'enable','on')
        % set edit strings
        strings = ud.fieldStrings{currentComponent}{currentForm}';
        set(hands(1:length(strings),2),{'string'},strings)
        set(ud.h.editFs,'string',ud.FsString)
             
    otherwise % selection is a valid SPT data type
              %  - can't transfer by arrows
              %  - might need to change component
              %  - set edit boxes to 'enable','off' with special strings
        if get(ud.h.importas,'value')~=types(val)
            set(ud.h.importas,'value',types(val))
            changeComponent
            set(ud.h.listbox,'value',val)
        end
        varName = str{val};
        ind = find(varName=='[');
        varName(ind-1:end)=[];
        workspaceFlag = get(ud.h.radio1,'value');
        if ud.FsFlag(types(val))
           if workspaceFlag
               Fs = getStructureField(varName,'Fs');
           else
               Fs = getStructureField(varName,'Fs',ud.fullFileName);
           end
           set(ud.h.editFs,'string',sprintf('%.9g',Fs))
        end
        if workspaceFlag
            label = getStructureField(varName,'label');
        else
            label = getStructureField(varName,'label',ud.fullFileName);
        end
        set(ud.h.editLabel,'string',label)
        set(hands(:,1),'enable','off')
        set(hands(1:4,2),'enable','off','string',['<' label '>'])
    end
    
  case 'Help'
    % Callback code for pushbutton with Tag "Help"
    fig = gcf;
    uiList = findobj(fig,'type','uicontrol');
    saveVis = get(uiList,'visible');
    if strcmp(computer,'PCWIN')
        set(uiList,'visible','off')
    end
    ud = get(fig,'userdata');
    fp = get(fig,'position');
    sz = sptsizes;
    f = uicontrol('style','frame',...
           'position',[sz.fus sz.fus fp(3)-2*sz.fus fp(4)-sz.fus-1],...
           'tag','importhelp');
    tp = [2*sz.fus 4*sz.fus+sz.uh fp(3)-4*sz.fus fp(4)-(6*sz.fus+sz.uh)];
       % text position
    [fontname,fontsize]=fixedfont;
    t = uicontrol('style','listbox','position',tp,'string',importHelpStr,'max',2,...
         'tag','importhelp','horizontalalignment','left',...
         'backgroundcolor','w','fontname',fontname,'fontsize',fontsize);
    % bp = [2*sz.fus 2*sz.fus sz.bw sz.uh];  % button position
    bp = [27 16 60 20];  %-- use exact same pos as 'Help' button
    b = uicontrol('style','pushbutton','position',bp,...
         'tag','importhelp','string','OK',...
         'callback','delete(findobj(gcf,''tag'',''importhelp''))');
    waitfor(b)
    if all(ishandle(uiList))
        if strcmp(computer,'PCWIN')
            set(uiList,{'visible'},saveVis)
        end
    end
    
  case 'Cancel'
    % Callback code for pushbutton with Tag "Cancel"
    ud = get(gcf,'userdata');
    % handle close request when in "Help" mode:
    b = findobj(gcf,'tag','importhelp');
    if ~isempty(b)
        delete(b)  % triggers waitfor in 'Help' callback
    end

    set(ud.h.OKButton,'userdata','Cancel')  % triggers waitfor in initial
                                            % function call
      
  case 'OKButton'
    % Callback code for pushbutton with Tag "OK"
    ud = get(gcf,'userdata');


    str = get(ud.h.listbox,'string');
    val = get(ud.h.listbox,'value');
    types = get(ud.h.listbox,'userdata');
    currentComponent = get(ud.h.importas,'value');
    currentForm = get(ud.h.formPopup,'value');
    errstr = ''; % error string
    
    componentNames = get(ud.h.importas,'string');
    if types(val)>0  % import previously exported object
        varName = str{val};
        ind = find(varName=='[');
        varName(ind-1:end)=[];
        workspaceFlag = get(ud.h.radio1,'value');
        if workspaceFlag
            ud.struc = getVariable(varName);
        else
            ud.struc = getVariable(varName,ud.fullFileName);
        end

        err = 0;        
        if ud.FsFlag(types(val))
           if workspaceFlag
               [ud.struc.Fs,err] = getVariable(get(ud.h.editFs,'string'));
           else
               [ud.struc.Fs,err] = getVariable(get(ud.h.editFs,'string'),...
                                                ud.fullFileName);
           end
           if err,
               errstr = ['Sorry, the Sampling Frequency you entered '...
                         'cannot be evaluated.'];
           end
        end
        
        if ~err
            [valid,ud.struc] = feval(ud.importFcn{currentComponent},...
                  'valid',ud.struc);
            if ~valid,
                errstr = ['Sorry, your selection is not a valid ' ...
                          componentNames{currentComponent} '.'];
            end
            err = ~valid;
        end
    else % make a new object
        % make a vector of handles
        strings = ud.fields{currentComponent}(currentForm).fields;
        hands = [ ud.h.edit1 
                  ud.h.edit2 
                  ud.h.edit3 
                  ud.h.edit4 ];
        hands = hands(1:length(strings));        
        if ud.FsFlag(currentComponent)
            hands = [hands; ud.h.editFs];
            strings{end+1} = get(ud.h.labelFs,'string');
        end
        params = cell(1,length(hands)+1);
        params{1} = get(ud.h.formPopup,'value');
        if get(ud.h.radio2,'value')
            getVariableParams{2} = ud.fullFileName;
        end
        for i=1:length(hands)
            getVariableParams{1} = get(hands(i),'string');
            [params{i+1},err] = getVariable(getVariableParams{:});
            if err,
               switch err

⌨️ 快捷键说明

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