http://www.mathworks.com/matlabcentral/newsreader/view_thread/255839
MATLAB Central Newsreader  check syntax errors in m file
Feed for thread: check syntax errors in m file
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Fri, 10 Jul 2009 17:23:01 +0000
check syntax errors in m file
http://www.mathworks.com/matlabcentral/newsreader/view_thread/255839#664342
Andrej S
hi,<br>
how can I programmatically check (syntax) errors in the m file?<br>
I was trying mlint, but there I can not distinguish between errors and warnings...<br>
<br>
thanks,<br>
a.

Thu, 17 Jun 2010 21:46:04 +0000
Re: check syntax errors in m file
http://www.mathworks.com/matlabcentral/newsreader/view_thread/255839#755452
Mason Freed
Any updates on this question?? There MUST be some way to distinguish warnings from syntax errors using the mlint() function. The information is obviously there  the editor shows them in different colors.<br>
<br>
Anybody?

Thu, 17 Jun 2010 22:24:05 +0000
Re: check syntax errors in m file
http://www.mathworks.com/matlabcentral/newsreader/view_thread/255839#755467
Matt Fig
This seems to work, if I understand what you are after:<br>
<br>
G = mlint('funcname')<br>
strfind({G.message},'error') % Returns empties if no word 'error' is found'

Thu, 17 Jun 2010 22:37:04 +0000
Re: check syntax errors in m file
http://www.mathworks.com/matlabcentral/newsreader/view_thread/255839#755473
Mason Freed
Thanks for the quick response Matt.<br>
<br>
However, your suggestion unfortunately doesn't work. For example, if I write this function:<br>
<br>
<br>
function myFunc()<br>
<br>
foo=2<br>
for ii=1:10<br>
disp(ii);<br>
bar=foo+3;<br>
<br>
<br>
In the editor, line 4 (the unended forloop) shows up as a red error. However, running mlint doesn't return the word "error" at all:<br>
<br>
>> mlint('myFunc.m')<br>
L 3 (C 4): Terminate statement with semicolon to suppress output (in functions).<br>
L 4 (C 13): An END might be missing, possibly matching FOR.<br>
L 6 (C 13): The value assigned to variable 'bar' might be unused.<br>
<br>
From the mlint output, there's no way to tell that the 2nd message is an error, while #1 and #3 are warnings.<br>
<br>
Any other thoughts?<br>
<br>
Thanks,<br>
Mason

Thu, 17 Jun 2010 23:10:20 +0000
Re: check syntax errors in m file
http://www.mathworks.com/matlabcentral/newsreader/view_thread/255839#755481
Matt Fig
Ah, I see. It seems to me that to do what you want would be tedious, but possible. Here is one way I can see through to do it. Open up the preferences and then look at mlint. Copy the string down for every error type that you want to check for, then in your function compare the results of the call to MLINT with a string database. For example, here is a brief sketch. Note that some of the string manipulation could be optimized, this is just for illustration as to how one could approach the problem:<br>
<br>
function tf = any_errors(fname)<br>
% Returns logical true if MLint detectable errors are found in FNAME.<br>
tf = 0;<br>
G = mlint(fname);<br>
<br>
if any(~cellfun(@isempty,strfind({G.message},'error')))<br>
return<br>
end<br>
<br>
DB = {'BREAK can only be used in';<br>
'Apparently a quoted string';<br>
'Apparently an END is missing'}; % Add as many as you want.<br>
<br>
for ii = 1:length(DB)<br>
if any(~cellfun(@isempty,strfind({G.message},DB{ii})))<br>
tf = 1;<br>
return<br>
end<br>
end

Fri, 18 Jun 2010 13:59:17 +0000
Re: check syntax errors in m file
http://www.mathworks.com/matlabcentral/newsreader/view_thread/255839#755634
Steven Lord
<br>
"Mason Freed" <mfreedREMOVETHIS@mfreedREMOVETHIS.com> wrote in message <br>
news:hve52s$1o9$1@fred.mathworks.com...<br>
> Any updates on this question?? There MUST be some way to distinguish <br>
> warnings from syntax errors using the mlint() function. The information is <br>
> obviously there  the editor shows them in different colors.<br>
><br>
> Anybody?<br>
<br>
What do you intend to do with this information? Do you want simply to check <br>
if the function _can_ run without actually running it? Or do you want to <br>
get a list of errors so that you can tell someone specifically what <br>
lines/sections to fix?<br>
<br>
 <br>
