Program works in MATLAB, but doesn't work after compiling as a standalone application

42 views (last 30 days)
I'm trying to use MATLAB compiler to make a program a standalone application. The program runs fine in MATLAB, but gives an error as a standalone application. I don't think the problem is with missing files or mismatched versions of the compiler and runtime.
The compiled program runs at first, the gives an error and tells me on what line it occurred. The problem is with the webwrite command. I'm using it to send an email through the Gmail API. This requires me to change the MediaType in weboptions from the default of 'application/x-www-form-urlencoded' to 'application/json'. When I include the email message in the webwrite command in a structure, it is converted to a JSON object and posted to the specified URL. This works in MATLAB, but the compiled program gives an error that says 'The input cannot be converted to JSON'. The lines of code pertaining to this are:
options = weboptions('MediaType','application/json','RequestMethod','post');
data=struct('raw',messageb64); %messageb64 is the email encoded in base64
webwrite(strcat('https://www.googleapis.com/gmail/v1/users/me/messages/send?access_token=',accesstoken),data,options)
I'm guessing the input mentioned in the error is the structure, but there is no problem encoding it as JSON in MATLAB, so I don't know what the problem is.
EDIT: I have looked at the FAQ sites and tried some of their suggestions. I confirmed that my versions of MATLAB and runtime match. I have also confirmed that the functions I created are all there. I have successfully compiled other programs; it really seems to be an issue with the webwrite command.
In response to Dasharath's questions:
1. I don't want to post the full code. There are 6 functions it depends on, which would be a pain to post. More importantly, because I am working with the gmail api, providing my full code would allow anybody to send email from my account. I'm using a test email, but I still don't want to do this. I know this makes it way harder for people to help, and I appreciate your efforts. I can post segments of the code if needed. I might write a version that will allow people to use their own emails. Let me know if that would be helpful.
2. I am doing all the coding and testing on the same machine. I just got back to college, so I might test it out on the school computers.
3. Access token is part of the URL the webwrite command posts to. It is a code that the Gmail API uses for authentication, and is only valid for an hour before needing to be refreshed, so that is why it is a variable and not just typed in like the rest of the URL. The message is passed in the structure named "data". I'm not entirely sure how it works, as I relied largely on trial and error, but the MATLAB documentation says it is converted to a JSON object.
4. The exact text of the error is "The input cannot be converted to JSON. Error in => prettyaurora.m at line 27."
Thanks for your help.
  1 Comment
Dasharath Gulvady
Dasharath Gulvady on 21 Aug 2015
  1. Could you show us the complete code with which we can try to reproduce the issue on our end?
  2. Is the issue on the same machine where it works in MATLAB but not in standalone application? Or is this on a different machine?
  3. How are ""accesstoken" and "message" passed? Is there any difference in the way these values are passed to the function in MATLAB and standalone? Is the accesstoken same in both the cases?
  4. Could you show us the complete error message?

Sign in to comment.

Accepted Answer

Daniel LaCroix
Daniel LaCroix on 7 Sep 2015
I ended up contacting MathWorks directly about this. It took several emails and several employees, but they finally figured it out (Thanks guys!), so I figured I would post the solution.
This error with compiling a program containing webwrite is a known bug in MATLAB. To make it work, copy the folder MATLABROOT\toolbox\matlab\connector\connector\+mls to the same folder the program you're compiling is in. Then add this folder to the list of files required to run in the compiler. It should work after this.
  3 Comments
Image Analyst
Image Analyst on 20 Apr 2018
Are you saying his solution of "I ended up contacting MathWorks directly about this." didn't work? Well if even the Mathworks can't figure it out, then I guess you're out of luck.
Danial DanialB
Danial DanialB on 22 Jan 2019
Thank you so much!!!
Had same problem but with sym function and i fixed it by adding MATLABROOT\toolbox\symbolic folder.

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 21 Aug 2015

Walter Roberson
Walter Roberson on 23 Aug 2015
You may wish to experiment with http://www.mathworks.com/matlabcentral/fileexchange/45238-jsonrpc2--a-set-of-classes-to-encode-decode-json-rpc-messages or http://www.mathworks.com/matlabcentral/fileexchange/33381-jsonlab--a-toolbox-to-encode-decode-json-files-in-matlab-octave -- not necessarily to do the heavy work but to help you debug what is going on, since it would be possible to add debugging and tracing into those routines as needed to figure out why you cannot encode.
  1 Comment
Daniel LaCroix
Daniel LaCroix on 24 Aug 2015
Thanks for the ideas. I downloaded one of them and was able to use it successfully to parse the structure into a JSON object. Once I had the JSON object though, I wasn't sure what to do with it. (I've been working with MATLAB for less than a year, so I tend to have trouble when there is little documentation.) I tried sending it to the same URL with webread, but wasn't sure how to include it in the webread command.
I like the idea of encoding the message as JSON with a different function, but I can't figure out what to do with it once it's encoded.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!