2016-05-16 89 views
0

我有一個代碼來測試一個網絡的OCR過程。 這:如何將矢量轉換爲標量,用於switch語句?

%% Set new target data (data testing) data2 = fetest8; % data2 = fetest2; % data2 = fetest3; targetx = zeros(1,204); targetx (:,1:50) = 1; targetx (:,51:52) = 2; targetx (:,53:62) = 3; targetx (:,63:82) = 4; targetx (:,83) = 5; targetx (:,84:85) = 6; targetx (:,86:92) = 7; targetx (:,93:95) = 8; targetx (:,96:105) = 9; targetx (:,106) = 10; targetx (:,107:111) = 11; targetx (:,112:116) = 12; targetx (:,117:126) = 13; targetx (:,127:176) = 14; targetx (:,177) = 15; targetx (:,178:180) = 16; targetx (:,181) = 17; targetx (:,182:201) = 18; targetx (:,202:204) = 19; 

prediksi = round(sim (net, data2)); %contain target in matrix, data type: double 

switch (prediksi) 
    case '1' 
     fprintf ('Alif\n'); 
    case '2' 
     fprintf ('Baa\n'); 
    case '3' 
     fprintf ('Haaa\n'); 
    case '4' 
     fprintf ('Daal\n'); 
    case '5' 
     fprintf ('Raa\n'); 
    case '6' 
     fprintf ('Siin\n'); 
    case '7' 
     fprintf ('Saad\n'); 
    case '8' 
     fprintf ('Thaaa\n'); 
    case '9' 
     fprintf ('Ayn\n'); 
    case '10' 
     fprintf ('Faa\n'); 
    case '11' 
     fprintf ('Gaaf\n'); 
    case '12' 
     fprintf ('Kaaf\n'); 
    case '13' 
     fprintf ('Laam\n'); 
    case '14' 
     fprintf ('Miim\n'); 
    case '15' 
     fprintf ('Nuun\n'); 
    case '16' 
     fprintf ('Haa\n'); 
    case '17' 
     fprintf ('Waaw\n'); 
    case '18' 
     fprintf ('Yaa\n'); 
    case '19' 
     fprintf ('Kaaf\n'); 
    otherwise 
     fprintf ('X\n');  
end 

Screenshoot prediksi的: enter image description here

1直至204,prediksi 1x24雙。

enter image description here 我得到這個錯誤:'SWITCH表達式必須是標量或字符串常量。' 如何解決這個錯誤?

任何幫助將非常感激。

+1

'sim(net,data2)'輸出是什麼? – Dan

回答

1

在這裏prediksi將是一個大小爲1xm的行矩陣。所以不能使用switch(prediksi)。相反,你必須使用一個循環,並做switch (prediksi(i))。這裏是更新代碼

%% Set new target data (data testing) data2 = fetest8; % data2 = fetest2; % data2 = fetest3; targetx = zeros(1,204); targetx (:,1:50) = 1; targetx (:,51:52) = 2; targetx (:,53:62) = 3; targetx (:,63:82) = 4; targetx (:,83) = 5; targetx (:,84:85) = 6; targetx (:,86:92) = 7; targetx (:,93:95) = 8; targetx (:,96:105) = 9; targetx (:,106) = 10; targetx (:,107:111) = 11; targetx (:,112:116) = 12; targetx (:,117:126) = 13; targetx (:,127:176) = 14; targetx (:,177) = 15; targetx (:,178:180) = 16; targetx (:,181) = 17; targetx (:,182:201) = 18; targetx (:,202:204) = 19; 

prediksi = round(sim (net, data2)); %contain target in matrix, data type: double 

for i=1:size(prediksi,2) 
switch (prediksi(i)) 
    case 1 
     fprintf ('Alif\n'); 
    case 2 
     fprintf ('Baa\n'); 
    case 3 
     fprintf ('Haaa\n'); 
    case 4 
     fprintf ('Daal\n'); 
    case 5 
     fprintf ('Raa\n'); 
    case 6 
     fprintf ('Siin\n'); 
    case 7 
     fprintf ('Saad\n'); 
    case 8 
     fprintf ('Thaaa\n'); 
    case 9 
     fprintf ('Ayn\n'); 
    case 10 
     fprintf ('Faa\n'); 
    case 11 
     fprintf ('Gaaf\n'); 
    case 12 
     fprintf ('Kaaf\n'); 
    case 13 
     fprintf ('Laam\n'); 
    case 14 
     fprintf ('Miim\n'); 
    case 15 
     fprintf ('Nuun\n'); 
    case 16 
     fprintf ('Haa\n'); 
    case 17 
     fprintf ('Waaw\n'); 
    case 18 
     fprintf ('Yaa\n'); 
    case 19 
     fprintf ('Kaaf\n'); 
    otherwise 
     fprintf ('X\n');  
end 
end 
+0

謝謝,它給我一個輸出,但所有的答案是「X」 沒有人比賽。 prediksi輸出是一個矩陣,包含範圍1-19(targetx值)中的1x204數據。 我還是真的不明白。你的解釋會有幫助。 –

+0

您可以使用變量編輯器中的'prediksi'的屏幕快照更新您的問題。直到我看到'prediksi'中的內容,我才能給出任何解釋。 –

+1

'prediksi'不是一個字符數組,從'case'1''變爲'case 1' – Matt