- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我已经通过 Matlab Central 进行了谷歌搜索,但找不到任何直接在 Matlab 中打开 DBF 文件的方法。在 TMW 文件交换中有一些对 DBFREAD 函数的引用,但它不再可用。真的有问题吗?
我有数据库工具箱,但在那里找不到 dbf 支持。
我不想使用 Excel 或其他工具在 Matlab 之外转换文件,因为我有很多文件要处理。 ODBC 也不好,我需要代码在 Mac 和 Unix 下也能工作。
请帮忙。
最佳答案
我联系了 DBFREAD 函数的作者 Brian Madsen,该函数已从文件交换中删除,可能是因为 Mathworks 将在未来的某个版本中将此函数包含到 MATLAB 中。布赖恩好心地允许我在这里发布这个功能。所有版权信息保持不变。我只修改了第 33-38 行以允许 DBFREAD 读取工作目录之外的文件。
function [dbfData, dbfFieldNames] = dbfread(filename, records2read, requestedFieldNames)
%DBFREAD Read the specified records and fields from a DBF file.
%
% [DATA, NAMES] = DBFREAD(FILE) reads numeric, float, character and date
% data and field names from a DBF file, FILE.
%
% [DATA, NAMES] = DBFREAD(FILE, RECORDS2READ) reads only the record
% numbers specified in RECORDS2READ, a scalar or vector.
%
% [DATA, NAMES] = DBFREAD(FILE, RECORDS2READ, REQUESTEDFIELDNAMES) reads
% the data from the fields, REQUESTEDFIELDNAMES, for the specified
% records. REQUESTEDFIELDNAMES must be a cell array. The fields in the
% output will follow the order given in REQUESTEDFIELDNAMES.
%
% Examples:
%
% % Get all records and a list of the field names from a DBF file.
% [DATA,NAMES] = dbfread('c:\matlab\work\mydbf')
%
% % Get data from records 3:5 and 10 from a DBF file.
% DATA = dbfread('c:\matlab\work\mydbf',[3:5,10])
%
% % Get data from records 1:10 for three of the fields in a DBF file.
% DATA = dbfread('c:\matlab\work\mydbf',1:10,{'FIELD1' 'FIELD3' 'FIELD5'})
%
% See also XLSREAD, DLMREAD, DLMWRITE, LOAD, FILEFORMATS, TEXTSCAN.
% Copyright 2008 The MathWorks, Inc.
% $Revision: 1.0 $ $Date: 2008/04/18 05:58:17 $
[pathstr,name,ext] = fileparts(filename);
dbfFileId = fopen(filename,'r','ieee-le');
if (dbfFileId == -1)
dbfFileId = fopen(fullfile(pathstr, [name '.dbf']),'r','ieee-le');
end
if (dbfFileId == -1)
dbfFileId = fopen(fullfile(pathstr, [name '.DBF']),'r','ieee-le');
end
if (dbfFileId == -1)
eid = sprintf('MATLAB:%s:missingDBF', mfilename);
msg = sprintf('Failed to open file %s.dbf or file %s.DBF.',...
name, name);
error(eid,'%s',msg)
end
info = dbfinfo(dbfFileId);
if ~exist('requestedFieldNames','var')
dbfFieldNames = {info.FieldInfo.Name};
requestedFieldNames = dbfFieldNames;
else
dbfFieldNames = (info.FieldInfo(matchFieldNames(info,requestedFieldNames)).Name);
end
fields2read = matchFieldNames(info,requestedFieldNames);
% The first byte in each record is a deletion indicator
lengthOfDeletionIndicator = 1;
if ~exist('records2read','var')
records2read = (1:info.NumRecords);
elseif max(records2read) > info.NumRecords
eid = sprintf('MATLAB:%s:invalidRecordNumber', mfilename);
msg = sprintf('Record number %d does not exist, please select from the range 1:%d.',...
max(records2read), info.NumRecords);
error(eid,'%s',msg)
end
% Loop over the requested fields, reading in the data
dbfData = cell(numel(records2read),numel(fields2read));
for k = 1:numel(fields2read),
n = fields2read(k);
fieldOffset = info.HeaderLength ...
+ sum([info.FieldInfo(1:(n-1)).Length]) ...
+ lengthOfDeletionIndicator;
fseek(dbfFileId,fieldOffset,'bof');
formatString = sprintf('%d*uint8=>char',info.FieldInfo(n).Length);
skip = info.RecordLength - info.FieldInfo(n).Length;
data = fread(dbfFileId,[info.FieldInfo(n).Length info.NumRecords],formatString,skip);
dbfData(:,k) = feval(info.FieldInfo(n).ConvFunc,(data(:,records2read)'));
% dbfData(:,k) = info.FieldInfo(n).ConvFunc(data(:,records2read)');
end
fclose(dbfFileId);
%--------------------------------------------------------------------------
function fields2read = matchFieldNames(info, requestedFieldNames)
% Determine which fields to read.
allFieldNames = {info.FieldInfo.Name};
if isempty(requestedFieldNames)
if ~iscell(requestedFieldNames)
% Default case: User omitted the parameter, return all fields.
fields2read = 1:info.NumFields;
else
% User supplied '{}', skip all fields.
fields2read = [];
end
else
% Match up field names to see which to return.
fields2read = [];
for k = 1:numel(requestedFieldNames)
index = strmatch(requestedFieldNames{k},allFieldNames,'exact');
if isempty(index)
wid = sprintf('MATLAB:%s:nonexistentDBFName',mfilename);
wrn = sprintf('DBF name ''%s'' %s\n%s',requestedFieldNames{k},...
'doesn''t match an existing DBF name.',...
' It will be ignored.');
warning(wid,wrn)
end
for l = 1:numel(index)
% Take them all in case of duplicate names.
fields2read(end+1) = index(l);
end
end
end
%--------------------------------------------------------------------------
function info = dbfinfo(fid)
%DBFINFO Read header information from DBF file.
% FID File identifier for an open DBF file.
% INFO is a structure with the following fields:
% Filename Char array containing the name of the file that was read
% DBFVersion Number specifying the file format version
% FileModDate A string containing the modification date of the file
% NumRecords A number specifying the number of records in the table
% NumFields A number specifying the number of fields in the table
% FieldInfo A 1-by-numFields structure array with fields:
% Name A string containing the field name
% Type A string containing the field type
% ConvFunc A function handle to convert from DBF to MATLAB type
% Length A number of bytes in the field
% HeaderLength A number specifying length of the file header in bytes
% RecordLength A number specifying length of each record in bytes
% Copyright 1996-2005 The MathWorks, Inc.
% $Revision: 1.1.10.4 $ $Date: 2005/11/15 01:07:13 $
[version, date, numRecords, headerLength, recordLength] = readFileInfo(fid);
fieldInfo = getFieldInfo(fid);
info.Filename = fopen(fid);
info.DBFVersion = version;
info.FileModDate = date;
info.NumRecords = numRecords;
info.NumFields = length(fieldInfo);
info.FieldInfo = fieldInfo;
info.HeaderLength = headerLength;
info.RecordLength = recordLength;
%----------------------------------------------------------------------------
function [version, date, numRecords, headerLength, recordLength] = readFileInfo(fid)
% Read from File Header.
fseek(fid,0,'bof');
version = fread(fid,1,'uint8');
year = fread(fid,1,'uint8') + 1900;
month = fread(fid,1,'uint8');
day = fread(fid,1,'uint8');
dateVector = datevec(sprintf('%d/%d/%d',month,day,year));
dateForm = 1;% dd-mmm-yyyy
date = datestr(dateVector,dateForm);
numRecords = fread(fid,1,'uint32');
headerLength = fread(fid,1,'uint16');
recordLength = fread(fid,1,'uint16');
%----------------------------------------------------------------------------
function fieldInfo = getFieldInfo(fid)
% Form FieldInfo by reading Field Descriptor Array.
%
% FieldInfo is a 1-by-numFields structure array with the following fields:
% Name A string containing the field name
% Type A string containing the field type
% ConvFunc A function handle to convert from DBF to MATLAB type
% Length A number equal to the length of the field in bytes
lengthOfLeadingBlock = 32;
lengthOfDescriptorBlock = 32;
lengthOfTerminator = 1;
fieldNameOffset = 16; % Within table field descriptor
fieldNameLength = 11;
% Get number of fields.
fseek(fid,8,'bof');
headerLength = fread(fid,1,'uint16');
numFields = (headerLength - lengthOfLeadingBlock - lengthOfTerminator)...
/ lengthOfDescriptorBlock;
% Read field lengths.
fseek(fid,lengthOfLeadingBlock + fieldNameOffset,'bof');
lengths = fread(fid,[1 numFields],'uint8',lengthOfDescriptorBlock - 1);
% Read the field names.
fseek(fid,lengthOfLeadingBlock,'bof');
data = fread(fid,[fieldNameLength numFields],...
sprintf('%d*uint8=>char',fieldNameLength),...
lengthOfDescriptorBlock - fieldNameLength);
data(data == 0) = ' '; % Replace nulls with blanks
names = cellstr(data')';
% Read field types.
fseek(fid,lengthOfLeadingBlock + fieldNameLength,'bof');
dbftypes = fread(fid,[numFields 1],'uint8=>char',lengthOfDescriptorBlock - 1);
% Convert DBF field types to MATLAB types.
typeConv = dbftype2matlab(upper(dbftypes));
% Return a struct array.
fieldInfo = cell2struct(...
[names; {typeConv.MATLABType}; {typeConv.ConvFunc}; num2cell(lengths)],...
{'Name', 'Type', 'ConvFunc', 'Length'},1)';
%----------------------------------------------------------------------------
function typeConv = dbftype2matlab(dbftypes)
% Construct struct array with MATLAB types & conversion function handles.
typeLUT = ...
{'N', 'double', @str2double2cell;... % DBF numeric
'F', 'double', @str2double2cell;... % DBF float
'C', 'char', @cellstr;... % DBF character
'D', 'char', @cellstr}; % DBF date
unsupported = struct('MATLABType', 'unsupported', ...
'ConvFunc', @cellstr);
% Unsupported types: Logical,Memo,N/ANameVariable,Binary,General,Picture
numFields = length(dbftypes);
if numFields ~= 0
typeConv(numFields) = struct('MATLABType',[],'ConvFunc',[]);
end
for k = 1:numFields
idx = strmatch(dbftypes(k),typeLUT(:,1));
if ~isempty(idx)
typeConv(k).MATLABType = typeLUT{idx,2};
typeConv(k).ConvFunc = typeLUT{idx,3};
else
typeConv(k) = unsupported;
end
end
%----------------------------------------------------------------------------
function out = str2double2cell(in)
% Translate IN, an M-by-N array of class char, to an M-by-1 column vector
% OUT, of class double. IN may be blank- or null-padded. If IN(k,:) does
% not represent a valid scalar value, then OUT(k) has value NaN.
if isempty(in)
out = {[NaN]};
return
end
% Use sprintf when possible, but fall back to str2double for unusual cases.
fmt = sprintf('%%%df',size(in,2));
[data count] = sscanf(reshape(in',[1 numel(in)]),fmt);
if count == size(in,1)
out = cell(count,1);
for k = 1:count
out{k} = data(k);
end
else
out = num2cell(str2double(cellstr(in)));
end
更新
如果输入参数中的位数不同,STR2DOUBLE2CELL 子函数有时会无法正常工作(参见 this discussion)。
这是我的 STR2DOUBLE2CELL 版本:
function out = str2double2cell(in)
% Translate IN, an M-by-N array of class char, to an M-by-1 column vector
% OUT, of class double. IN may be blank- or null-padded. If IN(k,:) does
% not represent a valid scalar value, then OUT(k) has value NaN.
if isempty(in)
out = {[NaN]};
return
end
out = cellfun(@str2double,cellstr(in),'UniformOutput',false);
关于matlab - 如何在 Matlab 中打开 DBase 文件 (.DBF)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2112932/
在我遇到这个问题之前,我以为我理解了通用约束。 public class DBase : DbContext, IDisposable where T : DBase T怎么可能是DBase ? 如果
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 6年前关闭。 Improve t
嗨,有人可以推荐一个免费组件,我可以使用该组件将dbase III表加载到Delphi中,最好不使用BDE。 TTable之类的东西,但对于Dbase 谢谢 科林 最佳答案 TDBF应该做您想要的。
我必须使用 ADOConnection 和 AdoTable 从旧的 dBase 数据库复制一些信息。我可以打开所有表格,但出现此异常 Data provider or other service r
我正在开发一个需要读取不同 dBase 文件的程序。我似乎只能找到 dBaseIII plus 文件进行测试。有谁知道我在哪里可以找到测试包或其他东西?我需要在不同版本的 dBase 上测试此代码。
我有一个非常大的 dBase 文件 (1.64Gb)。使用标准 foreign::read.dbf() 函数在 R 中加载整个文件需要很长时间。我只想在数据集中加载几个变量。有人有解决办法吗? 最佳答
我正在使用 Python 3.6 和 dbf 库 https://pypi.python.org/pypi/dbf以及来自 https://github.com/infused/dbf/tree/ma
我目前使用 OleDBCommand.ExecuteNonQuery(重复调用)从源 DataTable 一次将多达 350,000 行插入 dbase 文件 (*.dbf)。我正在重用 OleDbC
我正在研究如何在软件项目中使用自然地球,因此我下载了一个示例数据文件并查看了其 dBase 文件,即 ne_50m_admin_0_countries.dbf 以下是所述文件中的示例行: ScaleR
每次我尝试通过 webhost/adminlogin.php 登录时,我都会被重定向回同一页面。有什么我忘了补充的吗?谢谢你的帮助。下面是我的脚本 这是我的adminlogin.php
我从 mysql phpmyadmin 数据库获取 ID,并希望将结果查询到 dbf。结果是ID,我想使用ID从dbfs获取mysql中不存在的数据 有没有类似MYSQL的dBase DBF表达式 D
我的具体问题是我无法按日期字段进行过滤。 这是我的代码: string connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\Tool
我在使用 PHP 5.5.11 在 XAMPP 中安装 dBase 扩展时遇到问题。我将 php_dbase.dll 添加到 php/ext 文件夹并将以下行添加到 php.ini 文件: exten
首先,对不起我的英语。 我正在使用 PHP 和 dbase 数据库进行开发。通常我在 Windows 机器上这样做,但有时我必须在 Ubuntu 上开发。问题是,我找不到在 Ubuntu 上创建 od
我想知道如何将 Microsoft dBase 驱动程序的 autocommit 设置为 false,例如 https://stackoverflow.com/a/8079987/613495 中介绍
我已经通过 Matlab Central 进行了谷歌搜索,但找不到任何直接在 Matlab 中打开 DBF 文件的方法。在 TMW 文件交换中有一些对 DBFREAD 函数的引用,但它不再可用。真的有
我无法在 PHP7 和 Nginx 服务器上从 PhpMyadmin 导出数据库。 /var/log/nginx/error.log 说 18 FastCGI sent in stderr: "PHP
我有一个 DBF 文件,我正在尝试从 C# 代码中读取它。我可以在不应用任何条件或为 varchar 类型字段应用条件的情况下成功读取文件。我的问题是我必须从日期字段(类型:日期)中过滤记录。我尝试了
我正在尝试将数据从 Act 2000 转换为 MySQL 数据库。我已经成功地将 DBF 文件导入到单独的 MySQL 表中。但是我遇到了 *.BLB 的问题文件,这似乎是一个非标准的备忘录文件。 D
首先,我必须声明,就 Delphi 而言,我完全是个新手,尽管大约 14 年前我在学校做过一些 Turbo Pascal 编程... 我有一个商业 Delphi 程序,它使用 dBase 数据库和 B
我是一名优秀的程序员,十分优秀!