Coder deleting variable definitions

Why oh why does coder feel the need to delete my variable definitions from time to time? It is unbelievably frustrating having to redefine 200+ variables.

13 Comments

Adam, as a work-around, could you perhaps put the variable definitions into a different file and #include that file ?
So create a file that is full of stuff like:
double Var1;
int Var2;
And select that as a test file to have all of the variables autodefined?
Write the definitions into YourFileName.h. Then at the point in the C code that you need the variables defined, you would put the line
#include "YourFileName.h"
This has the effect of inserting everything from YourFileName.h as-if it had all been typed in at that point in the source.
Note: the double-quote marks are important for the syntax.
I see what you are saying. I understand how to do that in the C language. I was more referring to the Overview tab in MATLAB Coder where it asks for all of my entry-point files.
Adam: Are these variables not being used in the rest of your code? I would expect the variable definitions to be removed only as an optimization.
Maybe I'm not understanding how to use coder properly. If I want a file to be converted to C, I'd put it in my entry-point files section. Let's say the file is called S1.m. In S1.m I have two variables, zeta and w, that need to be defined before the function will compile. One is a double(1 x 1) and the other is a double(1 x 200). Once I do that, assuming I don't have any other errors within my code, it will compile. But from time to time, those definitions I have in place for zeta and w will disappear.
This would not be an entry-point file.
Are the variable zeta and w being used in some part of S1.m?
Yes they are. If I don't put them as a field in the entry-point file, the code will not be generated
I also believe I figured out how the definitions are getting deleted. When I highlight more than one entry-point file (primarily on accident), that's when it occurs. I don't know how to make it happen but I know every time it happens, I have at least 2 files highlighted.
Adam: I think I'm a little confused about your workflow. Perhaps you should consider sending your files and reproduction steps to MathWorks Tech Support and verify if this is expected behavior or a bug?
I will send this to them. I think it is a bug. I figured out how to recreate the problem 100% of the time though. You can test this yourself and let me know if your results vary. Create a coder project, and on the overview tab, when you add an entry-point file, make sure you define at least one field. Then left click on the entry-point files name and hold it just longer than a click. If you drag that file even the slightest, your field definitions will be erased.

Sign in to comment.

 Accepted Answer

Fred Smith
Fred Smith on 26 Jun 2012
Hi Adam,
Sorry to hear about your frustration. If you have 200 variables you might want to consider a different approach.
Have you looked at using codegen from the command-line instead of the UI? You can use "pre-conditioning" to incorporate your type definitions directly into your MATLAB code. This mechanism is verbose but solid. In many cases you can auto-generate the pre-conditions using some helper scripts. This feature is also available through the UI as of R2012a although it is somewhat hidden in the Settings dialog.
Another possibility in the UI, is to define your inputs by example. If you have appropriates variables in the base workspace, MATLAB coder can automatically figure out the types from those.
Hope this helps,
Fred

1 Comment

Thanks Fred. I never ended up getting it reported (ironically the bug report page bugged out...) but thankfully I got everything I needed from Coder and no longer need to use it. I will look at using codegen in the future as it sounds to be a much more painless process. Thanks again!

Sign in to comment.

More Answers (1)

Rich McKeever
Rich McKeever on 26 Jun 2012
Adam,
We saw your post about the issue here and fixed the bug yesterday. A patch is available here, but I also second Fred's suggestion of using other methods for defining large numbers of inputs.
Thanks,
Rich

Products

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!