Help with code, writematrix not working. Please help fix
44 ビュー (過去 30 日間)
古いコメントを表示
Hello, for whatever reason, writematrix is not working for me in MatLab. I need to write my results into an excel file and I don't know what else to do. No matter what I do, I cant fix it.
Error:
Error using writematrix (line 206)
Execution of script mpower as a function is not supported:
E:\Program Files\MATLAB\R2021a\toolbox\matlab\ops\mpower.m
Error in Meh (line 27)
writematrix('Departure City:','table1.xlsx','Sheet',2,'Range','A6')
My Code:
close all
clear
clc
%load the table to read cities and their distances
table=[0 2 3 4
2 0 0 1
3 0 0 5
4 1 5 0]
table = xlsread('table1.xlsx');
L=length(table);
disp(['Cities and their distances: '])
table
disp(['Cities to choose from: 1 to ',num2str(L)])
prompt='Departure City:';
DepartureCity=input(prompt);
while (DepartureCity>L)
disp(['Sorry that this is NOT a city we know. Please enter a valid city number'])
prompt='Departure City:';
DepartureCity=input(prompt);
end
writematrix('Departure City:','table1.xlsx','Sheet',2,'Range','A6')
writematrix(DepartureCity,'table1.xlsx','Sheet',2,'Range','B6')
prompt='Destination City:';
DestinationCity=input(prompt);
while (DestinationCity>L)
disp('Sorry that this is NOT a city we know. Please enter a valid city number')
prompt='Destination City:';
DestinationCity=input(prompt);
end
writematrix('Destination City:','table1.xlsx','Sheet',2,'Range','A7')
writematrix(DestinationCity,'table1.xlsx','Sheet',2,'Range','B7')
DepartCityRow=table(DepartureCity,:);
DestiCityRow=table(DestinationCity,:);
routes=0;
if DepartCityRow(DestinationCity) ~=0
routes=routes+1;
end
for i=1:L
if DepartCityRow(i)~=0 && DestiCityRow(i)~=0
routes=routes+1;
end
end
disp(['Total number of routes:',num2str(routes)])
writematrix('Total number of routes:','table1.xlsx','Sheet',2,'Range','A8')
writematrix(routes,'table1.xlsx','Sheet',2,'Range','B8')
direct=DepartCityRow(DestinationCity);
disp('STOP DISTANCE')
disp(['Direct ',num2str(direct),' Miles'])
writematrix('STOP DISTANCE','table1.xlsx','Sheet',2,'Range','A9')
writematrix('Direct ','table1.xlsx','Sheet',2,'Range','A10')
writematrix(direct,'table1.xlsx','Sheet',2,'Range','B10')
writematrix(' Miles','table1.xlsx','Sheet',2,'Range','C10')
j=0;
if routes>1
for i=1:L
if i~=DestinationCity && i~=DepartureCity && DepartCityRow(i)~=0 && DestiCityRow(i)~=0
j=j+1;
route(j,1)=DepartCityRow(i) + DestiCityRow(i);
disp(['City',num2str(i),' ',num2str(DepartCityRow(i)),'+',num2str(DestiCityRow(i)),'=',num2str(route(j,1)),' Miles'])
if j==1
writematrix('City','table1.xlsx','Sheet',2,'Range','A11')
writematrix(DepartCityRow(i),'table1.xlsx','Sheet',2,'Range','B11')
writematrix('+','table1.xlsx','Sheet',2,'Range','C11')
writematrix(DestiCityRow(i),'table1.xlsx','Sheet',2,'Range','D11')
writematrix('=','table1.xlsx','Sheet',2,'Range','E11')
writematrix(route(j,1),'table1.xlsx','Sheet',2,'Range','F11')
writematrix(' Miles','table1.xlsx','Sheet',2,'Range','G11')
else
writematrix('City','table1.xlsx','Sheet',2,'Range','A12')
writematrix(DepartCityRow(i),'table1.xlsx','Sheet',2,'Range','B12')
writematrix('+','table1.xlsx','Sheet',2,'Range','C12')
writematrix(DestiCityRow(i),'table1.xlsx','Sheet',2,'Range','D12')
writematrix('=','table1.xlsx','Sheet',2,'Range','E12')
writematrix(route(j,1),'table1.xlsx','Sheet',2,'Range','F12')
writematrix(' Miles','table1.xlsx','Sheet',2,'Range','G12')
end
end
end
if j==1
c={' ', ' ', ' ', ' ', ' ', ' ',' '};
writecell(c,'table1.xlsx','Sheet',2,'Range','A12:G12')
end
disp('Routes and distances written to the sheet 2 of table1.xlsx. Goodbye!')
end
0 件のコメント
回答 (4 件)
Cris LaPierre
2022 年 5 月 5 日
Your first input to writematrix needs to be a variable name, not a char. Change this:
writematrix('Departure City:','table1.xlsx','Sheet',2,'Range','A6')
to this
writematrix(DepartureCity,'table1.xlsx','Sheet',2,'Range','A6')
7 件のコメント
Cris LaPierre
2022 年 5 月 5 日
編集済み: Cris LaPierre
2022 年 5 月 5 日
You do not specify a path, so MATLAB is trying to save the file in the current folder. Use the current folder browser to change this location.
Perhaps your virtual desktop cannot write to OneDrive. Run a test. In your virtual desktop (but not in MATLAB), can you create a file in the specified location?
C:\Users\ijupchur\OneDrive - University of New Orleans\Documents\MATLAB\
Jeremy Hughes
2022 年 5 月 6 日
The error reffers to mpower.m being a script and not a function. mpower is a builtin function, and mpower.m contains only help text. It seems that MATLAB isn't detecting the builtin function, which is likely an installation issue. you can try reinstalling from scratch, that should resolve the issue.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Spreadsheets についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!