1

I'm fairly new at MATLAB, so forgive if I'm saying stuff that is completely wrong. I am trying to write a little program that reads in the files from a certain directory with extension .xlsx and then assigns the columns to vectors. When I manually give up the file names, my program looks like this:

files = {'130926','130927'}
file_number = 1;
file_amount = length(files);
while file_number <= file_amount
file_name = files(file_number);
cd('C:\place_where_I_store_my_files');
A = xlsread(char(strcat(file_name)));
J = A(:,1);
J_sp = A(:,2);
file_number = file_number + 1
end

I've tried a lot of different things to read in the files automatically among wich:

files = {'*.xlsx'}

But it all yields errors. Thanks for reading.

Matthias

4

3 回答 3

1

将该dir函数与通配符搜索一起使用。

my_path = 'C:\place_where_I_store_my_files';
xlsfiles = dir(fullfile(my_path, '*.xlsx'));
for ii = 1 : length(xlsfiles)
  disp(fullfile(my_path, xlsfiles(ii).name));
end

上面的代码会显示你指定的目录下所有xlsx文件的名称。

于 2013-09-27T18:39:30.170 回答
0

仅供参考,这是为我做的:

files = dir('C:\Users\Matthias\Desktop\KUL\2e_Master\Thesis\MBR_data\MBR_online\*cs.xlsx')
file_number = 1;
file_amount = length(files);
while file_number <= file_amount
file_name = files(file_number).name;
cd('C:\place_where_I_store_my_files');
A = xlsread(char(file_name));
J = A(:,1);
J_sp = A(:,2);
file_number = file_number + 1
end

问候, 马蒂亚斯

于 2013-10-10T10:43:19.543 回答
0

这个怎么样 :-

>> list = dir('C:\path\*.xlsx');
>> names=cellfun(@(x)x(1:end-5),{list.name},'UniformOutput', false);

访问使用names{1}, names{2}... 等等

这将创建一个匿名函数,该函数适用于从dir命令接收到的名称单元格。

键入 :help dirhelp cellfun在命令提示符下获取更多详细信息

于 2013-09-27T18:43:01.430 回答