Writetable for different data to same file

Hello,
I have a script that reads in a csv file and outputs a number of variables. I then store this variable in a table with two headings, the name of the file and in this case Time to Completion.
varNames = {'File','Time to Completion)'};
T = table(Name,TTC,'VariableNames',varNames);
writetable(T,'Time_Completion.xls');
My problem is that I want to use this same file (Time_Completion.xls) for different runs of the script with new data so that the columns populate with every new run rather than overwriting. I don't know to read the file every time it opens and if there's data in one row go down and fill the next. Appreciate any help. Thanks.

Answers (1)

See the Append Data to Bottom of Table example on the writetable documentation page.

6 Comments

Hi Cris, thanks for the response. I had tried append before and all I get is an error message about wrong number of arguments. I'm not very clear on how it works if I'm not manually adding a row to the table
Share the code you use to create the table as well as the code you use to append the data to your existing xls file.
[filepath,name,ext] = fileparts(fileName);
Name = convertCharsToStrings(name);
varNames = {'File','Time to Completion)'};
T = table(Name,TTC,'VariableNames',varNames);
writetable(T,'Time_Completion.xls');
I don't have any specific append code available. I was only modifying what is here.
When do you get the error message? Please share the complete error message (all the red text).
From the example I linked to above, code to append to an existing files should look something like this.
writetable(newInsect,'InsectCollection.txt','WriteMode','Append',...
'WriteVariableNames',false,'WriteRowNames',true)
The following describes how Append behaves with spreadsheets:
  • 'append' — The writing function appends the input data to the bottom of the occupied range of the specified sheet.
  • If you do not specify a sheet, then the writing function appends the input data to the bottom of the occupied range of the first sheet.
So if I change my line of code to include that line the error I get is below. Matlab version is 2021a
writetable(T,'Time_Completion.xls','WriteMode','Append');
Error using table (line 264)
Invalid parameter name: WriteMode.
Do you get an error when you run the following?
InsectSpecies = {'Monarch Butterfly';'Seven-spot Ladybird';'Orchid Mantis';...
'American Bumblebee';'Blue Dasher Dragonfly'};
InsectOrder = {'Lepidoptera';'Coleoptera';'Mantodea';'Hymenoptera';'Odonata'};
InsectFamily = {'Nymphalidae';'Coccinellidae';'Hymenopodidae';...
'Apidae';'Libellulidae'};
PredatoryInsect = logical([0;1;1;0;1]);
T = table(InsectSpecies,InsectOrder,InsectFamily,PredatoryInsect)
writetable(T,'InsectCollection.xls','WriteRowNames',true)
%% Now append a new row to the table
newInsect = table({'Red-banded leafhopper'},{'Hemiptera'},{'Cicadellidae'},logical([0]));
writetable(newInsect,'InsectCollection.xls','WriteMode','Append',...
'WriteVariableNames',false,'WriteRowNames',true)

Sign in to comment.

Categories

Asked:

CH
on 5 Aug 2021

Edited:

CH
on 6 Aug 2021

Community Treasure Hunt

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

Start Hunting!