Archive

Posts Tagged ‘matlab’

Publish MATLAB output as a document(html, doc, LaTex, Powerpoint etc.,)


Here I wanted to share how did I convert the matlab output to the specific document using the publish command of matlab. We can also run the matlab code from outside matlab window.

The following steps are to be followed to run the matlab code from command prompt.
step1: First edit PATH system variable and set the matlab path as [MATLABINSTALLEDFOLDER/bin/]
step2: Now from the command prompt we can run any file using the following code
<pre>
matlab -r scriptname (without the .m extension but first we have to change directory to the source existing folder)
ex: c:\Users\MyWindows7\MATLAB > matlab -r example
[example.m is the actual script to be run]
</pre>
Now let us actually see how we can publish an output of script as a document

function [ret]= publishToFormat(filename, format)
    options_doc_nocode.format= format;
    options_doc_nocode.outputDir = 'd:/matlabNDVIOutput';
    options_doc_nocode.showCode = false;
    publish(filename,options_doc_nocode)
    ret=1;

In the above code we are providing the name of the function and format to which the script output has to be stored. The above code is simple and straight forward it says the format of the output file and where that output file has to be stored. whether the source code of the script file has to be written to the output or not.

The following links are the ways of using java code in matlab and convert the matlab file into the java file and java web services.

http://www.mathworks.com/products/javabuilder/
http://www.mathworks.com/help/techdoc/matlab_external/f44062.html
http://blogs.mathworks.com/desktop/2009/07/06/calling-java-from-matlab/

Advertisements

Connect MS-Access database from MATLAB


Already I’ve shared the way and source code how to connect MySQL data base from MATLAB. In this post I want to share how to connect to MS-Access. I initially thought of using the MySQL as database but due to some issues of reinstalling the OS and being MySQL not getting installed properly, I went for MS-Access. I hope this post will be very helpful to you if you have come here searching for connecting MS-Access from MATLAB.

function db=insertdb(name, imagePath, ICC)
dbpath = 'C:\Users\MyWindows7\Documents\abc.mdb';
conurl = ['jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)}; DBQ=' dbpath];
con = database('','','','sun.jdbc.odbc.JdbcOdbcDriver', conurl)

%insertQuery=['insert into Image values(' name ',' imagePath ',' ICC ');' ]
%e = exec(con,insertQuery);
colnames={'imgName', 'imagePath', 'iCC'};
values={name, imagePath, ICC};
insert(con, 'Image',colnames, values ) %This statement inserts the values which are contained by values %array variables
%e = fetch(e);
%data = e.Data

In the above program % represents comments you can ignore them if you want. I included them to say that there are several other ways to insert a row into the MS-Access database. In the above code  if you specify the path of database(ie.,        .mdb file) then its enough there is no need for you to specify DSN name and other things. similarly you can also work with other SQL statements. I hope this helps. If you have any more queries or clarifications you can feel free to comment here.

Connect MATLAB with MySQL database


Here is the code to connect MATLAB with MySql.

%database url
url = ‘jdbc:mysql://localhost:3306/’;
%test is a database, root is a user, admin is a password,
%com.mysql.jdbc.Driver is a driver name which was included into the folder
%and accordingly written the [matlabroot]/toolbox/local/classpath.txt
%$matlabroot/java/jar/mysql-connector-java-5.0.8-bin.jar
conn = database(‘test’, ‘root’, ‘admin’, ‘com.mysql.jdbc.Driver’, url);

curs = exec(conn,’SELECT * FROM emp’);
curs = fetch(curs);
curs.data

For the above program to work, you need to have Mysql Driver and you need to specify the path in the matlab path file. Once the above settings are made you can use mysql database from matlab.

How to set and what to set I’ve mentioned in the above program as comments. I hope this helps you.

Read frames from a given video using MATLAB code


Here is the code to read the frames from a given video using MATLAB. It is very easy to read the individual frames from a given video. The code as follows

function [ frame ] = readFrames( vid )

readerobj = mmreader(vid);
vidFrames = read(readerobj);
numFrames = get(readerobj, ‘numberOfFrames’);
for k = 1 : numFrames
mov(k).cdata = vidFrames(:,:,:,k);
mov(k).colormap = [];
%imshow(mov(k).cdata);
imagename=strcat(int2str(k), ‘.jpeg’);
imwrite(mov(k).cdata, strcat(‘vasanth’,imagename));
%extractComponents(mov(k).cdata);

end

mmreader() function supports more number of video format if you go for avireader() it allows only avi files. In the above code mov(k).cdata contains the actual image information. Using imwrite() we are actually writting the image on the harddisk. imagename=strcat(int2str(k), ‘.jpeg’); Here I’m appending the k.jpeg ex: 2.jpeg if you want to have in the other formats you can simply write .bmp or .jpg or .png instead of jpeg in the above code. vidFrames(:,:,:,k); returns the video frames.