Steve Lord<br>
slord@mathworks.com<br>
comp.softsys.matlab (CSSM) FAQ: <a href="http://matlabwiki.mathworks.com/MATLAB_FAQ">http://matlabwiki.mathworks.com/MATLAB_FAQ</a><br>
To contact Technical Support use the Contact Us link on <br>
<a href="http://www.mathworks.com">http://www.mathworks.com</a>

Sat, 19 Jun 2010 21:29:04 +0000
Re: check syntax errors in m file
http://www.mathworks.com/matlabcentral/newsreader/view_thread/255839#755939
Yair Altman
"Steven Lord" <slord@mathworks.com> wrote in message <hvfu3l$cd0$1@fred.mathworks.com>...<br>
> <br>
> "Mason Freed" <mfreedREMOVETHIS@mfreedREMOVETHIS.com> wrote in message <br>
> news:hve52s$1o9$1@fred.mathworks.com...<br>
> > Any updates on this question?? There MUST be some way to distinguish <br>
> > warnings from syntax errors using the mlint() function. The information is <br>
> > obviously there  the editor shows them in different colors.<br>
> ><br>
> > Anybody?<br>
> <br>
> What do you intend to do with this information? Do you want simply to check <br>
> if the function _can_ run without actually running it? Or do you want to <br>
> get a list of errors so that you can tell someone specifically what <br>
> lines/sections to fix?<br>
<br>
<br>
In one of my applications I needed to enable the user to dynamically create executable Matlab code that would then be run interactively. This enabled users to create dynamic data analyses functions without actually needing to know Matlab or to code all the nutsandbolts of a regular Matlab function. For this I needed to display warnings and errorsonthefly and the OP's question goes directly to this need.<br>
<br>
My solution was to use the undocumented internal function mlintmex, as follows:<br>
<br>
errMsgs = mlintmex('m2', srcFileName);<br>
allMsgs = mlintmex('m0', srcFileName);<br>
<br>
numErrors = length(strfind(regexprep(errMsgs,'\*\*\*.*',''),char(10)));<br>
numAllMsg = length(strfind(regexprep(allMsgs,'\*\*\*.*',''),char(10)));<br>
numWarns = numAllMsg  numErrors;<br>
<br>
(and from the messages themselves I extracted the actual error/warning location)<br>
<br>
Yair Altman <br>
<a href="http://UndocumentedMatlab.com">http://UndocumentedMatlab.com</a>

Mon, 21 Jun 2010 15:23:21 +0000
Re: check syntax errors in m file
http://www.mathworks.com/matlabcentral/newsreader/view_thread/255839#756221
Mason Freed
First, thanks Yair. Your suggestion works perfectly, in a handful of lines. How do you find all of your nifty undocumented stuff?<br>
<br>
Matt, thanks for the suggestion, which I think would work, but Yair's is much easier to implement.<br>
<br>
And finally, Steven, the reason for my request is that I'm building an automated unit test system. It will run over all of our Matlab code whenever the source control system (subversion) registers a new commit. I want one of the policies to be that committed code contain no syntax errors. So I want a quick way to identify only the "killer" syntax errors. I can't make the policy that mlint reports no warnings at all, because the other developers would kill me for it. (Much as I would like to enforce that as a constraint.)<br>
<br>
Thanks,<br>
Mason

