Search code examples
matlabparfor

execution of parforlop in matlab


I have sample code emulating my actual code. Where I have cell arrays outside the parfor loop. I have to perform computations on strings and numerical outputs will be stored in arrays which I can write to a csv file after each parfor loop. So I made dummy code. But I couldn't get it to execute. The error message is: "subscription mismatch at line 6".

ftemp=fopen('temp.csv','w');
    march=cell(1,20);tc=0;
    march={'ab' 'cd' 'ef' 'gh' 'ij' 'kl' 'mn' 'op' 'qr' 'st' 'uv' 'AB' 'CD' 'EF' 'GH' 'IJ' 'KL' 'MN' 'OP' 'QR'};
    for i=1:10
        matlabpool open 4;
        parfor j=1:1:20
        a(j,1)=randi(200,1,1);
        b(j,2)=j+tc;
        c(j,3)=march{1,j};
        d(j,4)=(randi(200,1,1)/200);
        end
        fprintf(ftemp,'%d\t%d\t%s\t%f',a,b,c,d);
        matlabpool close
        clear a b c d;
        tc=tc+20;
    end
    fclose(ftemp);
    quit

Solution

  • Well for the unexpected outputs and number of rows in the output , it's because the wrong use of fprintf . We need to print element by element , which means the fprintf has to be inside the parfor loop, so your code should look like this:

    ftemp=fopen('temp.csv','w');
    march=cell(1,20);tc=0;
    march={'ab';'cd';'ef';'gh';'ij';'klm';'mn';'op';'qr';'st';'uv';'ABls';'CD';'E3F';'GH';'IJ';'dynaKL';'MN';'OP';'QR'};
    matlabpool open 4;
    for k=1:10
        %parpool local; % here in my version of Matlab 2015 there are no more "matlabpool" if it doesen't work change it back
        parfor j=1:20
            %i changed variable a(j,1) into a(j) and b(j,2) into b(j): may
            %contain empty arrays , idem: for c and d
            a(j)=randi(200,1,1);
            b(j)=j+tc;% indexing purposes to identify the order when parallel processing going on
            if length(march{j})>2 % this kind of conditions and computations are there in my actual code
                c{j}='skip';
            else
                c{j}=march{j};
            end
            %d(j)=randi(200,1,1)/200;
            fprintf(ftemp,'%d\t%d\t%s\t\n',a(j),b(j),c{j}); % char(c) in order to convert cell array to array of strings
        end
        clear a b c;
        tc=tc+20;
    
    end
    fclose(ftemp);
    matlabpool close;
    

    the output is:

    9   1   ab
    171 2   cd
    7   3   ef
    98  4   gh
    102 5   ij
    20  6   skip
    53  7   mn
    174 8   op
    36  9   qr
    30  10  st
    130 11  uv
    127 12  skip
    133 13  CD
    65  14  skip
    118 15  GH
    130 16  IJ
    139 17  skip
    168 18  MN
    57  19  OP
    25  20  QR
    22  21  ab
    39  22  cd
    83  23  ef
    22  24  gh
    159 25  ij
    40  26  skip
    164 27  mn
    78  28  op
    194 29  qr
    88  30  st
    125 31  uv
    1   32  skip
    2   33  CD
    112 34  skip
    161 35  GH
    170 36  IJ
    55  37  skip
    59  38  MN
    18  39  OP
    134 40  QR
    80  41  ab
    118 42  cd
    108 43  ef
    174 44  gh
    97  45  ij
    157 46  skip
    85  47  mn
    98  48  op
    40  49  qr
    11  50  st
    171 51  uv
    139 52  skip
    90  53  CD
    70  54  skip
    173 55  GH
    150 56  IJ
    186 57  skip
    155 58  MN
    136 59  OP
    96  60  QR
    158 61  ab
    118 62  cd
    124 63  ef
    127 64  gh
    26  65  ij
    124 66  skip
    91  67  mn
    186 68  op
    63  69  qr
    137 70  st
    170 71  uv
    98  72  skip
    132 73  CD
    80  74  skip
    160 75  GH
    20  76  IJ
    156 77  skip
    142 78  MN
    110 79  OP
    51  80  QR
    18  81  ab
    29  82  cd
    40  83  ef
    49  84  gh
    102 85  ij
    113 86  skip
    96  87  mn
    44  88  op
    166 89  qr
    90  90  st
    21  91  uv
    60  92  skip
    44  93  CD
    166 94  skip
    103 95  GH
    123 96  IJ
    77  97  skip
    163 98  MN
    138 99  OP
    111 100 QR
    94  101 ab
    133 102 cd
    158 103 ef
    13  104 gh
    26  105 ij
    117 106 skip
    90  107 mn
    58  108 op
    156 109 qr
    79  110 st
    196 111 uv
    168 112 skip
    192 113 CD
    160 114 skip
    56  115 GH
    129 116 IJ
    191 117 skip
    157 118 MN
    170 119 OP
    30  120 QR
    137 121 ab
    128 122 cd
    12  123 ef
    38  124 gh
    38  125 ij
    122 126 skip
    97  127 mn
    129 128 op
    142 129 qr
    154 130 st
    99  131 uv
    85  132 skip
    129 133 CD
    111 134 skip
    108 135 GH
    78  136 IJ
    102 137 skip
    48  138 MN
    100 139 OP
    89  140 QR
    114 141 ab
    12  142 cd
    184 143 ef
    145 144 gh
    5   145 ij
    48  146 skip
    92  147 mn
    64  148 op
    87  149 qr
    32  150 st
    136 151 uv
    103 152 skip
    90  153 CD
    73  154 skip
    28  155 GH
    191 156 IJ
    63  157 skip
    120 158 MN
    104 159 OP
    178 160 QR
    148 161 ab
    36  162 cd
    129 163 ef
    11  164 gh
    172 165 ij
    186 166 skip
    145 167 mn
    142 168 op
    150 169 qr
    185 170 st
    14  171 uv
    141 172 skip
    22  173 CD
    163 174 skip
    48  175 GH
    164 176 IJ
    117 177 skip
    25  178 MN
    110 179 OP
    111 180 QR
    175 181 ab
    60  182 cd
    195 183 ef
    44  184 gh
    163 185 ij
    4   186 skip
    103 187 mn
    95  188 op
    127 189 qr
    10  190 st
    11  191 uv
    182 192 skip
    162 193 CD
    179 194 skip
    76  195 GH
    104 196 IJ
    153 197 skip
    103 198 MN
    4   199 OP
    154 200 QR
    

    Also the output has 200 rows.