Title: How to convert vector to scalar, for switch statement?
Tags: matlab;switch-statement;ocr;scalar
Question: I have a code to test a network for OCR process.
Here it is:
%% 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');
Screenshoot of prediksi:
1 until 204, prediksi 1x24 double.
I got this error: 'SWITCH expression must be a scalar or string constant.'
How to fix this error?
Any help would be very appreciate.
Comment: What does `sim (net, data2)` output?

Here is the accepted answer: In here prediksi will be a row matrix of size 1xm. So cant use switch(prediksi). Instead you have to use a loop and do switch (prediksi(i)). Here is the updated code
%% 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');
Comment for this answer: Thank you, it give me an output, but all the answer is "X"
No one is match.
prediksi output is a matrix contain 1x204 data in range 1-19 (targetx value).
I still really didn't understand. Your explanation would be helpful.
Comment for this answer: It works.
The code give an appropriate output.
Thank you so much @Rijul
Comment for this answer: Can you please update your question with a screen shot of the `prediksi` in variable editor. I cant give you any explanation until I see what is in `prediksi`.
Comment for this answer: @Matt You are right. I didn't notice it. Thanks for reviewing the code.
Comment for this answer: `prediksi` is not a character array, change from `case '1'` to `case 1`


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2016:00:05/16/16', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<matlab><switch-statement><ocr><scalar>', 'Title': 'How to convert vector to scalar, for switch statement?', 'ViewCount': 1064, 'id': 37249500, 'user_AboutMe': None, 'user_AccountId': 7580222.0, 'user_DisplayName': 'Ana Ain', 'user_Reputation': 163.0, 'user_Views': 73.0, 'user_id': 5751128.0}}