MATLAB Answers

Assignment question based on excel file I/O

69 views (last 30 days)
Ajai Kannan
Ajai Kannan on 17 Feb 2019
Commented: Rik on 16 Mar 2021
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('Alexandria, VA' , 'Allentown, PA') should return 195
. If one or both of the specified cities are not in the file, the function returns -1.
download.png
The code I tried is the following:
I get error in the " if text{i,1} == a" and "if text{1,j} == b" lines stating matrix dimensions must agree. Will be really helpful if someone can help me with it.
function distance = get_distance(a,b)
[~,text,raw] = xlsread('Distances.xlsx');
for i = 2 : size(text,1)
if text{i,1} == a
for j = 1 : size(text,2)
if text{2,j} == b
distance = raw{i,j};
else
distance = -1;
end
end
else
distance = -1;
end
end
  4 Comments
Walter Roberson
Walter Roberson on 2 Apr 2019
This code is wrong for the case where the match is the last row or column. You break out of the loop but the value of the index happens to exactly equal to the size so you think there was no match.

Sign in to comment.

Answers (7)

Geoff Hayes
Geoff Hayes on 17 Feb 2019
Ajai - if you try to compare strings using the == operator, then you will see this error if the two strings being compared are of different lengths. Instead, you should be using strcmp or strcmpi to check for equality between two strings. Your code would then change from
text{i,1} == a
to
strcmp(text{i,1}, a)
Also, rather than using a loop, you could use find to see if the first column of text contains the cities that you are looking for (see the examples in the provided link for details).
  4 Comments
Walter Roberson
Walter Roberson on 23 Mar 2019
This is obviously an assignment. We seldom post the code to answer assignments. However, if you post your attempt and indicate the error messages you are encountering, then we will assist you in debugging what you have.

Sign in to comment.


ABINAND PANDIYAN
ABINAND PANDIYAN on 24 Apr 2020
Edited: ABINAND PANDIYAN on 25 Apr 2020
% This problem is right I think.
function distance = get_distance(A,B)
[~,~,dist] = xlsread('Distances.xlsx');
for i=2:(size(dist,1)+1)
if i>size(dist,1)
distance=-1;
return
end
if strcmp(dist{i,1}, A)
break
end
end
for j=2:(size(dist,2)+1)
if j> size(dist,2)
distance=-1;
return
end
if strcmp(dist{1,j}, B)
distance=dist{i,j};
break
end
end
end
  2 Comments
ABINAND PANDIYAN
ABINAND PANDIYAN on 25 Apr 2020
@Walter Roberson
Yeah I checked that, But now after hours of brainstroming I think I got the answer right with the edited content. You can check it now.

Sign in to comment.


Olel Arem
Olel Arem on 1 May 2020
Edited: Olel Arem on 1 May 2020
function distance=get_distance(city_1,city_2)
n=0;m=0;
[value,name]=xlsread('Distances.xlsx');
for ii=1:size(name,2)
if (strcmp(city_1,name(ii,1)))
n=ii;
end
end
for jj=1:size(name,1)
if (strcmp(city_2,name(1,jj)))
m=jj;
end
end
if(n==0 && m==0 && n==1&&m==1)
distance =-1;
elseif (n>0&&m>0)
distance=value(n-1,m-1);
else
distance=-1;
end

Fazlul  Haque
Fazlul Haque on 15 May 2020
done without using loops
  8 Comments
Rik
Rik on 13 Dec 2020
Did you read the documentation for the strcmpi function?

Sign in to comment.


Muhammad Qaisar Ali
Muhammad Qaisar Ali on 27 Jun 2020
function distance = get_distance(city_1,city_2)
persistent distt city_names;
if isempty(distt) && isempty(city_names) % Ceck wheater first time load or not.
[distt,city_names,~]=xlsread('Distances.xlsx'); % Reading ditances and names of cities
end
rowIndex = find(ismember(city_names(:,1),city_1)); % Searching city_1 in col_1 of excell seet.finding row index.
colIndex = find(ismember(city_names(1,:),city_2)); % Searching city_2 in row_1 of excell seet.finding col index.
if isempty(rowIndex) || isempty(colIndex) % If cities nit found return -1.
distance=-1;
else % Find and retun distance.
distance=distt(rowIndex-1,colIndex-1);
end
end
  2 Comments
Muhammad Qaisar Ali
Muhammad Qaisar Ali on 28 Jun 2020
Thanks for your vaulable feedbak..indeed it helps me alot..

Sign in to comment.


Priyamvada Shankar
Priyamvada Shankar on 22 Mar 2019
Can you please write the code...it will be a great help... please I have tried too many times but still getting some problems
  7 Comments
sadek kouz
sadek kouz on 18 Mar 2020
use break to break out of the looo

Sign in to comment.


XINYI CAI
XINYI CAI on 16 Mar 2021
why my answer does not work for non-exsitent city?
  3 Comments
Rik
Rik on 16 Mar 2021
No, you should use ~any(row) instead.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by