matlab读取文件夹中所有文件

matlab读取文件夹中所有文件,第1张

写一个for循环

每次修改文件

for i=1:803

if i<=9

name=['JULY7000',num2str(i),'trtx'];

elseif i<99

name=['JULY700',num2str(i),'trtx'];

else

name=['JULY70',num2str(i),'trtx'];

end

end

%%%%%%%%%%%%%%%%%%

问题补充:目前关键问题是怎么把filenames中的文件名'JULY70001trtx'带入到[x,y]=textread('e:\datas\JULY70001trtx','%f ; %f','headerlines',2);命令中读数据

%%%%%%%%%%%%%%%%%%%

这不简单吗

filenames=

patchname='e:\datas\';

fullname=[patchname,filenames]

[x,y]=textread(fullname,'%f ; %f','headerlines',2);

我不知道你这个函数用对了没有,

但是前面那几句话就可以实现文件名拼接

使用Matlab搜索特定后缀名的文件,把该文件所在的文件夹路径提取出来方法如下:

加入文件夹搜索路径 -- addpath (Add folders to search path)

ddpath('folderName1','folderName2','folderName3' )

addpath('folderName1','folderName2','folderName3' flag)

addpath folderName1 folderName2 folderName3 -flag)

previous_path = addpath()

这里的flag为

'-begin' :Add specified folders to the top of the search path

'-end' :Add specified folders to the bottom of the search path

'-frozen' :Disables change detection for folders you add to the path, which conserves Windows change notification resources (Windows only) Typehelp changenotification in the Command Window for more information

例子:

Add c:/matlab/myfiles to the top of the search path:addpath('c:/matlab/myfiles')Add c:/matlab/myfiles to the end of the search path:addpath c:/matlab/myfiles -endAdd myfiles and its subfolders to the search path:addpath(genpath('c:/matlab/myfiles'))

On Windows, Add myfiles to the top of the search path, disable folder change notification, and display the search path before adding myfiles:previous = addpath('c:/matlab/myfiles', '-frozen')

去除文件夹搜索路径 rmpath (Remove folders from search path)

rmpath('folderName') removes the specified folder from the search path Use the full path forfolderName

rmpath folderName is the command form of the syntax

如果想把该目录下的所有子目录都添加到路径中去,也不用把所有的子目录逐一addpath,用genpath函数:

Use genpath in conjunction with addpath to add a folder and its subfolders to the search path Add myfiles and its subfolders to the search path:

