Help concatenating a table row to a specific excel spreadsheet?
2 views (last 30 days)
Show older comments
I have written a script that calculates the average time between certain experimental events across all trials for a given participant. I would now like to create an excel spreadsheet that compiles this information for all the different participant files. It would contain 4 columns:
- the participant's file number (as a string),
- the average time between events #1 and #2,
- the average time between events #2 and #3,
- and the difference in the number of occurrences of events #3 and #4.
Each row will therefore be representative of an individual file, and you can append a row every time you run a new file. Here is my attempt below;
%... ^script that creates all the variables and whatnot
outputTable=table(VarNameStr,Screen_Grasp_Average,Grasp_Release_Average,difference); %these are the four variables I am trying to output
writetable(outputTable,D:\LocationName\DataLocation\Spreadsheet.xlsx);
What I am trying to do here is append these four different variables as a row to the excel spreadsheet 'Spreadsheet' located at D:\LocationName\DataLocation using writetable(). This has given me the error that the '\' is an invalid operator . I think I have two main issues facing me and one smaller issue;
- How to specify the location of the spreadsheet that I want to output to? It seems writetable doesn't have this ability. Should the script I am running and the spreadsheet be in the same folder? Or can they be separate?
- I want to be sure that I am appending to the spreadsheet, not overwriting anything.
- (smaller) How can I prevent repeat lines? I want to prevent the same file from being analyzed and outputted more than once since other lab members will be using this script as well. Worst comes to worst I could just use unique() when reading the file in matlab but I was curious if there was a better way.
Thanks for any answers/help!
0 Comments
Accepted Answer
dpb
on 12 Aug 2019
Edited: dpb
on 13 Aug 2019
outputTable=table(VarNameStr,Screen_Grasp_Average,Grasp_Release_Average,difference);
writetable(outputTable,D:\LocationName\DataLocation\Spreadsheet.xlsx);
D:\LocationName\DataLocation\Spreadsheet.xlsx is invalid syntax -- it isn't quoted to be a string and it isn't valid syntax to catenate variables (if the pieces of it other than punctuation) were actually variables. writetable expects the second argument to be an acceptable filename as either a char() vector or a string...you gave it something unrecognizable.
drive='D:';
folder='LocationName\DataLocation';
file='Spreadsheet.xlsx';
writetable(outputTable,fullfile(drive,folder,file));
will solve your filename problem when you properly define the various pieces for your application.
That will NOT solve the problem of appending to the Excel spreadsheet, however; you'll have to use the optional 'Range' argument to set the starting corner of the sheet to write to. BUT you'll have to figure out what the next available row is because there is no 'append' flag for Excel files as there is for sequential text files. This means keeping track of how many records have been written to date and compute the proper range expression based on that.
Depending on how this is going to be used, it may be better to either create the full table first before writing it or reading in the existing data and appending the writing (a lot of overhead) or to just write a sequential text file and then when done read that file and convert it to spreadsheet if think simply must have a spreadsheet.
2 Comments
More Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!