Export matlab visualisation as an image? | MATLAB | Forum

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
Export matlab visualisation as an image?
No permission to create posts
September 16, 2016
2:07 am
Avatar
rw950431

Top
Members
Forum Posts: 261
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

Anyone know if its possible to export a matlab visualization automatically as an image file?  Say I wanted to save a snapshot at a given time or when a given condition occurs.

September 16, 2016
2:45 pm
Avatar
Vinod

MathWorks
Members
Forum Posts: 200
Member Since:
May 1, 2016
sp_UserOfflineSmall Offline

It should be possible to use the SAVEAS function in MATLAB to save a figure to an image and then upload it to dropbox using dropbox's REST API and the WEBWRITE function. The condition detection could be in your MATLAB code.

September 16, 2016
5:50 pm
Avatar
Vinod

MathWorks
Members
Forum Posts: 200
Member Since:
May 1, 2016
sp_UserOfflineSmall Offline
Since an example is more useful than general instruction, I have this example for you. 
 
Step 1: Sign up for a dropbox account if you don't already have one.
Step 2: Go to https://www.dropbox.com/developers/apps and create a new App. Select "Dropbox API" and "App folder" as your choice for item (1) and (2). For item (3), pick a name. I use "ThingSpeak" as the name of the folder. Click "Create App"
Step 3: On the page click "Generate access token". Copy down this string exactly; it will be needed in your MATLAB app.
Step 4: In ThingSpeak, create a new MATLAB Visualization app
For the MATLAB code, type this:
 
 
tstamp = regexprep(char(datetime('now','format','yyyy-MM-dd''T''HH:mm')),':|\ |-','_');
fname = fullfile(tempdir,sprintf('ImageGeneratedAt_%s.jpg', tstamp));
plot(rand(1,10));
if <some condition>
     saveas(gcf,fname);
     dropboxAccessToken = '<insert your dropbox access token here, without angle brackets>';
     fid = fopen(fname, 'r');
     data = char(fread(fid)');
     fclose(fid);
     headerFields = {'Authorization', ['Bearer ', dropboxAccessToken]};
     headerFields{2,1} = 'Content-Length';
     headerFields{2,2} = string(length(data));
     headerFields = string(headerFields);
 
     opt = weboptions;
     opt.MediaType = 'application/octet-stream';
     opt.CharacterEncoding = 'ISO-8859-1';
     opt.RequestMethod = 'post';
     opt.HeaderFields = headerFields;
      [~,remoteFName, remoteExt] = fileparts(fname);
 
     webwrite(sprintf('https://api-content.dropbox.com/1/files_put/auto/%s%s',remoteFName,remoteExt), data, opt);
end
 
Modify your to be the condition when you want to save the snapshot of your MATLAB visualization. Remember to modify the dropboxAccessToken to exactly be the token you got from dropbox.
 
Presumably you will replace the plot(rand(1,10)) with some other interesting plot you want to make.
Now, whenever this MATLAB App is run, if <some condition> is met, you will see a .jpg file show up in your dropbox folder. Enjoy!
September 16, 2016
6:22 pm
Avatar
Vinod

MathWorks
Members
Forum Posts: 200
Member Since:
May 1, 2016
sp_UserOfflineSmall Offline
If you can define your condition in a React app, then you can create a MATLAB Analysis app that runs in the React to generate the plot in your Dropbox folder. The MATLAB analysis app would be:
 
tstamp = regexprep(char(datetime('now', 'format', 'yyyy-MM-dd''T''HH:mm')), ':|\ |-', '_');
fname = fullfile(tempdir,sprintf('ImageGeneratedAt_%s.jpg', tstamp));

eval('plot(rand(1,10))');

saveas(gcf, fname);

dropboxAccessToken = '<insert your dropbox access token here, without angle brackets>';

fid = fopen(fname, 'r');
data = char(fread(fid)');
fclose(fid);
headerFields = {'Authorization', ['Bearer ', dropboxAccessToken]};
headerFields{2,1} = 'Content-Length';
headerFields{2,2} = string(length(data));
headerFields = string(headerFields);
 
opt = weboptions;
opt.MediaType = 'application/octet-stream';
opt.CharacterEncoding = 'ISO-8859-1';
opt.RequestMethod = 'post';
opt.HeaderFields = headerFields;
[~,remoteFName, remoteExt] = fileparts(fname);
 
webwrite(sprintf('https://api-content.dropbox.com/1/files_put/auto/%s%s',remoteFName,remoteExt), data, opt);
 
This MATLAB analysis app will be triggered based on your condition specified in the React and the MATLAB visualization should show up in your Dropbox's Apps/ThingSpeak folder. You will need to modify the eval('plot(rand(1,10))') to do whatever interesting visualization you want.
September 17, 2016
4:49 pm
Avatar
piajola

Gold
Members
Forum Posts: 87
Member Since:
October 12, 2015
sp_UserOfflineSmall Offline

Hi Vinod,

Thank you for the "tips"Smile

My experience follows:

Blindly following your steps with Visualizations, worked fine ...
I have some working Visualizations:
- with Plot
- with Annotation (sort of color boxes with numbers inside)
- with Histogram
all worked, all are in DropBox

BUT ...

I have two types

- with thingSpeakPlot(time, [ humidityData,temperatureData], 'legend',{'H %', 'T *C'});

- with thingSpeakPlotYY( time2, data2,time1, data1, 'YGrid1','on','YLabel1','% H.R.',...
'YLabel2','*C','XLabel','Feha y hora',...
'Title','Tempix y Humix ultimas semana');

code finished with this error(?) message

curl: /opt/mlsedu/matlab/R2016b/bin/glnxa64/libcurl.so.4: no version information available (required by curl)
{"revision": 15, "bytes": 9704, "thumb_exists": true, "rev": "f4e35680f", "modified": "Sat, 17 Sep 2016 20:06:49 +0000", "mime_type": "image/jpeg", "path": "/Mimagen_2016_09_17T15_06.jpg", "is_dir": false, "size": "9.5 KB", "root": "app_folder", "client_mtime": "Sat, 17 Sep 2016 20:06:48 +0000", "icon": "page_white_picture"}

with a white box in DropBox. There is a file.

Not tried your Analysis because I do not know how to eval(...) my "visualizations". Your example is one line eval('one_line_only') and I need to read data (thingspeakRead) and more lines with the parameters, etc. How to do that

And keep the "tips" comingWink

September 18, 2016
9:45 pm
Avatar
Vinod

MathWorks
Members
Forum Posts: 200
Member Since:
May 1, 2016
sp_UserOfflineSmall Offline

thingSpeakPlot and thingSpeakPlotYY use a different technology and my solution will not work for those functions.

However, you can still use the PLOT and YYAXIS functions in your MATLAB Analysis app and use the suggestion as above. As for multi line analysis, you can do all of that using MATLAB code. The only thing that needs to be in the eval('<plot something>') is the MATLAB commands for plotting, which is typically just one line of code anyway.

Switching to pseudo code as I don't have a concrete example:

thingSpeakRead(....)
MATAB code for mathematical analysis ...
eval('<plot something>')
MATLAB code to capture snapshot and upload to dropbox ... 

If you have a public channel with data that you would be comfortable sharing, along with the MATLAB analysis and visualizations you currently have, I may be able to transcode it for you.

September 19, 2016
1:41 am
Avatar
rw950431

Top
Members
Forum Posts: 261
Member Since:
January 30, 2014
sp_UserOfflineSmall Offline

Vinod/

   Thats a neat hack! Wonder if the same technique could be used with imgur?  Looks a little more complex due to OAuth and the 1-month expiry on access tokens. https://api.imgur.com/oauth2

September 19, 2016
8:09 pm
Avatar
piajola

Gold
Members
Forum Posts: 87
Member Since:
October 12, 2015
sp_UserOfflineSmall Offline

Hi Vinod,

Thank you very much for your explanation and offer.

Sorry for the delayed post

Reading PLOT and YYAXIS and trying I have now the plots going to Dropbox

 

Again thanks a lot Smile

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
12 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 261

Vinod: 196

piajola: 85

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

Newest Members:

harriettlf60, ShaToow, Aleftinfenny, DiaBentee, hugo11dom@gmail.com, Pisaruk530

Administrators: Hans: 387, lee: 457