It is my understanding that you would like to use "switch-case" instead of "if-else" to avoid verbose statements. I am not able to figure out a straightforward way to use "switch-case", but the code may be simplified by implementing the string matching part inside a function. For example, define:
function str = stringMatching(cellarray, dvalue)
stringList = {'pass', 'C2C', 'C2C TO'};
outputList = {'Passive', 'C2C', 'C to C'};
str = '';
stringInCell = cellarray(:, 1);
ind = (cell2mat(cellarray(:, 2)) == dvalue);
for i = 1 : numel(stringList)
if (ismember(stringList{i}, stringInCell(ind))
str = outputList{i};
break;
end
end
Please note that "ismember" is used instead of "strcmp". You can refer to the following documentation for more information on "ismember":
In addition, I use "stringList" to store all the strings that needs to be found in the cell array, and "outputList" to specify the corresponding output strings. Therefore, if you need to match more strings, only "stringList" and "outputList" need to be changed.
With the above function, you may rewrite the "for" loop as follows:
for i = 1 : length(d)
str = stringMatching(a, d(i));
if ~isempty(str)
fprintf('It''s a %s\n', str);
end
end
Best Answer