Friday, December 24, 2004

Saving data using string filenames in MATLAB

It is an easy matter to save data as a mat(MATLAB binary) file or an ASCII file so long as you know the filename beforehand. For ex: to save data in the variable datvar to a file mydata.dat, all you have to do is say
save mydata.dat datvar
However if you want to dynamically generate your filename, things get a little tricky since MATLAB's documentation is sparse about this. Here's how:
% Create a filename (myfile.001) using sprintf
fOut = sprintf('myfile.%03d',i);   % say, i=1

% save data as an ASCII file
save(fOut, 'datvar', '-ascii');

% Or, create a mat file - (myfile001.mat)
% since MATLAB binary files typically have the extension .mat
fOut = sprintf('myfile%03d.mat',i);   % say, i=1

% save data as matlab binary
save(fOut, 'datvar');

If you have multiple columns of data, just ensure that datvar is an array. Remember that the .mat file will retain the name of the variable datvar but the ASCII file will only contain data.

Wednesday, December 22, 2004

Creating movies in MATLAB

If you want to show how your plots change with time, a movie is a good idea. Especially since MATLAB can produce movies that can be embedded in your presentations, or be seen with any good ol' movie player. Here's how:

The idea is to create plot figures and compile them as frames of your movie.

% Create a new figure, and position it
    fig1 = figure;
    winsize = get(fig1,'Position');
    winsize(1:2) = [0 0];
% Create movie file with required parameters
    fps= 25;
    outfile = sprintf('%s',outFileName)
    mov = avifile(outfile,'fps',fps,'quality',100);
% Ensure each frame is of the same size
    set(fig1,'NextPlot','replacechildren');
    for i=1:numframes
        plot(x,y); % generate your plot
% put this plot in a movieframe
% In case plot title and axes area are needed
%     F = getframe(fig1,winsize);
% For clean plot without title and axes
        F = getframe;
        mov = addframe(mov,F);
    end
% save movie
    mov = close(mov);

Voila! (Make sure that you do not open any window over your plot window while MATLAB is compiling your movie, else those windows will become part of your movie)

Creating XML outputs with MATLAB

If you want to format your data output as an XML file, here's how to do it without doing any complicated string handling. MATLAB can use the XML handling capabilities of Java very easily. Here's how:

Assume that your data is available in arrays arrData1 and arrData2. Create an XML document node, say Node1 as follows:

docNode = com.mathworks.xml.XMLUtils.createDocument('Node1');
docRootNode = docNode.getDocumentElement;


Now put the data in the data nodes..

for i=1:length(arrData1),
% create nodes..
    elPar = docNode.createElement('DataParent');
    elData1 = docNode.createElement('Data1');
    elData2 = docNode.createElement('Data2');

% put data in nodes..
    elData1.appendChild(...
docNode.createTextNode(sprintf('%f', arrData1(i))));
    elData2.appendChild(...
docNode.createTextNode(sprintf('%f', arrData2(i))));

% put nodes in the correct positions..
    elParent.appendChild(elData1);
    elParent.appendChild(elData2);
    docRootNode.appendChild(elParent);
end

Now save the XML document. You can save it directly, or use uiputfile to get the standard save dialog of the OS.

[filename, pathname] = uiputfile(...
sprintf('%s.xml', datFile), 'Save XML file as');

% Save the XML document.
% xmlFileName = [dataOutFile,'.xml'];
    xmlFileName = fullfile(pathname, filename);
    xmlwrite(xmlFileName,docNode);
% open xml file to see your output
    edit(xmlFileName);

To see more ways of using XML with MATLAB, refer to Java's documentation and play around to see what works.

The MATLAB blog

Over the course of my research and work I have accumulated a fair share of MATLAB hacks where I have used MATLAB to accomplish tasks that go beyond the usual equation-solving and numerical-simulations routines. Mostly I have been motivated by my own laziness and reluctance to code everything in C, or to use a combination of tools, but as I have discovered along the way, MATLAB is truly a very powerful tool and allows you to accomplish complicated stuff using very simple commands. If you have any interesting ways of using MATLAB, feel free to send them in and I will duly acknowledge your contribution. Enjoy!

Moving to a new website

This website was intended to be a little collection of tips and tricks, but I haven't been able to update it as often as I originally in...