简体   繁体   English

我想将多个Matlab文件的名称导出到.csv中

[英]I want to export the name of multiple matlab files into a .csv

I want to use a for loop to export the names of a folder of .mat files into one .csv file by row. 我想使用for循环将.mat文件的文件夹的名称逐行导出到一个.csv文件。

I am trying to use xlswrite, but don't understand how to access the filename of the .mat files. 我正在尝试使用xlswrite,但不了解如何访问.mat文件的文件名。

I am working with to write them to the csv. 我正在与他们写到CSV。

xlswrite(fullfile(dest_dir,'result.csv'), FILENAME HERE, ['A' num2str(i)]);

To get your filename, load the files using dir command, read them individually in your for loop, and then write them to file. 要获取文件名,请使用dir命令加载文件,在for循环中逐个读取它们,然后将其写入文件。 (I think I already posted something like this?) Here is the prototype: (我想我已经发布了类似的东西?)这是原型:

files = dir('*.mat');               % obtain all files with .mat extenstion
fid = fopen('result.csv','a');   

for k = 1:length(data)
    filename = files(k).name;       % get the filename
    fprintf(fid, '%s,\n', filename);
end

fid = fclose(fid);

Unfortunately, you cannot just save the file names as strings in a cell and write it as you might a matrix using say csvwrite , so you can just do this. 不幸的是,您不能仅将文件名另存为字符串并存储在单元格中,就可以像使用csvwrite这样将其写入矩阵,因此可以执行此操作。

xlswrite creates a excel workbook xlswrite创建一个Excel工作簿

With dir command you can get a structure whose one of field is name of file. 使用dir命令,您可以获得一个结构,其字段之一是文件名。

Using simple file IO function your requirement can be met. 使用简单的文件IO功能,可以满足您的要求。

I think either of this will do what you need: 我认为这两种都可以满足您的需求:

fid=fopen('result.csv','wt');

files=dir('*.mat');
x={files(:).name};

csvFun = @(str)sprintf('%s,',str);
xchar = cellfun(csvFun, x,'UniformOutput', false);
xchar = strcat(xchar{:});
xchar = strcat(xchar(1:end-1),'\n');

fprintf(fid,xchar);

fclose(fid);

Or 要么

If you just need .mat names in a column form: 如果只需要列格式的.mat名称:

fid=fopen('result.csv','wt');

files=dir('*.mat');
x={files(:).name};

[rows,cols]=size(x);

for i=1:rows
  fprintf(fid,'%s,',x{i,1:end-1});
  fprintf(fid,'%s\n',x{i,end});
end

fclose(fid);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM