Archive for the ‘code’ Category

jsoncpp writing back the structures and arrays

May 24, 2013 Leave a comment

Well successfully read the json file of the matlab mat structure. Lots of things to care of, not as simple as it looks.

Now writing back, that is taking the C++ class, structure and basic data types and outputting a json structure. Since I have already written the reader to the writer should match the reader’s expectation.

This one is quite decent information and advice on how to design class for such activity.


Will follow the writing part now and later will try to implement the reader in much more structured way.

Categories: C++, code, jsoncpp Tags: , , , ,

parsing jason nested array using jsoncpp

May 22, 2013 Leave a comment

so first experiments with the json and json cpp.

the json string is as follow

{“id”: 1,”tags”: [ [800, 99.7], [], [76] ],”stock”: {“warehouse”: 300,”retail”: 20}}

Wanted to read the nested arrays of “tags” as you can see such an array could be e.g. cell datatype in the matlab or could be represented as the array of vectors

int main (){</pre>
unsigned int i,j;
char fileName[] ="abc.json";

ifstream infile;
infile.exceptions (ios::failbit|ios::badbit);
{ (fileName);
catch (ifstream::failure& e)
cerr<<"\n Exception opening file "<<fileName<<": "<<e.what ();
return 0;

string input;
Json::Reader reader;
Json::Value root;
bool parsingSuccessful;

while (getline (infile,input,'\n'))
if (false == (parsingSuccessful = reader.parse (input,root)))
cerr<<"\nFailed to parse configuration:"
<<reader.getFormatedErrorMessages ();
return 0;

int verbName = root.get ("id","").asInt ();
Json::Value data = root.get ("tags",0);
cout << "size is " << data.size() <<endl;

//vector<float> freq (mWidth * mHeight);
for (int i = 0; i < data.size(); i++){
int sizeint = data[i].size();
cout <<endl <<"---------------" <<endl;
cout << "Size of individual is " <<sizeint << endl;
Json::Value dat1 = data[i];

for(int j=0; j< dat1.size(); j++){
cout << dat1[j].asDouble() << " ,";

catch (ifstream::failure& e)


return 0;
Categories: C++, code, jsoncpp, matlab Tags: , , , , , ,

Running Makefile on the Windows

August 1, 2012 Leave a comment

There are many ways to run make program on the Windows, many of them will ask you to run in cygwin or similar shells. I was interested in running the makefile in the Windows so that the application generated could be used by the other Windows programs.
So the solution is to use the Visual Studio Command Prompt, run it, cd to the directory where the makefile is and then use the following

nmake -f Makefile

If the Makefile is using g++ or gcc in it you have to change it to “cl”, this the compiler being used by the VC. You might have to make little changes but then it should work fine.


Have a look at following


Categories: C++, code Tags: , , , ,

srun and matlab

November 19, 2011 Leave a comment

ok Mr. Cluster, here I come, I have unlocked the power to issue commands to your nodes for my maltab function ha ha ha ha!

so if you are in slurm
srun -n1 -o t.txt matlab -nodesktop -nosplash -nodisplay -r “temp4(29)” &

t.txt <— will get all the output

after -r you can give all the commands as the form of script i.e. ” ”

in above case temp4 was matlab file function, and 29 was input

& so that it can run in background

-n1 only one measly node 😦

But then you can run multiple of such commands and each one gets node 🙂

Don’t forget to open the matlabpool in the file you are running if you are using the parallel commands.

Categories: basic, code, matlab Tags: , , , , ,

getPatch; matlab function to get a patch from a matrix

April 1, 2011 Leave a comment
function mat = getPatch(srcMat, rect)
%function getPatch(srcMat, rect)
%rect is [x y width height]

stX = max(rect(1),1);stY = max(rect(2),1);
endX = min(rect(1)+rect(3)-1, size(srcMat,1));endY = min(rect(2)+rect(4)-1, size(srcMat,2));
mat = srcMat(stY:endY, stX:endX);
Categories: code, matlab, matlab Code Tags: ,

Dynamic Multi-dimensional memory allocation C/C++

October 15, 2010 Leave a comment

An excellent library for the multi dimensional memory allocation.

some other code snippets are

calculating distance matrix; Matlab

July 7, 2010 Leave a comment

A simple code to calculate the L2 distance matrix between any features.

UPDATE: found pdist and pdist2 functions in matlab, so no more need of following function

function D = calculateDistMatrix( points1, points2, f)
%D = calculateDistMatrix( points1, points2)
%points1: is m by n matrix, where n is number of points
if nargin ❤
    f = 0;
totalPoints1 = size(points1,2);
totalPoints2 = size(points2,2);
if f == 0
D= zeros(totalPoints1, totalPoints2);
D = sparse(totalPoints1, totalPoints2);
for i=1:totalPoints1
    D(i,:) = sqrt(sum((points2-repmat(points1(:,i), 1,totalPoints2)).^2));