Code covered by the BSD License  

Highlights from
C-style MATLAB preprocessor

1.5

1.5 | 2 ratings Rate this file 1 Download (last 30 days) File Size: 2.98 KB File ID: #26713

C-style MATLAB preprocessor

by Misha Koshelev

 

16 Feb 2010 (Updated 19 Feb 2010)

Simple, only supports ifdef/endif/else with define/undef, simple syntax for &&/||.

| Watch this File

File Information
Description

Simple, only supports ifdef/endif/else with define/undef and simple syntax for doing boolean expressions.

My apologies to the author of the MPP package (also available). I needed something quick & dirty. Seems to work. Comes with test file.

MATLAB release MATLAB 7.6 (R2008a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
17 Feb 2010 Jan Simon

I was really happy to see a preprocessor for M-files here, because this could solve a lot of problems.
But after downloading I am disappointed, because there is not documentation in the function!
  function [varargout]=mpp(fn,varargin)
  % Simple MATLAB C-like preprocessor
  % <Author> <Date>
That's all. So I tried to look in the code to get some informations:
Initially the function searchs for something:
  if ~exist([fn,'.m1'])
This could mean a directory, a file name anywhere in the path, a Java class. To check the existence of a file, use EXIST(FileName, 'file') ! This is much faster and less ambiguous.

Then the name of the TEMP folder is obtained:
  tempdir=tempname;
  i=length(tempdir);while tempdir(i)~=filesep&&i>0;i=i-1;end;
  tempdir(i+1:end)=[];
1. "while tempdir(i) ~= filesep && i>0" fails if [i] is 0! Swap the two statements to: "while i> 0 && tempdir(i)~=filesep"
2. Or even better call FILEPARTS:
  tempdir = fileparts(tempname);
3. Or even much better: Do not overwrite Matlab's built-in function with the same name, but use it: TEMPDIR replies the TEMP directory...

I cannot estimate what the complex processing part does. Unfortunately no comments can be found here.

Finally you add the TEMP and its subfolders to the Matlab path "addpath(genpath(tempdir))" without removing it afterwards. This can lead to unwanted side-effects.

As conclusion I confirm the description: "something quick & dirty. Seems to work."
I'd strongly suggest some general improvements: Clean up the code, add much more comments, add a help section which explain inputs, outputs and what happens. Otherwise your program is not useful or usable by members of the FEX.
The idea is really good and MPP Fex: #7150 does not satisfy my needs.

18 Feb 2010 Siyi Deng  
Please login to add a comment or rating.
Updates
18 Feb 2010

Changed based on comments - thank you. Intended use for others is by running mpp('mpptest',param). Added some comments. Simple but works well.

18 Feb 2010

Support for simple recursion (see examples).

19 Feb 2010

Added support for pre-specifying defines on command line (_very_ raw). Also use new path each time, so that can actually run several instances of MATLAB, same m1 file, different defines, concurrently.

19 Feb 2010

Swapped rmdir and path statements to avoid warning.

Tag Activity for this File
Tag Applied By Date/Time
preprocessor Misha Koshelev 17 Feb 2010 10:48:22
c Misha Koshelev 17 Feb 2010 10:48:22

Contact us at files@mathworks.com