I'm trying to import a matrix from an excel file and then writing it back to the same excel file. I was able to read the matrix M just fine, but I'm not able to write to the excel file. What am I missing? Thanks.

1 view (last 30 days)
%Path1 is path only
Path1 = input('Enter path to excel file \n','s');
Path1 = 'C:\Users\Public\';
%File is filename only and Get concatenated
%Path1 and File into one string for the
%reads and writes
%File = input ('Enter file complete file name \n','s');
File = 'excel_Arrays.xlsx';
get = strcat (Path1, File);
M = xlsread(get,'Sheet1','B3:D5');
M;
%%filename = '3Eq-4Eq.xlsx';
xlswrite(get,'Sheet1','B10:D12');
  2 Comments
Stephen23
Stephen23 on 14 Feb 2018
Edited: Stephen23 on 14 Feb 2018
Do NOT use get as a variable name! Doing so will shadow the inbuilt and very important get function.
You forgot to pass M to xlswrite. You should also use fullfile rather than string concatenation:
fnm = fullfile(Path1,File);
M = xlsread(fnm,'Sheet1','B3:D5');
xlswrite(fnm,M,'Sheet1','B10:D12');

Sign in to comment.

Answers (1)

Rik
Rik on 14 Feb 2018
Edited: Rik on 14 Feb 2018
Replace this
xlswrite(get,'Sheet1','B10:D12');
with this
xlswrite(get,M,'Sheet1','B10:D12');
And you shouldn't overwrite get, so you should change the variable name to something else.
Reading the documentation for xlswrite shows you what options you have for your syntax. The syntax you were using would be xlswrite(filename,A,xlRange), so 'Sheet1' would be the matrix you are writing, which is likely not what you mean.

Community Treasure Hunt

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

Start Hunting!