2015-05-09 47 views
0

我有仿真我的实际代码的示例代码。我在parfor循环之外有单元阵列。我必须对字符串进行计算,数字输出将存储在数组中,我可以在每个parfor循环之后将其写入csv文件。所以我制作了虚拟代码。但我无法执行它。错误消息是:“第6行的订阅不匹配”。在matlab中执行parforlop

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 
+0

什么是您的Matlab版本? – Mehrez

+1

我不明白为什么它不应该工作。以下是另外两个注意事项:不要使用'i'作为变量,它已经在与复数相关的matlab中具有含义!此外,多次打开和关闭一个matlabpool是没有意义的。在脚本的开头和结尾处做一次。 – Cat

+0

@MHRZ,@ potAito感谢您的建议,但仍然可以让它工作。 MATLAB版本是2012A – samar

回答

0

那么对于输出中的意外输出和行数,这是因为错误使用了fprintf。我们需要通过元素打印件,这意味着fprintf必须是parfor循环中,所以你的代码应该是这样的:

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; 

输出为:

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 

而且输出具有200行。

1

错误的原因是您正试图将单元分配到第9行 数组我做了一些修改到你的代码,他们在注释中描述。

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 
    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; 
     c{j}=march{1,j}; % changed c(j)= march{1,j}; : cause of error 
     d(j)=randi(200,1,1)/200; 
    end 
    fprintf(ftemp,'%d\t%d\t%c\t%d',a,b,char(c),d); % char(c) in order to convert cell array to array of strings 
    delete(gcp) % there isn't such thing "matlabpool close" , the right expression is "delete(gcp)" 
    clear a b c d; 
    tc=tc+20; 
end 
fclose(ftemp); 
%quit : i remove because it closes matlab , please put it back if u really 
%want to close matlab after operation 

注:如果parpool为您的MATLAB版本doesent工作,请改回表达你使用matlabpool open 4;

+0

它确实执行了程序,但输出wasnt在多行,进一步的字符串不在那里。我在下面发布了我的答案,但输出仍然存在问题。 PLZ看看它 – samar

+0

我增加了另一个解决你的问题的答案,所以请不要忘记接受它作为答案:) – Mehrez

0
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; 
    end 
    fprintf(ftemp,'%d\t%d\t%s\n',a,b,c{:}); % char(c) in order to convert cell array to array of strings 
    clear a b c; 
    tc=tc+20; 
end 
fclose(ftemp); 
matlabpool close; 

但该程序的输出I coudlnt得到它

130 127 Abf5ȱ3³]À@¾ 
1 2  


97 98 cd 
101 102 gh 
105 106 skip 
109 110 op 
113 114 st 
117 118 skip 
67 68 skip 
71 72 IJ 
115 107 ip 
77 78 OP 
81 82 168 57 ®$ «E�,vp|}¤ 
21 22 !"#$%&'(
97 98 cd 
101 102 gh 
105 106 skip 
109 110 op 
113 114 st 
117 118 skip 
67 68 skip 
71 72 IJ 
115 107 ip 
77 78 OP 
81 82 88 125 S([g 0'0Â" 
41 42 +,-./:;< 
97 98 cd 
101 102 gh 
105 106 skip 
109 110 op 
113 114 st 
117 118 skip 
67 68 skip 
71 72 IJ 
115 107 ip 
77 78 OP 
81 82 161 170 7Na�U;6´q¶nl®v¤ 
61 62 [email protected] 
97 98 cd 
101 102 gh 
105 106 skip 
109 110 op 
113 114 st 
117 118 skip 
67 68 skip 
71 72 IJ 
115 107 ip 
77 78 OP 
81 82 171 139 ZF­ºPµ~R(`b[ 
81 82 STUVWXYZ[\]^_`abcd 
97 98 cd 
101 102 gh 
105 106 skip 
109 110 op 
113 114 st 
117 118 skip 
67 68 skip 
71 72 IJ 
115 107 ip 
77 78 OP 
81 82 132 80 ||£)Á1[ªavb- 
101 102 ghijklmnopqrstuvwx 
97 98 cd 
101 102 gh 
105 106 skip 
109 110 op 
113 114 st 
117 118 skip 
67 68 skip 
71 72 IJ 
115 107 ip 
77 78 OP 
81 82 186 63 ª`neepw»fq°r1 
121 122 {|}~� 
97 98 cd 
101 102 gh 
105 106 skip 
109 110 op 
113 114 st 
117 118 skip 
67 68 skip 
71 72 IJ 
115 107 ip 
77 78 OP 
81 82 51 160 M(1´uOAF¦{¬© 
141 142 ���  
97 98 cd 
101 102 gh 
105 106 skip 
109 110 op 
113 114 st 
117 118 skip 
67 68 skip 
71 72 IJ 
115 107 ip 
77 78 OP 
81 82 90 21 <,uZgh�lu¨B,¦ 
0 
161 162 £¤¥¦§¨©ª«¬­®¯°±²³´ 
97 98 cd 
101 102 gh 
105 106 skip 
109 110 op 
113 114 st 
117 118 skip 
67 68 skip 
71 72 IJ 
115 107 ip 
77 78 OP 
81 82 192 160 o£¿^hyµÄ¨�Ä�O 
181 182 ·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈ 
97 98 cd 
101 102 gh 
105 106 skip 
109 110 op 
113 114 st 
117 118 skip 
67 68 skip 
71 72 IJ 
115 107 ip 
77 78 OP 
81 82 

我的意思是说,为什么只有124次迭代发生,而不是200次,以及为什么这些任意输出在第3列