MATLAB Answers

Excel I/o matlab

1,282 views (last 30 days)
Priyamvada Shankar
Priyamvada Shankar on 24 Mar 2019
Edited: Mulayam Singh Choudhary on 18 Jun 2020 at 23:10
The attached Distances.xlsx file contains a spreadsheet with the pairwise distances in miles of the top 100 US cities by population. A preview of the spreadsheet is shown below. The first row and first column contain the city names using the following format: city name comma space capitalized state abbreviation, e.g., Nashville, TN. Note that the very first cell of the spresheet, A1, is blank. Write a function called get_distance that accepts two character vector inputs representing the names of two cities. The function returns the distance between them as an output argument called distance. For example, the call get_distance('Seattle, WA','Miami, FL') should return 3723. If one or both of the specified cities are not in the file, the function returns -1. Preview of the first five cities of Distances.xlsx

  17 Comments

madhan ravi
madhan ravi on 13 Jun 2019
Where is the data file?
Jaimin Motavar
Jaimin Motavar on 2 Jul 2019
you should use strcmp function instead of contains.
function distance = get_distance(x,y)
[~,~,raw] = xlsread('Distances.xlsx');
[a b]=size(raw);q=0;w=0;
for i = 2:a
if strcmp(raw{i,1},x)==1
q=i;
end
end
for j = 2:b
if strcmp(raw{1,j},y)==1
w=j;
end
end
if q>1 && w>1
distance = raw{q,w};
else
distance = -1;
end
Deep Raj Bhujel
Deep Raj Bhujel on 19 May 2020
Thank you Jaimin.

Sign in to comment.

Answers (3)

Irfan Hussain
Irfan Hussain on 1 Apr 2020
function distance = get_distance(city_1 , city_2)
[num,txt,raw] = xlsread('Distances.xlsx');
p = 0; q = 0;
name_1 = strcmpi(city_1,raw(:,1));
p = find(name_1 == 1);
name_2 = strcmpi(city_2,raw(1,:));
q = find(name_2 ==1);
if p > 1 && q > 1
distance = raw{p, q};
else
distance = -1;
end

  4 Comments

Show 1 older comment
Irfan Hussain
Irfan Hussain on 1 Apr 2020
yes you are right. basically the output give correct that i will past it in hurry. now i am improving the problem, i face non-existance city issue
Walter Roberson
Walter Roberson on 1 Apr 2020
  • If X contains no nonzero elements or is empty, then find returns an empty array.
Notice this does not say that "if X containst no nonzero elements or is empty then find returns 0"
Also, in your logic, suppose that p or q were 0 so you set distance to -1. But then you go ahead and try to do
distance = raw{p,q};
anyhow, even though you already know that one of the cities was not found.
Sai kalyan Kolapally
Sai kalyan Kolapally on 18 May 2020
function distance= get_distance(a,b)
[~,~,all]=xlsread('Distances.xlsx');
p=0;q=0;
city1= strcmpi(a,all(1,:));
p=find(city1==1);
city2= strcmpi(b,all(:,1));
q= find(city2==1);
if isempty(p)==1 || isempty(q)==1
distance=-1;
else
distance= all{p,q};
end
end

Sign in to comment.


SAMARTH MAHESHKUMAR GEMLAWALA
This is how i done it using for loop, make it simple to understand.
function distance = get_distance(a,b)
[num,txt,raw] = xlsread('Distances.xlsx');
x = size(raw)
v = a
c = b
for i=2:x(1,1)
y = raw{1,i};
if strcmp(y,v)
g=1
for j= 1:x(1,2)
z = raw{j,1};
if strcmp(z,c)
f=1
distance = raw{i,j};
break
else
distance = -1;
end
end
break
else
distance = -1;
end
end

  0 Comments

Sign in to comment.


Mulayam Singh Choudhary
Mulayam Singh Choudhary on 18 Jun 2020 at 23:09
Edited: Mulayam Singh Choudhary on 18 Jun 2020 at 23:10
function distance=get_distance(a,b)
a= convertCharsToStrings(a);
b= convertCharsToStrings(b);
[bar,~,raj]=xlsread('Distances.xlsx');
ii=2;
for k=1:length(raj)-1
raj{1,ii}= convertCharsToStrings(raj{1,ii});
raj{ii,1}= convertCharsToStrings(raj{ii,1});
ii=ii+1;
end
ii=2;
jj=2;
for m=2:length(raj)
if raj{1,ii}==a
break;
end
ii=ii+1;
end
for n=2:length(raj)
if raj{jj,1}==b
break;
end
jj=jj+1;
end
if ii>length(raj)||jj>length(raj)
distance=-1;
else
distance=bar(jj-1,ii-1);
end

  0 Comments

Sign in to comment.


Translated by