Write a function 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.
2 views (last 30 days)
Show older comments
function distance = get_distance(x,y)
[~,~,raw] = xlsread('Distances.xlsx');
T = readtable('Distances.xlsx');
texes = table2struct(T);
if (isfield(texes,x)==1 && isfield(texes,y)==1)
distance = -1;
else
index1 = find(strcmp(raw,x));
cell1 = index1(1,1);
index2 = find(strcmp(raw,y));
cell2 = index2(1,1);
distance = raw{cell1, cell2};
end
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/318402/image.png)
The given output is write,but I can't understand the warning and can't find out the problem. I find no-one with this problem, Please help someone kindly.
0 Comments
Answers (2)
Vinayak Mohite
on 19 Jun 2020
Edited: Vinayak Mohite
on 19 Jun 2020
Hi Konkon,
The warning is there because the column headers of the file which is being read contain spaces, leading numbers, or other characters. But variables in MATLAB cannot have them.
You can get your original column header by setting PreserveVariableNames property to true.
T = readtable('Distances.xlsx', "PreserveVariableNames",true)
You are getting a correct answer because this is just a warning and not an error. This warning just to let the user know that his/her data has been modified.
Konkon Das
on 19 Jun 2020
2 Comments
Vinayak Mohite
on 24 Jun 2020
One can use this property
T = readtable('Distances.xlsx','PreserveVariableNames',true);
only in R2019b or later.
But not using this will only generate a warning. This warning just to let the user know that his/her data has been modified. Hence you are getting the correct output
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!