how can turn cells to string?

1 回表示 (過去 30 日間)
Yona
Yona 2014 年 8 月 13 日
コメント済み: Yona 2014 年 8 月 21 日
I have a list of cells that contain string and i want to try to find if it contain a specify string. i have 4 possibility and on each of them i do something else. i can do in with if-else (a simple example add above). How i can do it but with switch?
a = {'pass' 1;'hp' 1;'C2C' 200; 'hC2C' 200; 'C2C TO' 534; 'hC2C' 534; 'C2C TO' 534; ...
'hp' 1001; 'pass' 1001; 'pass' 1001};
d = [1 200 534 1001];
for i=1:length(d)
if sum(strcmp(a(cell2mat(a(:,2))==d(i)),'pass')) ~= 0
fprintf('It''s a Passive\n')
elseif sum(strcmp(a(cell2mat(a(:,2))==d(i)),'C2C')) ~= 0
fprintf('It''s a C2C\n')
elseif sum(strcmp(a(cell2mat(a(:,2))==d(i)),'C2C TO')) ~= 0
fprintf('It''s a C to C\n')
end
end
  1 件のコメント
Adam
Adam 2014 年 8 月 13 日
Why do you need to use a switch? It's basically just a different form of if-else anyway.
Btw, try using
any( strcmp(a(cell2mat(a(:,2))==d(i)),'pass') )
rather than testing sum equal to 0

サインインしてコメントする。

採用された回答

Yucheng Ma
Yucheng Ma 2014 年 8 月 20 日
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" and "outputList" must be of the same size
stringList = {'pass', 'C2C', 'C2C TO'}; % A list of strings that need to be matched
outputList = {'Passive', 'C2C', 'C to C'}; % The output of the corresponding string in "stringList"
str = ''; % Initialize output
stringInCell = cellarray(:, 1); % put the strings in one cell array
% Use "ind" for the output of "cell2mat(a(:, 2)) == d(i)" in the original code
ind = (cell2mat(cellarray(:, 2)) == dvalue);
% The following loop tests if strings in "stringList" have any match in "cellarray"
% This loop replaces the multiple "if" statements in the original code.
for i = 1 : numel(stringList) % Iterate through all the elements in "stringList"
% Use "ismember" instead of "strcmp" to test if the string can be found in "cellarray"
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
  1 件のコメント
Yona
Yona 2014 年 8 月 21 日
i didn't think to do it like this, but it is a better way for more then 4 cases.
my original idea is to do something like:
for i=1:length(d)
switch a(cell2mat(a(:,2))==d(i))
case 'pass'
fprintf('It''s a Passive\n')
case 'C2C'
fprintf('It''s a C2C\n')
...
end
end
but your way is good too.
thank, Yona

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by