Help concatenating a table row to a specific excel spreadsheet?

조회 수: 1 (최근 30일)
Matthieu Sherwood
Matthieu Sherwood 2019년 8월 12일
댓글: dpb 2019년 8월 13일
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:
  1. the participant's file number (as a string),
  2. the average time between events #1 and #2,
  3. the average time between events #2 and #3,
  4. 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;
  1. 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?
  2. I want to be sure that I am appending to the spreadsheet, not overwriting anything.
  3. (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!

채택된 답변

dpb
dpb 2019년 8월 12일
편집: dpb 2019년 8월 13일
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
Matthieu Sherwood
Matthieu Sherwood 2019년 8월 13일
Hi, thank you for your response. I ended up changing it to a text file rather than an excel sheet, much easier.
dpb
dpb 2019년 8월 13일
Thought it might be... :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by