I am unable to compare the multiple strings at once to produce the required output. I have tried the following code. Here I would like to compare the input string and print the result from statements.
function result = select(sports)
sports = {'hockey','tennis'};_
if strcmpi(sports,'hockey');x=1;end
if strcmpi(sports,'cricket');x=2;end
if strcmpi(sports,'football');x=3;end
if strcmpi(sports,'tennis');x=4;end
result
end

2 件のコメント

Rik
Rik 2021 年 1 月 10 日
What is your actual goal? I presume sports={'hockey','tennis'} is actually meant to be the input to your function (instead of overwriting it). If so, what do you want the output to be?
BHARAT CHARAN GOUD MARUPALLI
BHARAT CHARAN GOUD MARUPALLI 2021 年 1 月 11 日
編集済み: BHARAT CHARAN GOUD MARUPALLI 2021 年 1 月 11 日
Thanks for your response. Yes, the input is sports = {'hockey', 'tennis'} and I want the output as [1 4]. I am trying to assign the values to the multiple strings in a matrix form. I am able to do it for a single string, but I want to do it for multiple strings (but not all) at once.

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

 採用された回答

Image Analyst
Image Analyst 2021 年 1 月 10 日

1 投票

Is this what you want (using ismember to find the index of the sport in the list of all the sports?
% Test script:
fprintf('Beginning to run %s.m ...\n', mfilename);
allSports = {'hockey','tennis', 'cricket', 'football', 'racing', 'golf', 'Cody'};
thisSport = 'Football';
result = select(thisSport, allSports)
fprintf('%s is sport #%d.\n', thisSport, result);
fprintf('Done running %s.m.\n', mfilename);
%======================================================
function result = select(thisSport, allSports)
thisSport = lower(thisSport);
allSports = lower(allSports);
[~, result] = ismember(thisSport, allSports)
end

5 件のコメント

BHARAT CHARAN GOUD MARUPALLI
BHARAT CHARAN GOUD MARUPALLI 2021 年 1 月 11 日
Thank you very much. I have added few more 'sports' (or strings) to the variable thisSport, and I am able to compare multiple strings..
BHARAT CHARAN GOUD MARUPALLI
BHARAT CHARAN GOUD MARUPALLI 2021 年 1 月 11 日
This was the first step of my problem, which is to assign the row number (or coulumn number) of the given string in large matrix. My actual problem was to extract a submatrix from a large matrix. My large matrix is given in Fig.1. If my input is = {'Hockey', 'Cricket','Golf','Racing'}, my output should generate a matrix of values as given in Fig.2. Can you please help me on this.
Fig.1
Rik
Rik 2021 年 1 月 11 日
It looks like the code below should work.
result = select(thisSport, allSports);
Sports(result,result)
Image Analyst
Image Analyst 2021 年 1 月 11 日
Please attach the Sports table in a .mat file if you still need help
save('answers.mat', 'Sports');
By the way, select is a built-in function so you might want to call it something else, like SelectSport() instead.
BHARAT CHARAN GOUD MARUPALLI
BHARAT CHARAN GOUD MARUPALLI 2021 年 1 月 12 日
Thank you Rik and Image Analyst, it solved my problem.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および 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