addpath(genpath('c:/matlab/myfiles')

我这里有一个读取的实例你可以看一下

%%

格式化文本的读 *** 作

%只读形式打开txt文件

file_t

=

fopen('mytxttxt','r');

%以十进制读取,且读取的数据自动排成一列,排的顺序为:先从第一行左边到第一行右边,然后排第二行

A

=

fscanf(file_t,'%d');

%关闭文件

fclose(file_t);

%%

使用textscan读取多列数据

file_t

=

fopen('mytxttxt','r');

%将原来的两列数据以数组原包(cell)的形式读取,cell共有两个元素

A

=

textscan(file_t,'%d

%d');

%C和上面A一样,D返回位置信息

[C,D]

=

textscan(file_t,'%d

%d');

fclose(file_t);

A{1}

%原包数据的第一个元素对应第一列

A{2}

C

D

%%

textread函数读取,现在不常用

%这种形式将每一列分别给A,B

[A,B]

=

textread('mytxttxt','%d

%d');

A

B

%这种形式将txt文件排成一列赋给C

C

=

textread('mytxttxt','%d');

C

%%

忽略标题

file_t

=

fopen('headlinetxt','r');

%忽略掉第一行的标题信息

A

=

textscan(file_t,'%d

%d','HeaderLines',1);

A

%%

使用textscan扫描字符串中的数据

clc

str_1

=

'The

number

is

1

2

3

4

5';

%首先使用textscan获取第一个前14个字符

[str1,position1]

=

textscan(str_1,'%14c',1);

str1{:};

%The

number

is

position1;

%14

%获取字符串的长度

[temp1,temp2]

=

size(str_1);

%然后读取后面的数字字符串

str_2

=

textscan(str_1(position1+1:temp2),'%9c',1);

%将字符串转化为数值

num

=

str2num(str_2{1})

%%

格式化文本的写 *** 作

%使用fprintf向文件中写入数据

%写形式打开文件,存在就打开,不存在新创建一个文件开始写

file_1

=

fopen('text_wtxt','w');

%以数字形式写入数据

fprintf(file_1,'%d',1225);

%关闭文件,返回0表示关闭成功

fclose(file_1);

%%

每写入一次换行或插入想要的字符

file_1

=

fopen('text_wtxt','w');

%\r回车符

\n换行符

这里必须回车换行连用

fprintf(file_1,'%d\r\n',[32;34]);

%每写入一个数字,后加一个空格,多列按列输出

temp

=

randint(4,2);

fprintf(file_1,'%d

',temp);

fclose(file_1);

%%

fprintf在命令空间输出

str_1

=

'Hello!

World!';

%这里fid

=

1;这时输出换行只需\n就行,%c为输出单个字符,%s为输出字符串

fprintf(1,'%c\n',str_1);

%%

扫描字符串2

clear

clc

str

=

'1985

112

-1053';

%将

替换为0

A

=

find(str

==

32);

str(A)

=

48;

%下面这这一句相当于+198501120-1053

%不是你给的+19850112-01053

str2num(str)

使用fid直接加入路径读取即可;

fid = fopen('X:\路径\JPG');

Matlab使用dir函数获得指定文件夹下的所有子文件夹和文件,并存放在在一种为文件结构体数组中

dir函数可以有调用方式为:

dir('') 列出当前目录下所有子文件夹和文件;

dir('G:\Matlab') 列出指定目录下所有子文件夹和文件;

dir('m') 列出当前目录下符合正则表达式的文件夹和文件;

得到的为结构体数组每个元素都是如下形式的结构体:

name -- filename

date -- modification date

bytes -- number of bytes allocated to the file

isdir -- 1 if name is a directory and 0 if not

datenum -- modification date as a MATLAB serial date number

分别为文件名,修改日期,大小,是否为目录,Matlab特定的修改日期

可以提取出文件名以作读取和保存用

方法:

file/import data/next/finish

>> whos

Name Size Bytes Class

data 5x4 160 double array

textdata 4x1 300 cell array

Grand total is 54 elements using 460 bytes

>> data

data =

1 11 111 1111

2 22 222 2222

3 33 333 3333

4 44 444 4444

5 55 555 5555

>> textdata

textdata =

'你好'

'欢迎来到'

'论坛'

'educn'

Matlab批量读取一个文件夹里的txt文件的方法。

如下参考:

1首先,在这里举例制作一个txtde文本文件,如下面页面的内容。

2将文本文件放入matlab的工作路径中,方便读取 *** 作。例如,如果我的文本文件在桌面上,我可以将matlab的工作路径更改为桌面。具体方法是点击matlab的省略号后的当前工作路径,d出选项选择文件夹,然后选择相应的路径。

3接下来,我们使用importdata函数来读取文件。例如,我想读取一个名为data的文本文件,我可以在命令窗口中输入:data=IMPORTDATA('datatxt')。

4结果表明,数据是一个结构化数组。在这一点上,我们可以看看结构的每个部分代表什么。如下图所示,第一个数组表示文本文件的数量,第二个和第三个数组表示文本文件中的汉字。此时,每个人都可以根据需要引用适当的数组。

5除了上述功能,您还可以使用tex胎面函数来读取。使用该特性时,可以使用[a,b,c,d]=tex胎面('data')来指定输出的每个部分的格式。txt','%2s%。3f%。3f%3f”)。

文本中的第二个引号表示输出的格式。例如,%2s表示第一列的输出格式是单元格数组%。3f表示输出数字保留三位小数。

以上就是关于matlab读取文件夹中所有文件全部的内容,包括:matlab读取文件夹中所有文件、怎么用MATLAB查找文件、如何在matlab中读取TXT数据文件等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/10093174.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-05
下一篇2023-05-05

发表评论

登录后才能评论

评论列表(0条)

    保存