Error on Switch/case with cell array char data
6 次查看(过去 30 天)
显示 更早的评论
Hello I am getting "SWITCH expression must be a scalar or a character vector." when i run my code bellow. I want to take an typed input from the user split it into individual cells and then check the variable with the created switch case concatenating the resulting letter's probability into a new variable. I know it has something to do to converting the inputted cell values into a character vector but I haven't found any good way to do it. Any help would be appreciated
get = input('type(letters only, no puncuation): ' , 's');
split = num2cell(get);
sorted_split = sort(split);
input_prob = [];
for n = 1 : length(sorted_split)
switch sorted_split{n}
case ' '
horzcat(input_prob,0.1859);
case 'a'
horzcat(input_prob,0.0642);
case 'b'
horzcat(input_prob,0.0127);
end
end
...
2 个评论
Cris LaPierre
2020-11-21
Your code runs for me without giving the error message you mention. Can you share exactly what you are using for input to create the error? Also, please copy/paste the error message here (all the red text). I wonder if the error may be coming from code that you have not shared here.
采纳的回答
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!