Problem in loading a large script
Show older comments
Hi. I want to load a large script (67Mb) and I get a memory eror. What can I do to solve it? What is the limit about number of rows in script file? (I can run script with 16K rowes. Works slowly but work)
5 Comments
Rik
on 30 Oct 2017
Sounds a bit odd that this should fail, but apparently it does.
Do you really need the script to be in 1 file? Why not split functions over multiple files? To avoid a folder with a massive number of files, you can also use a function database for functions that are only a few lines like this:
function varargout = f_database(fid, varargin)
fh = str2func(fid);
[varargout{1:nargout}] = fh(varargin{:});
end
%example:
function value_out=add_two(value_in)
value_out=value_in+2;
end
You can call inside functions with this syntax:
val=f_database('add_two',10);
(and yes, I see the contradiction of suggesting splitting up functions and making a function database)
@ehud ikar: Why bother generating all of those variables and lines of code? It would be much simpler to write this using some loops, some indexing, and a handful of variables. This could be done using just a few lines of MATLAB code, without requiring a "code generator" or any of the bad practices that this code contains (numbered variables, copy-and-paste similar code, repetition without loops, etc.).
Answers (1)
A huge script, generated automatically, global variables, numbered variables - a lot of bad programming practices. It is not clear to me, what causes the "memory error" (see above: please post the error message instead of this lean description), but of course there is a limit, where Matlab cannot parse or run such files anymore. Obviously you hit the limit. Then either install more RAM, or much better: use clean programming techniques. The fact, that this code generator works for small problems sufficiently does not allow to draw conclusions about using it for large scale problems.
I'm convinced that there are much easier methods using loops and multiple functions. It is a better approach to use arrays instead of automatically created code. Better use one tool (code) and vary the contents of the data (variables), instead of creating a code for each problem. Your problem is another proof for this good programming practice.
3 Comments
ehud ikar
on 30 Oct 2017
"...in the opinion of my academic advisor..."
I have read many papers and articles, and the quality of code coming from academics is often quite poor. It would be a mistake to assume that someone is competent in writing code, just because they are an "academic advisor". Both undergrad and grad students would do themselves a big favor if they gained some outside perspectives on their code.
"I have a problem that it is impossible to solve it without a code generator"
That is a very strong statement. How did you prove this? What other methods have you investigated or tried? Why did you reject those methods? What other methods are you knowledgeable about?
I still don't see anything in your description that requires generating huge, complex code. Simple loops would seem to be enough to do what you explained, or some simple recursive code perhaps. And this would resolve the script-loading issues that you claim that you are facing.
Jan
on 30 Oct 2017
in my opinion and in the opinion of my academic advisor, there are no
unnecessary steps in this process
An experienced software engineer will not share this opinion. Automatic code generation is a very bad idea, if such huge scripts are created, which create a massive number of variables. It is not only the number of lines, but such programming technique let the local workspace and the lookup table for the variables explode.
Think twice: The description of your problem explains, that the used method fails. We tell you, that your method is known to cause such problems and it is deprecated in consequence. Now you answer, that is the opinion of you and your advisor, that the method is okay. No, as you see, the method fails.
Again: Please post a copy of the error message to let us know, what the problem is exactly.
And use a dynamic programming technique, which varies the data by using variables, instead of creating hard coded functions automatically. You are not the first programmer, who fails with this approach when the problem grows.
Categories
Find more on Programming in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!