Mon, 21 Jun 2010 16:17:24 +0000
Re: check syntax errors in m file
http://www.mathworks.com/matlabcentral/newsreader/view_thread/255839#756234
Yair Altman
"Mason Freed" wrote in message ...<br>
> First, thanks Yair. Your suggestion works perfectly, in a handful of lines. How do you find all of your nifty undocumented stuff?<br>
<br>
There are two main sources: reading/searching this newsgroup and reading the internal code of Matlab's mfiles. I make a habit of noting down new stuff which is undocumented  then when the need occurs (often years later), I have an immediate answer. <br>
<br>
Here's another thread that documents mlintmex usage that you may find useful, which I posted a few years ago: <a href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/145245">http://www.mathworks.com/matlabcentral/newsreader/view_thread/145245</a><br>
<br>
In this particular case, I think I learned about mlintmex from the indomitable Us (Urs) Schwartz, but I can't find the original reference now. He also posted the related utilities FDEP and FARG on the File Exchange that you may find useful (as are all of his utilities). In any case, Us posted the following humorous comment about the source of *his* knowledge some years ago: <a href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/115423#292260">http://www.mathworks.com/matlabcentral/newsreader/view_thread/115423#292260</a> <br>
<br>
Yair Altman <br>
<a href="http://UndocumentedMatlab.com">http://UndocumentedMatlab.com</a>

Mon, 21 Jun 2010 17:06:22 +0000
Re: check syntax errors in m file
http://www.mathworks.com/matlabcentral/newsreader/view_thread/255839#756252
Mason Freed
"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <hvo3ak$aeb$1@fred.mathworks.com>...<br>
> "Mason Freed" wrote in message ...<br>
> There are two main sources: reading/searching this newsgroup and reading the internal code of Matlab's mfiles. I make a habit of noting down new stuff which is undocumented  then when the need occurs (often years later), I have an immediate answer. <br>
> <br>
> Here's another thread that documents mlintmex usage that you may find useful, which I posted a few years ago: <a href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/145245">http://www.mathworks.com/matlabcentral/newsreader/view_thread/145245</a><br>
> <br>
> In this particular case, I think I learned about mlintmex from the indomitable Us (Urs) Schwartz, but I can't find the original reference now. He also posted the related utilities FDEP and FARG on the File Exchange that you may find useful (as are all of his utilities). In any case, Us posted the following humorous comment about the source of *his* knowledge some years ago: <a href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/115423#292260">http://www.mathworks.com/matlabcentral/newsreader/view_thread/115423#292260</a> <br>
> <br>
> Yair Altman <br>
> <a href="http://UndocumentedMatlab.com">http://UndocumentedMatlab.com</a> <br>
<br>
Interesting  thanks for the references Yair.<br>
<br>
One note that I found, related to both mlint and mlintmex: they seem to choke on files with spaces in the filename. These give back "Filename {strangely modified path here} is not formed from a valid MATLAB identifier." I'm writing a quick util to copy filenames containing spaces to a temp file and run mlintmex there, but do you have any other magic suggestions to tackle this issue?<br>
<br>
Thanks,<br>
Mason

Mon, 21 Jun 2010 17:24:08 +0000
Re: check syntax errors in m file
http://www.mathworks.com/matlabcentral/newsreader/view_thread/255839#756257
Yair Altman
"Mason Freed" <mfreedREMOVETHIS@mfreedREMOVETHIS.com> wrote in message <hvo66e$gfu$1@fred.mathworks.com>...<br>
<br>
> One note that I found, related to both mlint and mlintmex: they seem to choke on files with spaces in the filename. These give back "Filename {strangely modified path here} is not formed from a valid MATLAB identifier." I'm writing a quick util to copy filenames containing spaces to a temp file and run mlintmex there, but do you have any other magic suggestions to tackle this issue?<br>
<br>
<br>
No magic suggestions for the simple reason that Matlab mfiles should not have a space in their filenames... If you're doing checkin sanity checks as you wrote above then this would be one obvious source of reported error.<br>
<br>
Yair Altman <br>
<a href="http://UndocumentedMatlab.com">http://UndocumentedMatlab.com</a>

Mon, 21 Jun 2010 17:34:22 +0000
Re: check syntax errors in m file
http://www.mathworks.com/matlabcentral/newsreader/view_thread/255839#756264
Mason Freed
"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <hvo77o$naj$1@fred.mathworks.com>...<br>
<br>
> No magic suggestions for the simple reason that Matlab mfiles should not have a space in their filenames... If you're doing checkin sanity checks as you wrote above then this would be one obvious source of reported error.<br>
> <br>
> Yair Altman <br>
> <a href="http://UndocumentedMatlab.com">http://UndocumentedMatlab.com</a> <br>
<br>
Thanks. I was under the impression originally that the issue occurred on PATHS with spaces in the name (e.g. "C:\Documents and settings") but you're exactly right. The only issue is with file NAMES that contain spaces. So no issue at all (and a good addition to the check!).<br>
<br>
Thanks again for the quick responses Yair.<br>
<br>
Mason

