input string data in the function

function compare_cases(country1,names,days,avg_days,dailycases)
IndexC = strfind(names,'country1');
[row,col] = find(not(cellfun('isempty',IndexC)))
dailydata= dailycases(row,:);
bar(days(1:end-1),dailydata);
end
my workspace has following loaded files
names 332*2 double
days 1*430 datetime
dailycases 332*429
when i call function i have to put the name of the country in place of 'country1'
% calling fuction to trace the data of china in the 'names' folder
compare_cases(China,names,days,430,dailycases)
but it doesnot intake string data
country1 in the function is not taking input an string data
what should be done,

 採用された回答

Adam Danz
Adam Danz 2021 年 5 月 27 日
編集済み: Adam Danz 2021 年 5 月 27 日

0 投票

You're not using the contry1 variable at all in your function. Instead, you've replace it with a character vector 'county1' which is not a variable. Remove the single quotes,
IndexC = strfind(names,country1);
I assume that your first input China is a variable
China = 'China';
Also note that strfind is a case-sensitive search. To avoid problems with cases, consider using
IndexC = strfind(lower(names),lower(country1));
Another problem is that strfind will accept partial matches. For example,
names = {'Korea','North Korea'}; % using "South Korea" would fix this, too
country = 'Korea';
strfind(names, country)
ans = 1×2 cell array
{[1]} {[7]}
To eliminate this problem and to improve code efficiency, consider using ismember or strcmpi
strcmpi(names, country)
ans = 1×2 logical array
1 0
Another big problem is, according to your workspace description, names is a numeric matrix so how do you expect there to be a string match between a word 'China' and numbers?

4 件のコメント

Muhammad
Muhammad 2021 年 5 月 27 日
country1 and contry1 is just typing mistake here i have written correctly in matlab
problem:
when i put directy china instead of country1 and without making fucntion it executes
but when i call country name through function it gives error
here in function country1 is variable and i have to put country name while
calling
Adam Danz
Adam Danz 2021 年 5 月 27 日
What is the entire error message you receive?
Muhammad
Muhammad 2021 年 5 月 28 日
undefined function or variable 'China'
Adam Danz
Adam Danz 2021 年 5 月 28 日
My answer explains the problem. You're not putting quotes around the word china.

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

その他の回答 (0 件)

カテゴリ

質問済み:

2021 年 5 月 27 日

コメント済み:

2021 年 5 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by