www.gusucode.com > 学生管理系统的一个例子GUI界面的管理matlab源码程序 > 学生管理系统的一个例子GUI界面的管理matlab源码程序/Matlab_database_GUI/CreateSchema.m

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%   ErrNum  0-->没有错误
%           1-->参数个数不正确
%               参数个数必须按照格式CreateSchema(FilePath,Property,Value,property,Value...)来写,否则返回错误1
%           2-->指定文件不存在
%           3-->文件在指定分割方式下,约束超出字段数
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function f=CreateSchema(FilePath,varargin)
f.Message='';
f.ErrNum=0;
f.FilePath='';
f.Path='';
f.FileName='';
f.TableName='';
f.FieldCount='';
f.FieldName='';
f.DataType='';
f.SchemaContent='';

ColNameHeader='False';
Format=' ';
MaxScanRows='0';
CharacterSet='ANSI';
FieldName={'Field'};
DataType={'Float'};

FileName=StrSpliteToCell(FilePath,'\');
FileName=cell2mat(FileName(end));
Path=strrep(FilePath,FileName,'');
TableName=StrSpliteToCell(FileName,'.');
TableName=cell2mat(TableName(1));
FieldCount=0;

Len=length(varargin);
f.ErrNum=mod(Len,2);
if f.ErrNum>0
    f.Message='输入参数不正确!';
    return
end

if ~isempty(varargin)
    for i=1:2:Len
        switch varargin{i}
            case 'ColNameHeader'
                ColNameHeader=varargin{i+1};
            case 'Format'
                Format=varargin{i+1};
            case 'MaxScanRows'
                MaxScanRows=varargin{i+1};
            case 'CharacterSet'
                CharacterSet=varargin{i+1};
            case 'FieldName'
                FieldName=varargin{i+1};
            case 'DataType'
                DataType=varargin{i+1};
        end
    end
end
fid=fopen(FilePath,'r');
if fid==-1
    f.Message='文件路径不正确!'
    f.ErrNum=2;
    f.FilePath='Not Exists!'
    return
end
HeadLine=fgetl(fid);
fclose(fid);
Content=StrSpliteToCell(HeadLine,Format);
FieldCount=length(Content);
f.FilePath=FilePath;
f.Path=Path;
f.FileName=FileName;
f.TableName=TableName;
f.FieldCount=FieldCount;
[Mf Nf]=size(FieldName);
[Md Nd]=size(DataType);
if (Mf==1 && Nf>FieldCount) || (Md==1 && Nd>FieldCount)...
        || (Mf==2 && max(cell2mat(FieldName(1,:)))>FieldCount)...
        || (Md==2 && max(cell2mat(DataType(1,:)))>FieldCount)
    f.Message='超出字段数!';
    f.ErrNum=3;
    return
end
Temp_FieldName=cell(1,FieldCount);
if Mf==1 && Nf==1
    for i=1:FieldCount
        Temp_FieldName(1,i)={strcat('Field',mat2str(i))};
    end
elseif Mf==1
    for i=1:Nf
        Temp_FieldName(i)=FieldName(i);
    end
    for i=Nf+1:FieldCount
        Temp_FieldName(i)={strcat('Field',mat2str(i))};
    end
elseif Mf==2
    TheIndex=cell2mat(FieldName(1,:));
    Temp_FieldName(TheIndex)=FieldName(2,:);
    theIndex=1:FieldCount;
    theIndex(TheIndex)=[];
    Len_theIndex=length(theIndex);
    for i=1:Len_theIndex
        Temp_FieldName(theIndex(i))={strcat('Field',mat2str(theIndex(i)))};
    end
end
Temp_DataType=cell(1,FieldCount);
if Md==1 && Nd==1
    for i=1:FieldCount
        Temp_DataType(1,i)={'Float'};
    end
elseif Md==1
    for i=1:Nd
        Temp_DataType(1,i)=DataType(i);
    end
    for i=Nd+1:FieldCount
        Temp_DataType(1,i)={'Float'};
    end
elseif Md==2
    TheIndex=cell2mat(DataType(1,:));
    Temp_DataType(TheIndex)=DataType(2,:);
    theIndex=1:FieldCount;
    theIndex(TheIndex)=[];
    Len_theIndex=length(theIndex);
    for i=1:Len_theIndex
        Temp_DataType(theIndex(i))={'Float'};
    end
end
FieldName=Temp_FieldName;
DataType=Temp_DataType;
SchemaContent=['[' FileName ']\n'];
SchemaContent=[SchemaContent 'ColNameHeader=' ColNameHeader '\n'];
SchemaContent=[SchemaContent 'Format=Delimited(' Format ')\n'];
SchemaContent=[SchemaContent 'MaxScanRows=' MaxScanRows '\n'];
SchemaContent=[SchemaContent 'CharacterSet=' CharacterSet '\n'];
for i=1:FieldCount
    SchemaContent=[SchemaContent 'col' mat2str(i) '=' char(34) cell2mat(FieldName(1,i)) char(34) ' ' cell2mat(DataType(1,i)) '\n'];
end
fid=fopen([Path 'Schema.ini'],'wt');
fprintf(fid,SchemaContent);
fclose(fid);
f.FieldName=FieldName;
f.DataType=DataType;
f.SchemaContent=SchemaContent;