Reading/Writing Excel file
Mostrar comentarios más antiguos
I'm trying to create a MATLAB function that reads data from a excel file, prompts the user to enter two cities, outputs the shorest distance between the two systems, and writes the data to the excel file. I've gotten it to prompt the user for the inputs, but it wont perfpr the calculations or write the data to the xlsx file. Here is the code:
close all
clear all
clc
table = readtable('Connectivity.xlsx','PreserveVariableNames',true);
miles = xlsread('Connectivity.xlsx');
A = (miles > 0);
B = A^2;
fprintf('Cities to choose from: ');
disp(1:length(A));
x = input('Departure city: ');
y = input('Destination city: ');
fprintf('\n');
if x == y
disp('You entered the same Departure city and Destination city!\n')
else
total_routes = A(x,y) + B(x,y);
str1 = sprintf('Total numbers of routes: %d', total_routes);
disp(str1);
disp('Stop Distance');
str2 = sprintf('Routes between City %d and City %d', x, y);
xlswrite('Connectivity.xlsx', {str2}, 2, 'A1');
xlswrite('Connectivity.xlsx', {str1}, 2, 'A2');
xlswrite('Connectivity.xlsx', {'Stop'}, 2, 'A3');
xlswrite('Connectivity.xlsx', {'Distance'}, 2, 'B3');
routes(1:total_routes, 1) = {' '}; % Route name (Direct or City name)
distances(1:total_routes, 1) = 0; % Mile of each route
num_route = 0; % Keep track of row number in matrix “routes” and “distances”
end
If anyone can help me figure out what I'm missing/doing wrong that would be great. Thanks!
4 comentarios
Walter Roberson
el 9 de Dic. de 2020
A = (miles > 0);
B = A^2;
A will be logical. Are you sure that you want to use matrix multiplication of two logical arrays?
There are reasons to do that, but I would expect them to be documented.
Andrew Lester
el 9 de Dic. de 2020
Harry Laing
el 9 de Dic. de 2020
total_routes = A(x,y) + B(x,y);
If I'm not mistaken, x and y are names of cities input by the user, so I'm not sure what this line is trying to acheive? Or is the input expected to be a number? As walter said A is a logical array, so surely referencing using names x and y makes no sense?
Walter Roberson
el 9 de Dic. de 2020
if x and y are scalar numeric then the code makes sense for talking about direct travel or travel with one stop.
Respuestas (0)
Categorías
Más información sobre Data Import from MATLAB en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!