Any problem in using the Matlab coder?

2 visualizaciones (últimos 30 días)
laoya
laoya el 31 de Mayo de 2013
Dear administrator,
I am testing the Matlab coder by the sample matlab code:
function mytest=test
mytest=zeros(10,1);
mytest=1;
The generated code is as follows:
/*
* test.c
*
* Code generation for function 'test'
*
* C source code generated on: Fri May 31 10:26:55 2013
*
*/
/* Include files */
#include "rt_nonfinite.h"
#include "findpeakcore.h"
#include "fpeak.h"
#include "test.h"
/* Function Definitions */
void test(real_T mytest_data[1], int32_T mytest_size[1])
{
mytest_size[0] = 1;
mytest_data[0] = 1.0;
}
/* End of code generation (test.c) */
Appearently the code is not correct. However, after I changed the matlab code as follows:
function mytest=test
mytest=zeros(10,1);
mytest(1:10)=1;
The generated C code is correct:
/*
* test.c
*
* Code generation for function 'test'
*
* C source code generated on: Fri May 31 10:45:26 2013
*
*/
/* Include files */
#include "rt_nonfinite.h"
#include "findpeakcore.h"
#include "fpeak.h"
#include "test.h"
/* Function Definitions */
void test(real_T mytest[10])
{
int32_T i;
for (i = 0; i < 10; i++) {
mytest[i] = 1.0;
}
}
/* End of code generation (test.c) */
Is there any problem in using the Matlab coder?
Thanks,
Laoya Tang

Respuestas (3)

Walter Roberson
Walter Roberson el 31 de Mayo de 2013
The code generated for the original function looks correct to me. Your code statement
mytest = 1;
replaces all of "mytest" with the scalar 1.0, and the generated code constructs a scalar with value 1.0
The code for the second function replaces all elements of the vector "mytest" to make each of them 1.0. And that is reflected in the code, which generates the equivalent of ones(1,10)
Note that both of these are different than what you would get if your assignment had been
mytest(1) = 1.0;
When you use MATLAB Coder, once you have initialized a variable (giving it a type and a size) then you (I gather) can usually adjust its length as long as the new length does not exceed the first length. You initialize to the maximum length you might lose, and you chomp to the actual size used. Which is what the code in your first example does.

laoya
laoya el 31 de Mayo de 2013
Hi Walter,
Thank you very much for your quick and kindly reply. For the first case, I would like all 10 elements be assigned to 1. But the generated code only assigned the first element to 1. Am I right?
Thanks, Laoya
  2 comentarios
Walter Roberson
Walter Roberson el 31 de Mayo de 2013
The generated code for your first version makes the result be just the 1 x 1 scalar with content 1.0. It does that because that is what your code requests.
mytest = 1;
requests that all of mytest be thrown out and the 1 x 1 scalar 1.0 stored.
Your second version sets all of the elements to 1.0 . You could, though, rewrite it as
mytest(1:end) = 1;
or
mytest(:) = 1;
or you could skip both that line and the zeros() line and use
mytest = ones(1,10);
laoya
laoya el 31 de Mayo de 2013
Hi Walter,
Thanks for your kindly reply. What I mean is that since the matlab coder knows that I would set all elements 'mytest' to 1, why not the generated c code behave the same as what matlab code do? the mytest=1 is just a simple example, I just what to say that the code like mytest=1 couldn't generate c code that have exactly the same result as matlab code.
Thanks, Laoya

Iniciar sesión para comentar.


laoya
laoya el 31 de Mayo de 2013
Editada: Walter Roberson el 31 de Mayo de 2013
Dear all,
Another problem is that when I have the following function, the generated code will crash:
function foo
test=[];
for i=1:2
test=[test i];
end
How to cope with this kind of problem?
Thanks, Laoya
  6 comentarios
laoya
laoya el 31 de Mayo de 2013
Hi Fred,
Thanks for your kindly reply. The function 'foo' I posted is too simple so that the matlab coder didn't generate any code in this function. The matlab version I used is 2013a.
Thanks, Laoya
T
Walter Roberson
Walter Roberson el 1 de Jun. de 2013
with respect to MATLAB Coder (MATLAB interactive works a bit differently)
mytest = zeros(1,10);
means to establish an entry named "mytest" in the variable table, and to mark it as being of type double, and then to mark its current length as 10 and assign 0.0 to those 10 locations. I believe it does not keep a record of the maximum length (10), but I am not positive. It will use the initial size for the C declaration, but it might not track that size afterwards.
Then after that,
mytest = 1;
means to take that existing entry, check that "1" is convertible to the existing "double" of that entry (which of course it is as it is already double), and then to mark the current length as being 1 and set that 1 element to 1.0. At that point, as far as the Coder is concerned, the length is only 1, there is only 1 element, not 10.
If you want to set just the first element to 1, the code would be
test(1) = 1;
and that would only change the internal marking of the current size if 1 was greater than the current size.
Now,
mytest(1:10) = 1;
means to take that existing entry, check that "1" is convertible to the existing "double" of that entry (which of course it is as it is already double), and then to mark the current length as being 10 (unless it was already larger) and to set 10 elements to 1.0. I think that if the original size was less than 10, then the generated code would be allowed to write into whatever was in memory after the elements that were originally declared. Which would pretty likely create problems... eventually.
The code has two different purposes. The first bit of code changes the size to be length 1 and changes that; the second affects 10 elements. The generated code for them should be different.
If you want to code to change all the elements of test, no matter how many there are, then
test(:) = 1;
would be appropriate.
Please keep in mind that in MATLAB itself, when you assign a value to an entire variable (no subscript on the left side) then the new value replaces the existing variable as if you had clear'd the variable and then assigned the new value to it.
A = [1 2 3];
A = 'hello'
in MATLAB itself means that A is to be entirely replaced by the character vector 'h' 'e' 'l' 'l' 'o', and not that the elements of A should be overwritten with 'hello'
A = [1 2 3];
A(:) = 'hel';
would leave A as [104.0 101.0 108.0] rather than as 'hel' because discarding and replacing a variable means a different thing than writing over top of part of that variable.

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB Coder en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by