Reading/Writing Excel file
이전 댓글 표시
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
Walter Roberson
2020년 12월 9일
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
2020년 12월 9일
Harry Laing
2020년 12월 9일
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
2020년 12월 9일
if x and y are scalar numeric then the code makes sense for talking about direct travel or travel with one stop.
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!