无论您运行的平台如何,对处理文件的函数强制区分大小写的一种方法是为此类函数编写一个包装器。
例如,在 的情况下load
,我想出了以下直接替换:
function varargout = myload(fname, varargin)
% make sure filename ends with MAT extension
[~,~,ext] = fileparts(fname);
if isempty(ext), fname = [fname '.mat']; end
% open file (searching entire MATLAB path)
fid = fopen(fname,'r');
if fid < 0, error('file not found'); end
% get fullpath to opened file, and close file handle
filename = fopen(fid);
fclose(fid);
% extract the filename
[~,name,ext] = fileparts(filename);
filename = [name ext];
% compare against original name (case-sensitive)
if ~strcmp(fname,filename)
error('Cannot find an exact (case-sensitive) match for file');
end
% load the MAT-file
S = load(fname, varargin{:});
% assign output
if nargout > 0
varargout{1} = S;
else
fn = fieldnames(S);
for i=1:numel(fn)
assignin('caller', fn{i}, S.(fn{i}))
end
end
end
在上面的实现中我可能错过了一些案例,但你明白了..