I am getting an error in line 4 " myproj('project2.m" how do I fix this?
1 次查看(过去 30 天)
显示 更早的评论
close all;
clear all;
clc;
myproj('project2.m')
miles = xlsread('Project2.m');
A = (miles > 0);
B = A^2;
fprintf('Cities to chose from: 1 to %d. Pick a number in that range.\n', numCities);
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(project2enee, {str2}, 2, 'A1');
xlswrite(project2enee, {str1}, 2, 'A2');
xlswrite(project2enee, {'Stop'}, 2, 'A3');
xlswrite(project2enee, {'Distance'}, 2, 'B3');
routes(1:total_routes, 1) = {' '};
distances(1:total_routes, 1) = 0;
num_route = 0;
end
table = readtable ('project2enee.m');
Len=length(table);
disp (['Cities to chose from: 1 to 4 ',num2str(Len)])
prompt='Departure city:';
depcity=input (prompt);
while (depcity<Len)
disp ('Not a Valid City, Please enter a Valid city')
prompt='Departure city:';
depcity=input(prompt);
while(descity>Len)
disp('Not a Valid City, Please enter a Valid city')
prompt='Destination city:';
descity=input(prpmpt);
end
dep=table(depcity,:);
des=table(descity,:);
routes=0;
fir i-1:Len
if dep(1,1i)~=0
routes=routes+1;
end
end
disp (['Total number of routes:' num2str (routes)])
direct=dep(descity);
disp('STOP DISTANCE')
disp(['Direct ',num2str(direct), ' Miles'])
j=0;
if routes>1
fori=1:Len;
if 1i~= descity && 1i~= depcity
j=j+1;
route(j,1)=dep(i) + des(i);
disp(['City',num2str(1i),' ',num2str(dep(1i)),'+',num2str(des(1i)), "=", num2str(route(j,1)),' Miles'])
end
end
3 个评论
Walter Roberson
2022-11-30
Please do not keep editing the code in the main question: that makes the responses you get meaningless.
You can post updated code in comments.
Star Strider
2022-11-30
采纳的回答
DGM
2022-11-30
编辑:DGM
2022-11-30
What Walter says is correct. I'm not going to guess what you meant, but the character it says isn't valid isn't a valid character.
if routes>1
for i = 1:Len;
if 1i~= descity && 1i`= depcity % ` is not a valid character
... but the rest of this doesn't make sense to me either.
It's unclear why you're trying to compare descity and depcity to make sure they're not a unit-magnitude imaginary number. If that can happen in your code, why?
Even if you have some reason to represent cities as potentially imaginary numbers, you're really not doing yourself or anyone else any favors by using i also as a loop index.
Also, Imaginary numbers are not valid array indexes.
if dep(1,1i)~=0
% ...
end
and
disp(['City',num2str(1i),' ',num2str(dep(1i)),'+',num2str(des(1i)), "=", num2str(route(j,1)),' Miles'])
Why are imaginary numbers even being used anywhere here? Did you just find/replace all instances of i with 1i?
0 个评论
更多回答(2 个)
Image Analyst
2022-11-30
Try
cityTable = readtable('project2enee.m')
numCities = height(cityTable);
fprintf('Cities to chose from: 1 to %d. Pick a number in that range.\n', numCities);
Don't use table as the name of a variable since it's a built-in function. Also don't have the word "function" in your line 3 since that makes the file a script followed by a function but you never actually call the function in the script part of the file.
Take out this line:
table xlsred('project2enee.m')
It's totally wrong. It doesn't have an equals sign, and xlsread is spelled wrong and not needed since we're going to call readtable() instead.
1 个评论
Image Analyst
2022-11-30
Well now you've edited it. Your new error is just what it says. It doesn't know what myproj is. Why do you think is should know what that is? It doesn't.
Walter Roberson
2022-11-30
Change
close all;
clear all;
clc;
myproj('project2.m')
to
myproj('project2.m')
Stored in a file that is not named myproj.m or project2.m
Then take the rest of the code and store it in a file named myproj.m . And change
miles = xlsread('Project2.m');
to become
function myproj(filename)
miles = xlsread(filename);
By the way, it is quite unusual to rename a .xls or .xlsx file to have a .m file extension. Perhaps it is a file that is in csv (comma separated value) format? If so then I cannot promise that xlsread will correctly guess that it is intended to be a text file.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!