【问题标题】:Creating EXCEL File with parfor command in MATLAB for parallel processing在 MATLAB 中使用 parfor 命令创建 EXCEL 文件以进行并行处理
【发布时间】:2014-09-09 03:22:39
【问题描述】:

我想在 parfor 循环之外(在代码的开头)创建 excel 文件,然后在每个循环中更新 excel 文件,最后在循环完成后将 excel 文件存储到特定位置。但我遇到了一些错误。使用以下命令:

   matlabpool('open',2);
   pwd='C:\Users\myPC\Desktop';
   fName = fullfile(pwd, 'file.xls');
   %# create Excel COM Server

   Excel = actxserver('Excel.Application');
   Excel.Visible = true;
   %# create new XLS file
   wb = Excel.Workbooks.Add();
   wb.Sheets.Item(1).Activate();%line 10
   offset = 0;

   C1 = {'NAME', 'Max', 'Min','Average'};
   %# calculate cell range to fit matrix (placed below previous one)
   cellRange = xlcalcrange('A1', offset,0, size(C1,1),size(C1,2));
   offset = offset + size(C1,1);
   %# insert matrix in sheet
   Excel.Range(cellRange).Select();
   Excel.Selection.Value =C1;


   parfor i=1:2
   %some code ,  eg :
   MAX =1
   MIN =2
   AVG=3
   name='jpg'
   row2  = { name MAX MIN AVG };
        %# calculate cell range to fit matrix (placed below previous one)
        cellRange = xlcalcrange('A1', offset,0, size(row2,1),size(row2,2));
        offset = offset + size(row2,1);
        Excel.Range(cellRange).Select();  %line32
        Excel.Selection.Value =row2;

   end

   %# parsave XLS file
   wb.SaveAs(fName,1);
   wb.Close(false);

   %# close Excel
   Excel.Quit();
   Excel.delete();
   matlabpool('close');

以上代码显示以下错误: 1. parfor中的变量Excel不能分类, 2. 由于变量“偏移”的使用方式,PARFOR 循环无法运行。 3. 由于使用变量“Excel”的方式,PARFOR 循环无法运行。, 4. 'Excel' 的有效索引在 PARFOR 循环中受到限制。 5.调用被callee拒绝。第10行错误(wb.Sheets.Item(1).Activate();)。

Pls. help to use above code so that I can create excel file which updates inside PARFOR loop and EXCEL file get saved outside PARFOR loop

【问题讨论】:

    标签: excel matlab parallel-processing parfor


    【解决方案1】:

    从不同线程同时写入文件是不安全的。这就是当您尝试执行此类操作时 MATLAB 会自动出错的原因。

    这个问题的答案将是两个之一:

    1. parfor 循环内,将您的输出写入某种独立的缓冲区,然后在parfor 循环外将缓冲区串行写入文件。或者,
    2. 不要使用parfor。请改用for

    请参阅:http://blogs.mathworks.com/loren/2009/10/02/using-parfor-loops-getting-up-and-running/ 以了解有关 parfor 命令限制的更多信息。

    【讨论】:

    • 太好了,很高兴我能帮上忙!
    猜你喜欢
    • 1970-01-01
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多