how can turn cells to string?

11 vues (au cours des 30 derniers jours)
Yona
Yona le 13 Août 2014
Commenté : Yona le 21 Août 2014
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 commentaire
Adam
Adam le 13 Août 2014
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

Connectez-vous pour commenter.

Réponse acceptée

Yucheng Ma
Yucheng Ma le 20 Août 2014
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 commentaire
Yona
Yona le 21 Août 2014
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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by