How to write a single element into an excel sheet
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Share a link to this question
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
1 vote
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
Isi Isi
on 16 Apr 2017
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'
dpb
on 16 Apr 2017
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');
dpb
on 16 Apr 2017
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.
Isi Isi
on 17 Apr 2017
Thanks for your effort mate
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.
More Answers (0)
Categories
Find more on Spreadsheets in Help Center and File Exchange
See Also
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)