matlab 有关 addpath(genpath(fullfile(''))) 及matlab对文件目录路径的操作,转

fullfile();

利用文件各部分信息创建[1]合成完整文件名。

用法:

fullfile('dir1', 'dir2', ..., 'filename')

f = fullfile('dir1', 'dir2', ..., 'filename')

解释:若文件'100.hea' 的路径为'D:\matalab\xindianshuju\wenjian\100.hea',那么 'dir1'='D:';'dir2'='matlab';'dir3'='wenjian';‘dir4'='xindianshuju';'filename'='100.hea'

genpath();

P = genpath(D) returns a path string starting in D, plus, recursively, all the subdirectories of D, including empty subdirectories.

addpath();

addpath DIRNAME prepends the specified directory to the current

matlabpath. Surround the DIRNAME in quotes if the name contains a

space. If DIRNAME is a set of multiple directories separated by path

separators, then each of the specified directories will be added.

已有 605 次阅读 2012-5-9 21:28 |个人分类:matlab工具|系统分类:科研笔记|关键词:目录 f Windows matlab 版本号

1、 filesep

用于返回当前平台的目录分隔符,Windows是反斜杠(\),Linux是斜杠(/)。

2、 fullfile

用于将若干字符串连接成一个完整的路径。例如:

>> f=fullfile('D:','Matlab','example.txt')

f=D:\Matlab\example.txt

(在Windows中,“D:\”表示D盘,“D:”表示目录)

3、 fileparts

用于将一个完整的文件名分割成4部分:路径,文件名,扩展名,版本号。例如:

>> f=fullfile('D:','Matlab','example.txt');

>>[pathstr,name,ext,versn]=fileparts(f)

pathstr=D:\Matlab

name=example

ext=.txt

versn=’’

4、 pathsep

返回当前平台的路径分隔符。Windows是分号(;),Linux是冒号(:)。

5、 exist

可以用于判断目录或者文件是否存在,同时不同的返回值有不同的含义。例如:

>> f=fullfile('D:','Matlab','example.txt');

>>exist(f)

ans=2

>>exist('D:\Matlab')

ans =7

6、 which

可以通过一个函数或脚本名称得到它的完整路径,同时还能处理函数重载的情况,例如:

>> which abs(0)

C:\MATLAB7\toolbox\matlab\elfun\@double\abs.bi % double method

>> which abs(single(0))

C:\MATLAB7\toolbox\matlab\elfun\@single\abs.bi % single method

7、 isdir

判断一个路径是否代表了一个目录,例如:

>> p='D:\Matlab';

>> f=fullfile(p,'example.txt');

>> isp=isdir(p)

isp=1

>> isf=isdir(f)

isf=0

8、 dir

用于列出一个目录的内容,返回值为结构体数组类型,包含如下部分:name:文件或目录的名称;date:修改日期;bytes:文件大小;isdir:是否是目录。例如:

>> p='D:\Matlab';

>>files=dir(p)

files =

8x1 struct array with fields:

name

date

bytes

isdir

9、 cd

用于切换当前工作目录。例如:

>>cd('c:/toolbox/matlab/demos') %切换当前工作目录到demos

>>cd .. %切换当前工作目录到matlab

10、 pwd

用于当前工作目录的路径。例如:

>> pwd

ans =C:\MATLAB7\work

11、 path

用于对搜索路径的操作。例如:

<<path %查询当前所有的搜索路径(MATLABPATH)

<<p=path %把当前的搜索路径存在字符串变量p中

<<path(‘newpath’) %将当前搜索路径设置为newpath

<<path(path,’newpath’) %向路径添加一个新目录newpath

<<path(’newpath’, path) %向当前搜索路径预加一个新目录nespath

12、 addpath和rmpath

用于对matlab搜索路径的添加和删除。例如:

<<addpath(‘directory’) %将完整路径directory加入到当前搜索路径的最顶端

<<rmpath

13、 what

用于显示出某目录下存在哪些matlab文件;若输入完整路径,可列出指定目录下的文件。例如:

<<what

<<what dirname

<<what(‘dirname’)

其中dirname是要查找的路径的名字,路径在matlab的搜索路径内时,没有必要输入全名,只输入最后或最后两级就够了。

14、 path2rc

保存当前matlab的搜索路径到pathdef.m文件中。

转自:matlab对文件目录路径的操作

http://www.ilovematlab.cn/thread-164384-1-1.html

在一个matlab的学习群里,有人提出了这么一个问题:在matlab里如何读取多信文件的数据,例如,有许多数据,存放在不同的txt文件里。

我的办法是把这些文件的文件名按一定的规律命名,假如有一百个数据文件,则可以把这些文件分别命名为:filename1.txt,filename2.txt,...,fielname100.txt,在读取的时候则可以使用循环:

for i = 1:100

fileName = ['filename' num2str(i) '.txt'];

x = load(filiName);

end

但另一位朋友给出了另一个解决的办法,无需对数据文件的文件名进行修改,就是文件名无须有规律,他的办法是:

A = dir(fullfile('d:\datafile','*.txt'));

这个语句是把存放数据文件的目录d:\datafile下的所有txt文件列出来,并把这些文件名的信息存放到一个变量A中,A是一个结构体变量,只要对A进行循环就可以读取到所有文件的数据了。