Using Excel files in function

4 visualizaciones (últimos 30 días)
Perturabo
Perturabo el 7 de Feb. de 2019
Comentada: Mahesh Sharma el 27 de Jul. de 2019
I have a spreadsheet of list of cities and distances between them such as
New York Washington DC Los Angeles
New York 0 500 . 1000
Washington DC 500 0 . 1500
Los Angeles 1000 1500 0
how do i create a function which takes 2 cities as input and gives the distance as output, given that invalid city names give 0 as distance?
I tried a few things but honestly I don't know how to proceed with this problem.
  2 comentarios
Bob Thompson
Bob Thompson el 7 de Feb. de 2019
Could you expand a little on what things you tried, and why they didn't work? That will help us give you a more clear answer, rather than something super general.
Perturabo
Perturabo el 7 de Feb. de 2019
Editada: Perturabo el 7 de Feb. de 2019
I tried reading the excel raw data into a matrix and returning the element at m,n where m and n are the cities. But it doesn't work and it returns the whole cell array itself. But honestly I didn't understand file I/O in MATLAB enough to handle functioning with excel.

Iniciar sesión para comentar.

Respuesta aceptada

Bob Thompson
Bob Thompson el 7 de Feb. de 2019
Editada: Bob Thompson el 7 de Feb. de 2019
This is a quick first cut, so you will likely need to do some fine tuning of your own. I am assuming that your city distance data is one large square array, with city names in first row and first column.
function [distance] = city_distance(city1,city2);
% Read in the city data, keeping all data in cells
[~,~,data] = xlsread('mycityfile.xlsx');
% Check for first city
c1 = cellfun(@(x) strcmp(x,city1),data(:,1)); % Edit
if c1 == 0; % Edit
error('No match was found for the first city.')
end
% Check for the second city
c2 = cellfun(@(x) strcmp(x,city2),data(1,:)); % Edit
if c2 == 0; % Edit
error('No match was found for the second city.')
end
% Distance determination
distance = data{c1,c2};
end
  11 comentarios
Priyamvada Shankar
Priyamvada Shankar el 23 de Mzo. de 2019
Have you got that where you went wrong,if yes then help me also... I'm too stuck in this question.
Mahesh Sharma
Mahesh Sharma el 27 de Jul. de 2019
hey
try this one , this works.
function distance = get_distance(A,B)
[~,~,raw] = xlsread('Distances.xlsx');
distance = -1;
for i = 2 : size(raw,1)
if strcmp(A,raw{1,i})
for j = 2 : size(raw,2)
if strcmp(B,raw{j,1})
distance = raw{j,i};
return
end
end
else
distance = -1;
end
end
end

Iniciar sesión para comentar.

Más respuestas (1)

Ana Guerreiro
Ana Guerreiro el 27 de Abr. de 2019
Hi Bob Nbob
Your code does not work for "non-existent city" that needs to be equal to -1 (Distance= -1).
Can you help me, showing how to do it, please?
Best regards

Categorías

Más información sobre Data Import from MATLAB en Help Center y File Exchange.

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by