Sender: on the Sand of the Sahara Desert, the letter area: matlab
Title: M files turn C / C , "impossible" change "possible"
Sending station: BBS Harbin Institute of Technology (Sun Aug 1 15:42:01 2004)
M files turn C / C , "impossible" change "possible"
When you use the "MCC -B SGLCPP file name" successfully compile one of the programs as a C language, is it
I feel very excited, think it is very good? Let me give you a pot of cold water. Try the following procedure:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Function fork4 ()
I = IMREAD ('lena.tif');
IMSHOW (i)
BW = roion;% This function allows you to select a polygon on the image
IMSHOW (BW)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Compile, success with MCC -B SGLCPP fork4. (Although the compiler gives a lot of warnings, executable
In the end, it is still generated) Let's run the automatically generated executable to try ... Ah, you can't choose, out
Now the following error:
Exception! File: Handler.cpp, Line: 73
Output Argument 'a' Was Not Assigned During Call To 'Xlim'.
This way is not available? !
Roipoly is a function that is often used. If you have a program that has its programs, it is very
Let us engage in image processing. Can you think about it to work? Try a try: from the wrong prompt
The problem is likely to be in the xlim function, and we search for the Matlab installation path. Found it in "c: / matlab6p5toolbox / matlab / graph3d", I put it on the key code:
......................................
IF Nargin == 0
A = GET (GCA, 'XLIM');
Else
if Length (Arg1) == 1 & IsHandle (Arg1) & strcmp (GET (Arg1, 'Type'), 'Axes'
AX = arg1;
IF Nargin == 2
Val = arg2;
Else
A = GET (AX, 'XLIM');
Return
end
Else
IF Nargin == 2
Error ('Wrong Number of Arguments')
Else
AX = GCA;
Val = arg1;
end
end
IF ISSTR (VAL)
IF (strCMP (Val, 'Mode'))
A = GET (AX, 'XLIMMODE');
Else
Set (AX, 'XLIMMODE', VAL);
end
Else
Set (AX, 'XLIM', VAL);
end
end
......................................
The error message is "a (return value) is not assigned". Then we do this: copy Xlim.m
Put it into our work directory (this step is very important, because below we want to modify the XLim.m program, if you modify directly in the source file will affect all the functions based on this function, put it in our working directory. Will only affect
To the current program), change a paragraph in XLim.m as follows:
.......................
IF ISSTR (VAL)
IF (strCMP (Val, 'Mode'))
A = GET (AX, 'XLIMMODE');
Else
Set (AX, 'XLIMMODE', VAL);
DISP ('Here A')
end
Else
Set (AX, 'XLIM', VAL);
DISP ('Here B')
end
.......................
The purpose of this is to detect A. If you are not assigned, you will return it. Running fork4 discovery command in matlab
Only 'Here A' is displayed in the line, which means that the flow through this position during the operation of Fork4, but A is not assigned. in order to
Make the compiled program to run, I assigned to A in this point, so the code is changed:
.......................
IF ISSTR (VAL)
IF (strCMP (Val, 'Mode'))
A = GET (AX, 'XLIMMODE');
Else
Set (AX, 'XLIMMODE', VAL);
a = 1;
end
Else
Set (AX, 'XLIM', VAL);
end
.......................
Compile, running you will find that there is no problem in xlim.m, but there is a problem in Ylim.m, so it is imperative before.
Put Ylim.m (must remember not to modify the file in the original location) Corresponding code modification:
.......................
IF ISSTR (VAL)
IF (strCMP (Val, 'Mode'))
A = GET (AX, 'Ylimmode');
Else
Set (AX, 'Ylimmode', VAL);
A = 1;
end
Else
Set (AX, 'Ylim', VAL);
end
.......................
Compile, run, ok! ! !
Ok, anything involving the RoIPoly function can be done.
Finally, I want to explain why this is feasible.
"A is not assigned, it is returned". "It is not necessary to assign a value in this case, so it simply does not assign
It is returned, which does not have problems in Matlab, but it may be used for some special reasons after turning into C language.
The number must be assigned to the return value in the function body, so there is a runtime error. This error is not logical or number
The value on the mistake, but only violates some "formal specifications", so our artificial giving a value to a value to comply with this specification. What is the value? As mentioned earlier, since A is not necessary to assign a value, it doesn't matter if you give it. So just give it a value.
Many tasks of "M files to C / C files, VC compilation" in the seemingly "mission imposible" are actually
You can implement it by modifying Matlab's M files, in another article "Analysis: Solve the M program into CPP programs
Another example of the compilation problem. Interested in referring, but "fish" is not important, "fishing" is important.