Mon, 21 Jun 2010 19:01:06 +0000
Re: check syntax errors in m file
http://www.mathworks.com/matlabcentral/newsreader/view_thread/255839#756297
Walter Roberson
Mason Freed wrote:<br>
> I can't make the policy that mlint reports no warnings at all, <br>
> because the other developers would kill me for it. (Much as I would like <br>
> to enforce that as a constraint.)<br>
<br>
It isn't a reasonable constraint until at least 2009b:<br>
<br>
[A, B, C] = SomeCall(D);<br>
<br>
If it happens that the code only needs C and not A or B, then the author <br>
should not be forced to do something like write A and B to an output file that <br>
is then deleted just so that the values are "used" so as to avoid the warning.<br>
<br>
The latest versions of Matlab allow<br>
<br>
[~, ~, C] = SomeCall(D)<br>
<br>
but then you are relying on a new feature and destroying backwards compatibility.<br>
<br>
<br>
Likewise, it is not uncommon to write routines that follow a standard calling <br>
procedure and have arguments passed in to them that that _particular_ routine <br>
doesn't happen to use but where the routines are "black boxes" from outside. <br>
For example if you are doing tabledriven calls to an appropriate optimization <br>
function then the calls have to pass in all the parameters that _any_ of the <br>
optimization routines might need. There might, for example, be a tuning <br>
parameter that only makes sense for one kind of optimization but because of <br>
the common calling sequence has to be passed to all of them. You don't want to <br>
force the author to somehow uselessly "use" the parameter just to avoid the <br>
warning. (This too is solved with ~ in the very newest Matlabs, but again that <br>
has costs.)

Mon, 21 Jun 2010 20:42:24 +0000
Re: check syntax errors in m file
http://www.mathworks.com/matlabcentral/newsreader/view_thread/255839#756346
Mason Freed
Walter Roberson <roberson@hushmail.com> wrote in message <hvod1p$855$1@canopus.cc.umanitoba.ca>...<br>
> Mason Freed wrote:<br>
<br>
> It isn't a reasonable constraint until at least 2009b:<br>
<br>
I would disagree  as far as I know, the '%#ok' construct has been around for as long as mlint has. So it can be reasonably used to explicitly suppress warnings for known cases (as your examples show). I always come down on the side of eliminating all warnings  it is just a safer way to do things, and doesn't add too much overhead to the programmer. Just my humble opinion, of course!<br>
<br>
Thanks,<br>
Mason

