Matlab, jQuery and JSON

I do all my numerical algorithm development in Matlab, so I needed a way to easily get results from Matlab into javascript. Here is a (a pretty cool if I don’t say so myself) matlab code that creates a JSON file from a matlab data structure.

data.x = rand(10,1);
data.y = rand(10,1)

dataname = 'data' 

fid = fopen('matlab.json','w');

numdata = max(size(fieldnames(eval(dataname))))

fdata = char(fieldnames(eval(dataname)));

fprintf(fid,'{ "%s" : {\n ',dataname)
for dataiter=1:numdata,
  fprintf(fid,' "%s" : [ \n',fdata(dataiter));
  stmp = sprintf('%s.%s',dataname,fdata(dataiter));
  for i=1:(max(length(eval(stmp))))-1,
    stmp = sprintf('%s.%s(%i)',dataname,fdata(dataiter),i);
    fprintf(fid,'%1.10e , \n',eval(stmp));
  stmp2 =  sprintf('%s.%s',dataname,fdata(dataiter));;
  stmp = sprintf('%s.%s(%i)',dataname,fdata(dataiter),(max(length(eval(stmp2)))));
  fprintf(fid,'%1.10e  \n',eval(stmp));

  if dataiter == numdata ,
    fprintf(fid,'] \n');
    fprintf(fid,'], \n');


fprintf(fid,'} } \n');

And then reading it in and parsing it with jQuery is really simple.

 var x = [];
 var y = [];
 $.ajaxSetup({ async: false});
  $.getJSON("matlab.json", function (json)
              x =;
              y =;

Welcome to Loglet in the Clouds

I am currently (re)-writing the Loglet Lab¬†software application into a “cloud” application.¬† Basically, it means re-writing the algorithms in javascript, so that it is easy to plug into and “mash up” the Web 2.0 “cloud” (i.e. as a plug-in (gadget) for google spreadsheets).¬† As the code will be open source ( current license LPGL 2.1 ), I’ll post things that other users or developers of Loglet Lab Cloud edition might find useful.

Here is an example of a proof-of-concept google gadget for google spreadsheets, which uses the excellent flot plotting library for the jQuery framework: