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.
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
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/445405-assignment-question-based-on-excel-file-i-o#comment_687411
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/445405-assignment-question-based-on-excel-file-i-o#comment_687411
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/445405-assignment-question-based-on-excel-file-i-o#comment_688931
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/445405-assignment-question-based-on-excel-file-i-o#comment_688931
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/445405-assignment-question-based-on-excel-file-i-o#comment_688934
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/445405-assignment-question-based-on-excel-file-i-o#comment_688934
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/445405-assignment-question-based-on-excel-file-i-o#comment_689079
Direct link to this comment
https://uk.mathworks.com/matlabcentral/answers/445405-assignment-question-based-on-excel-file-i-o#comment_689079
Sign in to comment.