Tue, 22 Jun 2010 08:20:14 +0000
Re: check syntax errors in m file
http://www.mathworks.com/matlabcentral/newsreader/view_thread/255839#756449
Walter Roberson
Mason Freed wrote:<br>
> Walter Roberson <roberson@hushmail.com> wrote in message <br>
> <hvod1p$855$1@canopus.cc.umanitoba.ca>...<br>
>> Mason Freed wrote:<br>
> <br>
>> It isn't a reasonable constraint until at least 2009b:<br>
> <br>
> I would disagree  as far as I know, the '%#ok' construct has been <br>
> around for as long as mlint has. So it can be reasonably used to <br>
> explicitly suppress warnings for known cases (as your examples show). I <br>
> always come down on the side of eliminating all warnings  it is just a <br>
> safer way to do things, and doesn't add too much overhead to the <br>
> programmer. Just my humble opinion, of course!<br>
<br>
That policy will just lead to people routinely turning off "all <br>
instances in file" of warnings without examination. The medicine will be <br>
worse than the problem.<br>
<br>
We turn off warnings only when we completely understand their causes and <br>
know that the existing code is appropriate. Sometimes we don't <br>
completely understand the code we have written or sometimes the warning <br>
points out future work that should be done and turning off the warning <br>
would hide that hint from us. As long as the warning is there it is <br>
"nagware" prompting us to continue thinking about the issue.<br>
<br>
<br>
I said above that sometimes we don't completely understand the code we <br>
have written, and that is true.<br>
<br>
For example I wrote a section of code that represents projection of a 2D <br>
set of points on to a line, and when I wrote it in accordance with all <br>
the standard formula it produced provably wrong answers. When I reversed <br>
the X and Y coordinate the result produced the expected answers. I <br>
consulted over half a dozen different references and the theoretical <br>
implementation is the right one but doesn't work. We have a vague notion <br>
as to why exchanging X and Y might work, but it would take more time to <br>
dig in to it and prove or disprove it than the matter is worth.<br>
<br>
There are a number of other places where we don't know _why_ our code <br>
works. We write experimental formula and test the formula against data; <br>
we don't always understand the math behind the formula. There are lots <br>
of different possible formula, and working out the theoretical math of <br>
each of them could take decades *each*. That isn't meant to be an <br>
exaggeration either: there are some formula in the field we are working <br>
in that have been used since the 1960's and their implications are still <br>
not fully understood. So we do a "Darwinian selection": we try formula <br>
and only go deeper into the theory of the ones that work well for us (or <br>
for the ones that really look like they should do well but turn out not <br>
to: a good explanation for why something seemingly obvious fails can <br>
provide very useful insights into what has to be taken into account by <br>
something that works.)<br>
<br>
If this all sounds sloppy... it's because we do applied *research*, and <br>
research _commonly_ has a 90% failure rate for ideas, and only a 1% or <br>
2% real success rate (with the other 89% being academically interesting <br>
but not good enough to be worth pursuing for applied research.)

Tue, 22 Jun 2010 15:38:22 +0000
Re: check syntax errors in m file
http://www.mathworks.com/matlabcentral/newsreader/view_thread/255839#756568
Mason Freed
Walter Roberson <roberson@hushmail.com> wrote in message <3n_Tn.2420$Yo5.136@newsfe01.iad>...<br>
> That policy will just lead to people routinely turning off "all <br>
> instances in file" of warnings without examination. The medicine will be <br>
> worse than the problem.<br>
<br>
That, as you point out, would be bad programming practice too. I would discourage that as much as I would discourage having warnings left over.<br>
<br>
> We turn off warnings only when we completely understand their causes and <br>
> know that the existing code is appropriate. Sometimes we don't <br>
> completely understand the code we have written or sometimes the warning <br>
> points out future work that should be done and turning off the warning <br>
> would hide that hint from us. As long as the warning is there it is <br>
> "nagware" prompting us to continue thinking about the issue.<br>
<br>
See <a href="http://blogs.mathworks.com/desktop/2008/03/17/whatsonmytodolist">http://blogs.mathworks.com/desktop/2008/03/17/whatsonmytodolist</a> for an alternative (perhaps better) method for keeping track of hacks and todo items.<br>
<br>
As I said in the beginning, this is my opinion on how to do things. There are infinitely many others, and the best one likely depends on the particulars of each situation. To each, his own.<br>
<br>
Thanks,<br>
Mason

Tue, 22 Jun 2010 19:12:28 +0000
Re: check syntax errors in m file
http://www.mathworks.com/matlabcentral/newsreader/view_thread/255839#756644
Steven Lord
<br>
"Mason Freed" <mfreedREMOVETHIS@mfreedREMOVETHIS.com> wrote in message <br>
news:hvo7qt$2bh$1@fred.mathworks.com...<br>
> "Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <br>
> <hvo77o$naj$1@fred.mathworks.com>...<br>
><br>
>> No magic suggestions for the simple reason that Matlab mfiles should not <br>
>> have a space in their filenames... If you're doing checkin sanity checks <br>
>> as you wrote above then this would be one obvious source of reported <br>
>> error.<br>
>><br>
>> Yair Altman <a href="http://UndocumentedMatlab.com">http://UndocumentedMatlab.com</a><br>
><br>
> Thanks. I was under the impression originally that the issue occurred on <br>
> PATHS with spaces in the name (e.g. "C:\Documents and settings") but <br>
> you're exactly right. The only issue is with file NAMES that contain <br>
> spaces. So no issue at all (and a good addition to the check!).<br>
<br>
For that check, you will want to use ISVARNAME. That will detect if the <br>
name of your script or function file violates any of these rules:<br>
<br>
1) Names must be NAMELENGTHMAX or fewer characters<br>
2) Names must start with a letter<br>
3) Names must contain only letters, numbers, and underscore<br>
4) Names must not be a keyword (see ISKEYWORD)<br>
<br>
 <br>
