使用matlab将excel文件写入目录中的许多文件夹

时间:2015-07-24 21:59:05

标签: excel matlab

我需要将一个excel文件写入D:\下的许多文件夹(文件夹*)并循环遍历它们以进一步单独处理。

我长时间尝试以下代码..

  srcFolders = dir('D:\folder*');
for i = 1 : length(srcFiles)
    filename = strcat(path,'\',srcFiles(i).name);
    xlswrite('srcFolders\filename.xls', srcFolders(folder).name,'Sheet1', folder_range); 
end 

1 个答案:

答案 0 :(得分:1)

我假设您想要将不同的数据集写入Excel文件,并且遇到了一些麻烦。如果这不是准确的评论,请告诉我。

%Just generating some arbitrary data
data = arrayfun(@(m,n) rand(m,n), randi(50, 9, 1), randi(50, 9, 1), 'uni', 0);

%The base file path
path = 'D:';

%The pattern used to choose the folders
folderPattern = 'folder*';

%The files you want to write to
srcFiles = {'A', 'B', 'D'};

%And the extension
ext = '.xlsx';

%Get the path of the folders
srcFolders = dir(fullfile(path, folderPattern));
srcFolders = fullfile(path, {srcFolders.name});

%Construct the full file path
fullPath = cellfun(@(f) fullfile(f, strcat(srcFiles(:), ext)), srcFolders, 'uni', 0);
fullPath = vertcat(fullPath{:});

%Write the data to the files
for i = 1:length(fullPath)
    xlswrite(fullPath{i}, data{i})
end

编辑:

基于OP的Feed。

注意第二次大修。我没有测试过这个,我已经完成了。这对于一个问题来说很重要,而且你正在滥用这个网站的工作方式。

%The base file path
path = 'D:';

%Relative reference to reference images
ImageRefLocations = {'Ref\RefI1.jpg', 'Ref\RefI2.png', 'Ref\RefI3.jpg'};
%Base Image location
baseImage = fullfile(path, ImageRefLocations(:));

%The pattern used to choose the folders
folderPattern = 'folder*';

%The image extension
[~, ~, ext] = unique(cellfun(@(f) fileparts(f), baseImage, 'uni', 0));

%The excel extension
ExcelExt = '.xlsx';

msg = {' = no'; ' = ok'};

i0 = cellfun(@(f) imread(f), baseImage, 'uni', 0);

%Get the path of the folders
srcFolders = dir(fullfile(path, folderPattern));
isDir = [srcFolders.isdir];
srcFolders = {srcFolders(isDir).name}';
PathedFolders = fullfile(path, srcFolders);
excelFiles = fullfile(path, strcat(srcFolders, ExcelExt));

for i = 1:length(PathedFolders)
    f = PathedFolders{i};
    Images = cellfun(@(x) dir(fullfile(f, ['*', x])), ext, 'uni', 0);
    Images = vertcat(Images{:});

    if isempty(Images)
        continue
    end

    Images = {Images.name}';
    [~, ImageNames, ~] = cellfun(@(x) fileparts(x), Images, 'uni', 0);
    ImageList = fullfile(f, Images);

    match = zeros(size(ImageList));

    for j = 1:length(ImageList)
        image = imread(ImageList{j});

        for k = 1:length(i0)
            if ~all(size(image) == size(i0{k}))
                continue
            end

            match(j) = all(image(:) == i0{k}(:));

            if match(j)
                continue
            end
        end
    end

    matchMessage = strcat(Images, msg(match + 1));

    xlswrite(excelFiles{i}, matchMessage)
end