Help with code, writematrix not working. Please help fix

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)

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

It gives this error when I fixed that:
Error using writematrix (line 206)
Unable to write to file 'C:\Users\ijupchur\OneDrive - University of New
Orleans\Documents\MATLAB\table1.xlsx'. Ensure the file is a valid spreadsheet file and is not password
protected.
Error in Untitled27 (line 27)
writematrix(DepartureCity,'table1.xlsx','Sheet',2,'Range','A6')
Ensure the file is a valid spreadsheet file and is not password protected.
What can make a spreedsheet invalid? It isn't password protected and this excel file I'm using worked before.
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.
The file is not open.
How do I run MatLab as an administrator? I am using a virtual desktop granted by my college and the version is R2021a.
How do I change the destination?
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\
Hey, thank you. My teacher just emailed us this morning our Virtual DeskTop is having issue even running simple code

Sign in to comment.

I can. I just attempted to make a new file that worked the first time, only to close off and produce the same error as last time.
which -all writematrix
which -all mpower

4 Comments

Where do I write that? Is it a library?
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.
which -all writematrix
E:\Program Files\MATLAB\R2021a\toolbox\matlab\iofun\writematrix.m
>> which -all mpower
E:\Program Files\MATLAB\R2021a\toolbox\matlab\ops\mpower.m
E:\Program Files\MATLAB\R2021a\toolbox\control\ctrlmodels\@InputOutputModel\mpower.m % InputOutputModel method
E:\Program Files\MATLAB\R2021a\toolbox\wavelet\wavelet\@laurpoly\mpower.m % laurpoly method
>>
What does this exactly mean? Is it a program that contains a list of all the functions I can use?

Sign in to comment.

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.

Products

Release

R2021a

Asked:

on 5 May 2022

Commented:

on 8 May 2022

Community Treasure Hunt

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

Start Hunting!