Steve Lord<br>
slord@mathworks.com<br>
comp.softsys.matlab (CSSM) FAQ: <a href="http://matlabwiki.mathworks.com/MATLAB_FAQ">http://matlabwiki.mathworks.com/MATLAB_FAQ</a><br>
To contact Technical Support use the Contact Us link on <br>
<a href="http://www.mathworks.com">http://www.mathworks.com</a>

Tue, 22 Jun 2010 19:21:27 +0000
Re: check syntax errors in m file
http://www.mathworks.com/matlabcentral/newsreader/view_thread/255839#756650
dpb
Steven Lord wrote:<br>
> "Mason Freed" <mfreedREMOVETHIS@mfreedREMOVETHIS.com> wrote in message <br>
> news:hvo7qt$2bh$1@fred.mathworks.com...<br>
...<br>
<br>
>> Thanks. I was under the impression originally that the issue occurred on <br>
>> PATHS with spaces in the name (e.g. "C:\Documents and settings") but <br>
>> you're exactly right. The only issue is with file NAMES that contain <br>
>> spaces. So no issue at all (and a good addition to the check!).<br>
> <br>
> For that check, you will want to use ISVARNAME. That will detect if the <br>
> name of your script or function file violates any of these rules:<br>
> <br>
> 1) Names must be NAMELENGTHMAX or fewer characters<br>
> 2) Names must start with a letter<br>
> 3) Names must contain only letters, numbers, and underscore<br>
> 4) Names must not be a keyword (see ISKEYWORD)<br>
<br>
Leaving only the issue of possible name collisions throughout a large <br>
project, maybe...?<br>
<br>


Wed, 23 Jun 2010 09:33:04 +0000
Re: check syntax errors in m file
http://www.mathworks.com/matlabcentral/newsreader/view_thread/255839#756791
Yair Altman
> > > Any updates on this question?? There MUST be some way to distinguish <br>
> > > warnings from syntax errors using the mlint() function. The information is <br>
> > > obviously there  the editor shows them in different colors.<br>
> > ><br>
> > > Anybody?<br>
> > <br>
> > What do you intend to do with this information? Do you want simply to check <br>
> > if the function _can_ run without actually running it? Or do you want to <br>
> > get a list of errors so that you can tell someone specifically what <br>
> > lines/sections to fix?<br>
<br>
> My solution was to use the undocumented internal function mlintmex, as follows:<br>
> <br>
> errMsgs = mlintmex('m2', srcFileName);<br>
> allMsgs = mlintmex('m0', srcFileName);<br>
> <br>
> numErrors = length(strfind(regexprep(errMsgs,'\*\*\*.*',''),char(10)));<br>
> numAllMsg = length(strfind(regexprep(allMsgs,'\*\*\*.*',''),char(10)));<br>
> numWarns = numAllMsg  numErrors;<br>
> <br>
> (and from the messages themselves I extracted the actual error/warning location)<br>
<br>
Apparently, the builtin mlint function has an undocumented feature of accepting any mlintmex input argument. After all, all mlint's args are passed to mlintmex that actually does all the lint processing. This undocumented feature is even documented in an internal comment within the mlint.m file, although the actual extra args are not themselves documented.<br>
<br>
So, in summary, you could simply do:<br>
errMsgs = mlint('m2',srcFileNames); % m2 = errors only<br>
m1Msgs = mlint('m1',srcFileNames); % m1 = errors and severe warnings only<br>
allMsgs = mlint('m0',srcFileNames); % m0 = all errors and warnings<br>
<br>
Note that mlint returns the data in struct format, while mlintmex returns a string that you must parse.<br>
<br>
Yair Altman <br>
<a href="http://UndocumentedMatlab.com">http://UndocumentedMatlab.com</a>

