how can turn cells to string?

1 Ansicht (letzte 30 Tage)
Yona
Yona am 13 Aug. 2014
Kommentiert: Yona am 21 Aug. 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 Kommentar
Adam
Adam am 13 Aug. 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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Yucheng Ma
Yucheng Ma am 20 Aug. 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 Kommentar
Yona
Yona am 21 Aug. 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by