📄 sptimport.m
字号:
case 1
errstr = ['Sorry, your entry in the "' strings{i} ...
'" field could not be evaluated.'];
case 2
errstr = ['Sorry, you need to enter something '...
'in the "' strings{i} '" field.'];
end
break
end
end
if ~err
[err,errstr,ud.struc] = ...
feval(ud.importFcn{currentComponent},'make',params);
end
end
if ~err
label = get(ud.h.editLabel,'string');
err = ~isvalidvar(label);
if ~err
if ~isempty(findcstr(ud.labelList,label))
% prompt for over-write
switch questdlg(...
{['By importing "' label '", you are replacing an']
'already existing object in the SPTool named'
['"' label '". Any objects that depend on"' label '"']
['will be altered.']
'Are you sure you want to import?'},...
'Name Conflict','Yes','No','No')
case 'Yes'
ud.struc.label = label;
case 'No'
return
end
else
ud.struc.label = label;
end
else
errstr = {'Sorry, the name you have entered is not valid.'
'It must be a legal MATLAB variable name.'};
end
end
set(gcf,'userdata',ud)
if isempty(errstr)
% now send signal that we are done to waitfor:
set(ud.h.OKButton,'userdata','OK')
else
% put up error dialog box
h=msgbox(errstr,'Import Error','error','modal');
waitfor(h)
end
case 'importas'
% Callback code for popupmenu with Tag "importas"
changeComponent
case 'formPopup'
% Callback code for popupmenu with Tag "formPopup"
ud = get(gcf,'userdata');
currentComponent = get(ud.h.importas,'value');
currentForm = get(ud.h.formPopup,'value');
ud.formValue(currentComponent) = currentForm;
set(gcf,'userdata',ud)
changeComponent(1)
case 'arrow1'
% Callback code for pushbutton with Tag "arrow1"
ud = get(gcf,'userdata');
str = get(ud.h.listbox,'string');
val = get(ud.h.listbox,'value');
set(ud.h.edit1,'string',str{val})
editStringChange(1)
case 'arrow2'
% Callback code for pushbutton with Tag "arrow2"
ud = get(gcf,'userdata');
str = get(ud.h.listbox,'string');
val = get(ud.h.listbox,'value');
set(ud.h.edit2,'string',str{val})
editStringChange(2)
case 'arrow3'
% Callback code for pushbutton with Tag "arrow3"
ud = get(gcf,'userdata');
str = get(ud.h.listbox,'string');
val = get(ud.h.listbox,'value');
set(ud.h.edit3,'string',str{val})
editStringChange(3)
case 'arrow4'
% Callback code for pushbutton with Tag "arrow4"
ud = get(gcf,'userdata');
str = get(ud.h.listbox,'string');
val = get(ud.h.listbox,'value');
set(ud.h.edit4,'string',str{val})
editStringChange(4)
case 'arrow5'
% Callback code for pushbutton with Tag "arrow5"
ud = get(gcf,'userdata');
str = get(ud.h.listbox,'string');
val = get(ud.h.listbox,'value');
set(ud.h.editFs,'string',str{val})
ud.FsString = get(ud.h.editFs,'string');
set(gcf,'userdata',ud)
case 'edit1'
% Callback code for edit with Tag "edit1"
editStringChange(1)
case 'edit2'
% Callback code for edit with Tag "edit2"
editStringChange(2)
case 'edit3'
% Callback code for edit with Tag "edit3"
editStringChange(3)
case 'edit4'
% Callback code for edit with Tag "edit4"
editStringChange(4)
case 'editFs'
% Callback code for edit with Tag "editFs"
ud = get(gcf,'userdata');
ud.FsString = get(ud.h.editFs,'string');
set(gcf,'userdata',ud)
end
function editStringChange(i)
%editStringChange sets userdata structure which saves the strings
% entered into the import dialog box
% i is between 1 and 4
% userdata is changed
ud = get(gcf,'userdata');
currentComponent = get(ud.h.importas,'value');
currentForm = get(ud.h.formPopup,'value');
eval(['ud.fieldStrings{currentComponent}{currentForm}{i} = ' ...
'get(ud.h.edit' num2str(i) ',''string'');']);
set(gcf,'userdata',ud)
function getContentsOfFile
% Called by 'browse' button and callback of filename edit box,
% also when 'From Disk' radio button is clicked
ud = get(gcf,'userdata');
if isempty(ud.fullFileName)
set(ud.h.listbox,'string',{'<no file selected>'},'value',1,'userdata',-1)
else
whosString = ['w=whos(''-file'',''' ud.fullFileName ''');'];
err=0;
eval(whosString,'err=1;')
if err
set(ud.h.listbox,'string',{'<file not found>'},'value',1,'userdata',-1)
else
if length(w) == 0
set(ud.h.listbox,'string',{'<file empty>'},'value',1,'userdata',-1)
else
listString = {'<no selection>' w.name};
[type,listString] = componentMarkup(listString,w,ud.fullFileName);
set(ud.h.listbox,'string',listString,'value',1,'userdata',type)
end
end
end
selectNothing
function getContentsOfWorkspace
% Called when 'From Workspace' radio button is clicked
% and (possibly) at initialization time
ud = get(gcf,'userdata');
w = evalin('base','whos');
if length(w) == 0
set(ud.h.listbox,'string',{'<no variables>'},'value',1,'userdata',-1)
else
listString = {'<no selection>' w.name};
[type,listString] = componentMarkup(listString,w);
set(ud.h.listbox,'string',listString,'value',1,'userdata',type)
end
selectNothing
function selectNothing
ud = get(gcf,'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];
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)
function [type,listString] = componentMarkup(listString,w,fname)
%componentMarkup - identify types of objects in workspace or MAT-file
% Inputs:
% listString - cell array - the first element is ignored, the remaining
% elements are string variable names in the workspace or file
% w - whos structure from workspace or MAT-file
% fname - if present, specifies full MAT-file name (directory & filename)
% if not present, componentMarkup expects to find the variables in
% the WORKSPACE.
% Outputs:
% type - vector of integers, 1 element for each string in listString
% -1 --> do not allow transfer with the arrow buttons
% (unknown SPT object or the first element in the list)
% 0 --> normal MATLAB vector; allow transfer with arrow buttons
% 1..n (where n = number of components) -->
% SPT object identified, number corresponds to position
% in string of ud.h.importas
% listString - list of strings for ud.h.listbox, edited to indicate
% various SPT objects
ud = get(gcf,'userdata');
if nargin == 2 % FROM WORKSPACE
workspaceFlag = 1;
else
workspaceFlag = 0;
end
type = zeros(1,length(listString));
type(1) = -1;
dataNames = get(ud.h.importas,'string');
%versions = cell(length(dataNames));
%for i = 1:length(ud.importFcn)
% versions{i} = feval(ud.importFcn{i},'version');
%end
for i = 2:length(listString)
if strcmp(w(i-1).class,'struct') & isequal(w(i-1).size,[1 1])
if workspaceFlag
SPTIdent = getStructureField(listString{i},'SPTIdentifier');
else
SPTIdent = getStructureField(listString{i},'SPTIdentifier',fname);
end
if ~isempty(SPTIdent)
ind = find(strcmp(dataNames,SPTIdent.type));
if isempty(ind)
type(i) = -1;
else
type(i) = ind;
end
end
switch type(i)
case -1
listString{i} = [listString{i} ' [Unknown]'];
case 0
% do nothing
otherwise
listString{i} = ...
[listString{i} ' [' dataNames{ind} ']'];
end
end
end
function varargout = getStructureField(varargin)
%getStructureField
% field = getStructureField(varName,fieldName,fname)
% Returns the field 'fieldName' of the structure 'varName' in the
% MAT-file 'fname'. fname is optional; if you don't specify it, this
% function looks in the workspace instead.
if nargin == 2 % workspace
varargout{1} = evalin('base',[varargin{1} '.' varargin{2}],'[]');
else % MAT-file
load(varargin{3},varargin{1})
varargout{1} = eval([varargin{1} '.' varargin{2}],'[]');
end
function varargout = getVariable(varargin)
%getVariable
% [var,err] = getVariable(varName,fname)
% Returns the var 'varName' in the MAT-file 'fname'. fname is optional;
% if you don't specify it, this function looks in the workspace instead.
% If 'varName' is not found in fname, the string is evaluated in this function's
% workspace;
% err = 2 if the string is empty,
% 1 if there is an error in evaluating the string,
% 0 if OK
GETVARIABLE_ERROR = 0; % this name needs to be long and ugly to minimize
% chances of a (still possible) name clash
if isempty(varargin{1})
varargout{1} = [];
varargout{2} = 2;
return
end
if nargin == 1 % workspace
varargout{1} = evalin('base',varargin{1},'''ARBITRARY_STRING''');
if isequal(varargout{1},'ARBITRARY_STRING')
GETVARIABLE_ERROR = 1;
end
else % MAT-file
eval(['load(''' varargin{2} ''',''' varargin{1} ''')'],'GETVARIABLE_ERROR=1;')
eval(['varargout{1}=' varargin{1} ';'],'GETVARIABLE_ERROR=1;');
end
if GETVARIABLE_ERROR
varargout{1} = [];
end
varargout{2}=GETVARIABLE_ERROR;
function changeComponent(formFlag)
%changeComponent - set Import As: area of dialog according to components
% if formFlag is present, the 'form' popup has changed (not the
% 'Import As' popup).
ud = get(gcf,'userdata');
currentComponent = get(ud.h.importas,'value');
currentForm = get(ud.h.formPopup,'value');
if currentForm ~= ud.formValue(currentComponent)
currentForm = ud.formValue(currentComponent);
set(ud.h.formPopup,'value',currentForm)
end
set(ud.h.formPopup,'value',ud.formValue(currentComponent))
if length(ud.fields{currentComponent}) == 1
set([ud.h.Form ud.h.formPopup],'visible','off')
else
set([ud.h.Form ud.h.formPopup],'visible','on')
end
set(ud.h.formPopup,'string',{ud.fields{currentComponent}.form})
hands = [ud.h.arrow1 ud.h.edit1 ud.h.label1
ud.h.arrow2 ud.h.edit2 ud.h.label2
ud.h.arrow3 ud.h.edit3 ud.h.label3
ud.h.arrow4 ud.h.edit4 ud.h.label4
ud.h.arrow5 ud.h.editFs ud.h.labelFs];
currentLabels = ...
ud.fields{currentComponent}(currentForm).fields;
set(hands(1:length(currentLabels),:),'visible','on')
set(hands(length(currentLabels)+1:4,:),'visible','off')
% set labels:
set(hands(1:length(currentLabels),3),{'string'},currentLabels')
types = get(ud.h.listbox,'userdata');
if nargin<1
% if an SPT Object is selected in the listbox, deselect it
% in the process of changing Components
if types(get(ud.h.listbox,'value')) > 0
set(ud.h.listbox,'value',1) % no selection!
set(hands(:,1),'enable','off')
set(hands(:,2),'enable','on')
end
set(ud.h.editLabel,'string',ud.defaultLabel{currentComponent})
if ~ud.FsFlag(currentComponent)
set(hands(5,:),'visible','off')
else
set(hands(5,:),'visible','on')
end
end
if types(get(ud.h.listbox,'value')) <= 0
% if previously exported structure is not selected, update edit strings
set(hands(1:length(currentLabels),2),{'string'},...
ud.fieldStrings{currentComponent}{currentForm}')
end
function label = uniqueDefaultLabel(labelList,defaultLabel)
% Inputs:
% labelList - list of strings
% defaultLabel - string; such as 'sig','filt'
% Output:
% label - unique identifier such as 'sig1', 'sig2'
i=1;
label = [defaultLabel sprintf('%.9g',i)];
while ~isempty(findcstr(labelList,label))
i=i+1;
label = [defaultLabel sprintf('%.9g',i)];
end
function initImport
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -