How to write a single element into an excel sheet

Hi, How do I write a single element into an existing excel sheet please? For example if I have x=5, how may I write x into cell B2 of an existing excel sheet please?

 Accepted Answer

dpb
dpb on 16 Apr 2017
Edited: dpb on 18 Apr 2017
doc xlswrite
Pretty-much, trivially...
xlswrite('yourfile,xls',x,sheet,'B2')
where sheet is either a numeric number for the desired ordinal sheet or a character string naming the sheet.

6 Comments

Thanks for your reply. That is exactly what I did but when I try accessing the file it says 'the import tool doesn't have access to read the file'
Well, that's a file security issue of some sort...oh! Is the Excel session still open or does another session/user have the spreadsheet open as well? Have to close all processes using it.
Oh, and occasionally a "zombie" process can be in memory; use the task manager to see if there is an unsuccessfully-terminated Excel process by any chance. If so, kill it, too...
I have attached a screen shot of the error. I don't think the issue is related to any of what you've mentioned. Here is the code I'm trying to run. The first 2 lines have been commented out but they run properly when I run them instead of the last 2:
%classinfo=[1 2 3; 4 5 6; 7 8 9];
%xlswrite('classlist',classinfo,'section2','A1');
bag=20;
xlswrite('classlist',bag,'section2','A1');
What system are you running under? Looks like Excel isn't installed or the COM engine isn't runing--under the Notes section of the doc for xlswrite one finds the following--
>> help xlswrite
....
* If your system does not have Excel for Windows, or if the COM server
(part of the typical installation of Excel) is unavailable, xlswrite:
* Writes ARRAY to a text file in comma-separated value (CSV) format.
* Ignores SHEET and RANGE arguments.
...
NB: The screen shot you show is referring to a file 'classlist.csv' so xlswrite didn't write a .xls file in the first case but a csv file. The "doesn't have access" message can only mean the file is locked by another process.
You can't update a field in a .csv file anyway, it's a serial format and doesn't have the ability to write somewhere in the middle without writing over everything from that point on.
Thanks for your effort mate
dpb
dpb on 17 Apr 2017
Edited: dpb on 17 Apr 2017
If you are on a Windows machine, reinstalling/refreshing Excel is the recommended fix. If not, you'll have to do something else...
ADDENDUM
Why is the import tool getting involved, anyway? I presume maybe you tried to open it to see if the command did what was expected? If so, I'm guessing when COM failed, it had created a file but didn't successfully close it and left it locked since was trying to update it at the time. Just hypothesis not being able to see the whole session, but would 'splain the symptoms.

Sign in to comment.

More Answers (0)

Asked:

on 16 Apr 2017

Edited:

dpb
on 18 Apr 2017

Community Treasure Hunt

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

Start Hunting!