Unable to download from dropbox to ThingSpeak using Matlab code.

3 views (last 30 days)
I used the 'Download files from your DropBox API folder using MATLAB' code in thingspeak's matlab analysis, to get a training model.mat file from dropbox, but it just keeps showing not enough input arguments. how do I solve this please? I checked and it went wrong during "narginchk(2,3)" but I just am not sure how and what to do
function downloadFromDropbox(dropboxAccessToken,fileNames,varargin)
dropboxAccessToken='xxx'; %xxx to hide the actual token
fileNames='TrainingModel.mat';
% Check to input arguments
narginchk(2,3);
% If no file specified, pop up the dialog for the user to select one
if isequal(nargin,2)
%Set default downloadPath to workspace location
downloadPath = pwd;
elseif isequal(nargin,3)
% If downloadPath is provided, check if the given path exists
if ~exist(varargin{1},'dir')
throw(MException('downloadFromDropbox:pathNotFound','Download path does not exist.'));
else
downloadPath = varargin{1};
end
end

Answers (1)

Christopher Stapels
Christopher Stapels on 9 Jun 2022
Edited: Christopher Stapels on 9 Jun 2022
I think you are calling the function with no input arguments.
You have at least two options. You can call the functionin your script, or remove all the mechanics at the top of the function and just make the function part of the script.
For the first option, move this whole function to the end of the script
function downloadFromDropbox(dropboxAccessToken,fileNames,varargin)
and then call the function earlier in the script, making sure to provide all the required inputs
downloadFromDropbox(token, names, localPath);
function downloadFromDropbox(dropboxAccessToken,fileNames,varargin)
...
end
This doent make the output available tfor you to use in the session, however, it just saves it to a local space. In ThingSpeak, you dont have access to a local space.
Here is the code I use. Note the trick at the end to convert it to the right file type - you may not need that depending on what your are reading in. You will have to change the name 'myModel.mat' to your filename. IT would probably be best to write it as a variable at teh top od the script.
theFile='filenametoDownload';
dropboxAccessToken='xyz';
FName = theFile;
% Generate the custom header
headerFields = {'Authorization', ['Bearer ', dropboxAccessToken]};
headerFields{2,1} = 'Dropbox-API-Arg';
headerFields{2,2} = sprintf('{"path": "/%s"}',FName);
headerFields{3,1} = 'Content-Type';
headerFields{3,2} = 'application/octet-stream';
headerFields = string(headerFields);
% Set the options for WEBREAD
opt = weboptions;
opt.ContentType='binary';
opt.MediaType = 'application/octet-stream';
opt.CharacterEncoding = 'ISO-8859-1';
opt.RequestMethod = 'post';
opt.HeaderFields = headerFields;
% download
try
tempOutput = webread('https://content.dropboxapi.com/2/files/download', opt);
catch someException
throw(addCause(MException('downloadFromDropbox:unableToDownloadFile','Unable to download file.'),someException));
end
f = fopen('myModel.mat','w');
fwrite(f,tempOutput);
fclose(f);
ld = load('myModel.mat');

Communities

More Answers in the  ThingSpeak Community

Community Treasure Hunt

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

Start Hunting!

Translated by