MATLAB: Dealing with lengthy expressions

expression

I am trying to estimate parameters for a custom distribution having two parameters and 3 variables. I encounter very lengthy expressions during my coding and I need to copy them from one file to another, but am unable to do so. It exceeds the command window display limit and the variable editor is blank when I open the symbolic expression. Kindly help. Can anybody suggest an alternative command for parameter estimation also? Thanks

Best Answer

If they are symbolic expressions, it might make sense to use matlabFunction() to convert them from symbolic code to MATLAB; you would use the option to write to a file. You might also want to experiment with the optimization option.
Alternately,
fid = fopen('NewFile.m', 'wt');
fwrite(fid, char(TheSymbolicExpression));
fprintf(fid, '\n');
fclose(fid);
This will not, however, automatically wrap the expression.
Here is a way that might help:
newstr = regexprep( char(TheSymbolicExpression), '([+*/-])', ' $1 ');
(Note: it is required that the - character be the last thing in the [] if the dash is there at all.)
The newstr that is created will have whitespace where the original would have strung everything together. That whitespace may help if you have an editor with an auto-format function.