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.

조회 수: 1 (최근 30일)
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
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.

답변 (2개)

Vinayak Mohite
Vinayak Mohite 2020년 6월 19일
편집: Vinayak Mohite 2020년 6월 19일
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
Konkon Das 2020년 6월 19일
Hi Vinayak,
Thanks a lot for your response.
I've tried a lot.But still now in stuck.First,I go to search for PreserveVariableNames property in doc file,but couldn't find probably for R2015a version. I search in net and then apply. But there is a warning like the below.
[Warning: Table variable names that were not valid MATLAB identifiers have been modified. Since table variable names must be unique, any table variable names that happened to match
the new identifiers also have been modified.]
After that I modified the code,but still showing the same warning.I couldn't find any other way. Can you help please.
function distance = get_distance(x,y)
[~,~,raw] = xlsread('Distances.xlsx');
PreserveVariableNames = xlsread('Distances.xlsx',1,'B1:LY1');
[~,text] = xlsread('Distances.xlsx',1,'B1:LY1');
for k1 = 1:length(text)
fprintf(1, '''%s'' ; ',text{k1});
end
T = readtable('Distances.xlsx','PreserveVariableNames',true);
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
  댓글 수: 2
Vinayak Mohite
Vinayak Mohite 2020년 6월 24일
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
Konkon Das
Konkon Das 2020년 6월 24일
But because of this warning, I can't submit my code. Still showing an error like the server timed out...

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by