I am getting an error in line 4 " myproj('project2.m" how do I fix this?

6 ビュー (過去 30 日間)
lawson
lawson 2022 年 11 月 30 日
コメント済み: Star Strider 2022 年 11 月 30 日
close all;
clear all;
clc;
myproj('project2.m')
Unrecognized function or variable 'myproj'.
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 件のコメント
Star Strider
Star Strider 2022 年 11 月 30 日
If this is intended to be a for loop:
fir i-1:Len
it needs to be spelled correctrly and it needs its own end statement.
Also 1i (i) is the imaginary operator. Perhaps isreal or imag
isreal(depcity)
or:
imag(depcity) ~= 0
would be more appropriate, although why a city could be imaginary eludes me.
.

サインインしてコメントする。

採用された回答

DGM
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?

その他の回答 (2 件)

Image Analyst
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
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
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.

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by