www.gusucode.com > mbcview 工具箱matlab源码程序 > mbcview/@cgdatasetnode/export.m

    function ok=export(nd,dest)
%EXPORT Export dataset information
%
%  OK=EXPORT(ND,DEST)  where DEST is 'csv' or 'mbcmodel'
%

%  Copyright 2000-2014 The MathWorks, Inc. and Ford Global Technologies, Inc.


ok=0;

switch dest
    case 'csv'
        ok = i_exportcsv(nd,dest);
    case 'mbcmodel'
        ok = i_exportToMBCModel(nd);
    case 'workspace'
        ok = iExportWorkspace(nd);
end


function ok = i_exportcsv(nd,dest)

ok = false;
fopts = {'*.csv','Comma Separated Value (csv)'};
[fname,pname] = uiputfile(fopts, 'Save Data set as', mbcGetPath('cage', 'Data'));

if isnumeric(fname) || isnumeric(pname)
    return;
end


[~,fname,ext]=fileparts(fname);

if isempty(ext)
    ext=['.' dest];
end

FILE=fullfile(pname,[fname ext]);
fid = fopen(FILE , 'w');

if fid == -1
    h = errordlg(['Error opening ' FILE '. Check read-only status.'] , 'Cage' , 'modal');
    uiwait(h);
    return;
end

oppt=info(getdata(nd));
data = get(oppt,'Data');
heads = get(oppt,'factors');
[~,cols] = size(data);
dataIn = get(oppt,'factor_type');
heads=heads(dataIn~=0);
data=data(:,dataIn~=0);

fprintf(fid,'%s',heads{1});
for n=2:cols
    fprintf(fid,',%s',heads{n});
end
fprintf(fid,'\n');

% build up format string for numeric output
fmtstr=repmat('%f,',1,cols);
% add newline char to format string and remove final comma
fmtstr=[fmtstr(1:end-1) '\n'];

% output data
fprintf(fid,fmtstr,data');

fclose(fid);
ok=1;


function ok = i_exportToMBCModel(nd)

ok = true;
mbh = MBrowser;
pMBCModel = get(mbh,'RootNode');
% make a new data object
[~, pData] = createDataObject(pMBCModel.info);

ssf = pData.info;
oppt=info(getdata(nd));
% make a structure with CAGE data set
s.data = get(oppt,'Data');
s.varNames = get(oppt,'factors');
% label data as being exported from CAGE
s.filename = sprintf('CAGE data set %s',getname(oppt));
ss= struct2sweepset(sweepset,s);
% Set the sweepset for the sweepsetfilter (This will trigger an UpdateAll in the
% sweepsetfilter and hence all relevant events will be triggered)
ssf = setSweepset(ssf, ss);
% Do we need to set any default test groupings?
if isempty(get(ssf, 'definetests'))
    ssf = applyDefaultTestDefinition(ssf);
end
% name the data object appropriately
ssf = set(ssf,'Label',getname(oppt));
pData.info = ssf;

if mbh.CurrentNode == pMBCModel
    % update model browser view if we are on root node
    mbh.ViewNode;
end

function ok = iExportWorkspace(nd)
ok = true;
oppt=info(getdata(nd));
% make a structure with CAGE data set
data = num2cell(get(oppt,'Data'),1);
varNames = get(oppt,'factors');
T = table(data{:},'VariableNames',varNames);
assignin('base',getname(nd),T)
fprintf('Data set %s exported to workspace as a table.\n',getname(nd));