Wed, 23 Jun 2010 13:58:04 +0000
Re: check syntax errors in m file
http://www.mathworks.com/matlabcentral/newsreader/view_thread/255839#756892
us
"Yair Altman" <altmanyDEL@gmailDEL.comDEL> wrote in message <hvo3ak$aeb$1@fred.mathworks.com>...<br>
> "Mason Freed" wrote in message ...<br>
> > First, thanks Yair. Your suggestion works perfectly, in a handful of lines. How do you find all of your nifty undocumented stuff?<br>
> <br>
> There are two main sources: reading/searching this newsgroup and reading the internal code of Matlab's mfiles. I make a habit of noting down new stuff which is undocumented  then when the need occurs (often years later), I have an immediate answer. <br>
> <br>
> Here's another thread that documents mlintmex usage that you may find useful, which I posted a few years ago: <a href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/145245">http://www.mathworks.com/matlabcentral/newsreader/view_thread/145245</a><br>
> <br>
> In this particular case, I think I learned about mlintmex from the indomitable Us (Urs) Schwartz, but I can't find the original reference now. He also posted the related utilities FDEP and FARG on the File Exchange that you may find useful (as are all of his utilities). In any case, Us posted the following humorous comment about the source of *his* knowledge some years ago: <a href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/115423#292260">http://www.mathworks.com/matlabcentral/newsreader/view_thread/115423#292260</a> <br>
> <br>
> Yair Altman <br>
> <a href="http://UndocumentedMatlab.com">http://UndocumentedMatlab.com</a> <br>
<br>
just a note<br>
the file below (please, use the ...view original format... to copy/paste!) runs through all currently known MLINT/MLINTMEX options (2010a) and collects each option in an output struct with respective fieldnames...<br>
additional notes:<br>
 some options (eg, yacc, ud) will take some time(!!!!!)...<br>
 the copy may have wrapped the syntax falsly...<br>
<br>
% example<br>
r=doli('unique');<br>
[r.call.message]<br>
<br>
us<br>
<<<<< FILE >>>>><br>
<br>
%DOLI MLINT a file with various options<br>
%<br>
%SYNTAX<br>
%<br>
% R = DOLI(FNAM);<br>
% use MLINT<br>
%<br>
% R = DOLI(FNAM,1);<br>
% use MLINTMEX<br>
%<br>
%OUTPUT<br>
%<br>
% R : output struct with fieldnames == MLINT option<br>
% and content of output<br>
% retrieve messages, eg,<br>
% [R.calls.message]<br>
%<br>
% other help removed for posting<br>
<br>
%<br>
function r=doli(varargin)<br>
<br>
% created:<br>
% us 12Feb2001 us@neurol.unizh.ch<br>
% modified:<br>
% us 23Jun2010 15:43:22<br>
%<br>
% localid: us@USZwsnos36362x86Windows XP7.10.0.499.R2010a<br>
<br>
opt={<br>
% option remarks<br>
% <br>
'all'<br>
'allmsg'<br>
'amb'<br>
'body'<br>
'callops'<br>
'calls'<br>
'com'<br>
'cyc'<br>
% 'db' % == set + ud + tab<br>
'dty'<br>
'edit'<br>
'en' % messages in english<br>
'id'<br>
'ja' % messages in japanese<br>
'lex'<br>
'm0' % + other opt<br>
'm1' % + other opt<br>
'm2' % + other opt<br>
'm3' % + other opt<br>
'mess'<br>
'msg'<br>
'notok'<br>
'pf'<br>
'set'<br>
'spmd'<br>
'stmt'<br>
'tab'<br>
'tmtree'<br>
'tmw' % not valid anymore<br>
'toks'<br>
'tree'<br>
'ty'<br>
'ud'<br>
'yacc' % ONLY: !mlint FILE yacc ...<br>
<br>
% default behavior of DOLI<br>
% 'struct' % output > struct<br>
};<br>
<br>
r.o=opt;<br>
if ~nargin<br>
help(mfilename);<br>
return;<br>
end<br>
if nargin > 1<br>
eng=@mlint;<br>
else<br>
eng=@mlintmex;<br>
end<br>
fnam=which(varargin{1});<br>
if isempty(fnam)<br>
disp(sprintf('DOLI> file not found %s',varargin{1}));<br>
return;<br>
end<br>
<br>
fmt=max(cellfun('length',opt(:,1)));<br>
fmt=sprintf('%%6s> %%%d.%ds %%d',fmt,fmt);<br>
<br>
disp(sprintf('%s > %s','MLINT',fnam))<br>
for i=1:size(opt,1);<br>
copt=opt{i,1};<br>
sopt=copt(2:end);<br>
if strcmp(sopt,'yacc')<br>
[tmp,tmp]=system(sprintf('mlint %s yacc m3',fnam)); %#ok<br>
else<br>
tmp=eng(fnam,copt,'struct');<br>
end<br>
if iscell(tmp)<br>
tmp=tmp{:};<br>
end<br>
disp(sprintf(fmt,'OPTION',copt,numel(tmp)));<br>
r.(sopt)=tmp;<br>
end<br>
end<br>
%

