Help with code, writematrix not working. Please help fix
조회 수: 49 (최근 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!