File Exchange

image thumbnail

Matlab to Google Spreadsheets

version 1.3 (22 KB) by

Matlab functions to send data to Google Spreadsheets.

12 Ratings



View License

WARNING: This code does not work anymore with the current Google Sheets API. Please use the Matlab2Sheets project below.
This set of Matlab functions will allow creating Google
spreadsheets, adding worksheets to them, modifying the
worksheets, and placing data in them.

Comments and Ratings (22)

These scripts worked great for a long time, but now I keep getting error "Last response was: 404/Not Found" even if I try just Matlab2GoogTest

valarmathi I

I am getting "Last response (POST) was: 403/Forbidden", though I have entered my cliend id and secret, and also given the permissions for OAuth2. Are there other permissions Im missing?

I've been playing around with it for a while, and can't get passed "Error using urlreadwrite
Too many input arguments."
Can anyone point me in the right direction?


Looks cool, but the Matlab2GoogTest.m script does not work for me. After fixing some errors, I got it to upload files to google, but the spread sheets always arrive empty except for the A1 cell, which contains a string like "[B@74727a7d" or similar. What could be the problem? Thanks!


Scott (view profile)

Here is the code to update an entire row with one batch update:
function editWorksheetRow(spreadSheetKey,workSheetKey,row,columnNumbers,cellValues,aToken)

import java.lang.*;


nCols = 15;
while (~success && safeguard<MAXITER)
    nEntries = length(columnNumbers);

    % Get edit details
    urlCell=['' spreadSheetKey '/' workSheetKey '/private/full?return-empty=true&min-row=' num2str(row) '&max-row=' num2str(row) '&min-col=1&max-col=' num2str(nCols)];
    con = urlreadwrite(mfilename,urlCell);
    con.setRequestMethod( 'GET' );
    con.setDoInput( true );
    con.setRequestProperty('Authorization',['Bearer ' aToken]);
    if (con.getResponseCode()~=200)

    con.disconnect(); clear con;
    con = urlreadwrite(mfilename,getURLStringList);
    con.setDoOutput( true );
    con.setRequestProperty('Authorization',['Bearer ' aToken]);
    event = '';
    event=['<feed xmlns='''' ' ...
        'xmlns:batch='''' ' ...
        'xmlns:gs=''''>' ...
    for i=1:nEntries
        slashIdx = strfind(editKey,'/');
        event = [event '<entry>' ...
            '<batch:operation type=''update''/>'...
            '<link rel=''edit'' type=''application/atom+xml'' '...
            strcat(' href=''',spreadSheetKey,'/',workSheetKey,'/private/full/R',num2str(row),'C',num2str(columnNumbers(i)),'/',editKey,'''/>')...
            strcat('<gs:cell row=''',num2str(row),''' col=''',num2str(columnNumbers(i)),''' inputValue=''',cellValues{i},'''/>')...
    event = [event '</feed>'];
    ps = PrintStream(con.getOutputStream());
    ps.close(); clear ps;
    if (con.getResponseCode()~=200)
if success
    con.disconnect(); clear con;
    display(['Last response was: ' num2str(con.getResponseCode) '/' con.getResponseMessage().toCharArray()']);
    clear con;


Scott (view profile)

Never mind on my earlier post - I just needed to enable the drive API and the permission error disappeared. Everything works great now. Thank you!


Scott (view profile)

I am getting "Last response (POST) was: 403/Forbidden", though I have entered my cliend id and secret, and also given the permissions for OAuth2. Are there other permissions Im missing?

Same here: it used to work perfectly, and it is so well done! Unfortunately it recently stopped working and I keep getting "Could not obtain authorization tokens from Google."
Something must have changed in Google's API. If anyone finds a fix, please post it.

Has worked brilliantly until recently, when can not obtain tokens from Google any more. Is there a fix of any nature?

Kent Conover

Great idea, but "MatlabGoogTest" returns "Could not obtain authorization tokens from Google." when I try to log in. I am eager to see a fix!


Sven (view profile)

Wonderful work. The two things stopping 5 stars at the moment is documentation (none of the functions that the user must call have any help contents) and the 1-cell-at-a-time functionality only.

It would be very advantageous to supply similar functionality to xlsread/xlswrite, which both accept a cell range to read/write, and if no range is given, xlsread simply returns the full contents of that worksheet.

Malte Ahm

Really great functions! what is missing for me is a "editWorksheetCellBatch" function to be able to edit larger worksheets quicker.

I can see, that the Google API should support it:

I have tried making the function myself, but my experience with java and html protocols is very limited. I keep getting an error 400 (Bad Request) when i try to send the data to the server (ps.print(event)).


Thomas (view profile)

How long is the authentication valid for? I'm running this in a loop and I start getting 403 errors after about 500 seconds, which doesn't happen if I run each iteration by itself


Thomas (view profile)


Thomas (view profile)

Works great!

Eric Trautmann

Eric Trautmann (view profile)


The code below should work (replace connectNAuthorize function in the package with the one below)
urlread should be provided by Matlab.

The 'writely' token is for Google Spreadsheets, the 'wise' token is for Google Docs.

Once you have the tokens you can use the other functions in the package.

Hope this helps,

[authorized, aTokenS]=connectNAuthorize(userName, password,'writely');
if (~authorized)
[authorized, aToken]=connectNAuthorize(userName, password,'wise');
if (~authorized)

function [authorized, aToken]=connectNAuthorize(userName, password,service)


if authorized==1
    aToken=['auth' str(strfind(str,'Auth')+4:end-1)];

Matt Behr

hello .... is there anyway to avoid the credentials dialog. I want to run a script nightly to update data in google docs.


ted p teng

ted p teng (view profile)

Excellent toolbox! You can also easily modify the 'editWorksheetCell' to edit for an array of cells instead of individual cells for reducing the number of http requests [ ].

Jake Hughey


Thanks for the suggestion. I've updated the submission. You can now read both the cell value and its formula.

Hope this helps.

Jake Hughey

this set of functions looks great, but what is missing for me is a "getWorksheetCell" function, because I also want to pull data from google spreadsheets into matlab. I don't know java well enough to write one myself.



mention about new Google API


added two new functions


Updated to Google OAuth2


Google login process has dialog box. New function reads values and formulas from the Google spreadsheet.

MATLAB Release
MATLAB 8.5 (R2015a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video