Wed, 23 Jun 2010 14:55:21 +0000
Re: check syntax errors in m file
http://www.mathworks.com/matlabcentral/newsreader/view_thread/255839#756924
us
"us " <br>
> % example<br>
> r=doli('unique');<br>
> [r.call.message]<br>
<br>
must read<br>
<br>
[r.calls.message]<br>
<br>
sorry for confusion<br>
us

Wed, 23 Jun 2010 15:34:04 +0000
Re: check syntax errors in m file
http://www.mathworks.com/matlabcentral/newsreader/view_thread/255839#756940
Mason Freed
Thanks Yair and us  both interesting additions. I'll start using mlint with the m2 option directly  nicer output format.<br>
<br>
There's a lot of information in the doli() output. Interesting. (Not too much use for me right now, but I'll keep it in the back of my head for later projects...)<br>
<br>
Thanks,<br>
Mason

Wed, 23 Jun 2010 16:02:21 +0000
Re: check syntax errors in m file
http://www.mathworks.com/matlabcentral/newsreader/view_thread/255839#756956
Mason Freed
"Steven Lord" <slord@mathworks.com> wrote in message <hvr1us$n9v$1@fred.mathworks.com>...<br>
> <br>
> For that check, you will want to use ISVARNAME. That will detect if the <br>
> name of your script or function file violates any of these rules:<br>
> <br>
> 1) Names must be NAMELENGTHMAX or fewer characters<br>
> 2) Names must start with a letter<br>
> 3) Names must contain only letters, numbers, and underscore<br>
> 4) Names must not be a keyword (see ISKEYWORD)<br>
<br>
Great tip  thanks Steve.

Wed, 23 Jun 2010 17:12:05 +0000
Re: check syntax errors in m file
http://www.mathworks.com/matlabcentral/newsreader/view_thread/255839#756988
Mason Freed
"Mason Freed" <mfreedREMOVETHIS@mfreedREMOVETHIS.com> wrote in message <hvtb6d$lqn$1@fred.mathworks.com>...<br>
> "Steven Lord" <slord@mathworks.com> wrote in message <hvr1us$n9v$1@fred.mathworks.com>...<br>
> > <br>
> > For that check, you will want to use ISVARNAME. That will detect if the <br>
> > name of your script or function file violates any of these rules:<br>
> > <br>
> > 1) Names must be NAMELENGTHMAX or fewer characters<br>
> > 2) Names must start with a letter<br>
> > 3) Names must contain only letters, numbers, and underscore<br>
> > 4) Names must not be a keyword (see ISKEYWORD)<br>
> <br>
> Great tip  thanks Steve.<br>
<br>
Actually, I found a flaw in this idea: isvarname('end') is 0. However, in a class folder, end.m is a valid overload for the end function.<br>
<br>
I think I'm just going to implement #1#3 above, and leave out #4.<br>
<br>
Thanks,<br>
Mason