Help with code, writematrix not working. Please help fix
Show older comments
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
Answers (4)
Cris LaPierre
on 5 May 2022
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 Comments
Ian Upchurch
on 5 May 2022
Cris LaPierre
on 5 May 2022
Ensure the file is a valid spreadsheet file and is not password protected.
Ian Upchurch
on 5 May 2022
Cris LaPierre
on 5 May 2022
If the file is open in Excel, it can't be written to.
You may be trying to write to a location that MATLAB does not have write access to. You could try running MATLAB as an administrator, or chaning the destination.
Ian Upchurch
on 5 May 2022
Cris LaPierre
on 5 May 2022
Edited: Cris LaPierre
on 5 May 2022
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\
Ian Upchurch
on 8 May 2022
Ian Upchurch
on 5 May 2022
0 votes
Sean de Wolski
on 5 May 2022
which -all writematrix
which -all mpower
4 Comments
Ian Upchurch
on 5 May 2022
Walter Roberson
on 5 May 2022
put those at the MATLAB command line and tell us the output. They will not repair the problem at all, but they will give us information that might help us figure out what the problem is.
Ian Upchurch
on 5 May 2022
Ian Upchurch
on 5 May 2022
Jeremy Hughes
on 6 May 2022
0 votes
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.
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!