Matlab and Visual Sudio
2 views (last 30 days)
Show older comments
Can anyone suggest me why this code is not able to do the plotting?
#include <engine.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#pragma comment ( lib, "libmat.lib")
#pragma comment ( lib, "libmx.lib")
#pragma comment ( lib, "libmex.lib")
#pragma comment ( lib, "libeng.lib")
#define EBRANGE 12
int main () {
int i, j;
mxArray *DiscreteRangeOfEB = NULL;
DiscreteRangeOfEB = mxCreateDoubleMatrix(1, EBRANGE, mxREAL);
int *RangePoints = (int *)calloc(EBRANGE, sizeof(int));
for(i = 0; i < EBRANGE; i++){
RangePoints[i] = i;
}
for(i = 0; i < EBRANGE; i++){
printf("RangePoints[%d] = %d.\n", i, RangePoints[i]);
}
memcpy((void *) mxGetPr(DiscreteRangeOfEB), (void *) RangePoints, EBRANGE*sizeof(int));
float MyArray[EBRANGE];
for(j=0; j < EBRANGE; j++)
MyArray[j] = (float)(j);
mxArray *EBDistributionValue = NULL;
EBDistributionValue = mxCreateDoubleMatrix(1, EBRANGE, mxREAL);
memcpy((void *) mxGetPr(EBDistributionValue), (void *) MyArray, EBRANGE*1*sizeof(float));
Engine *MyEngine;
MyEngine = engOpen("null");
engPutVariable(MyEngine, "RangePoints", DiscreteRangeOfEB);
engPutVariable(MyEngine, "RangeValues", EBDistributionValue);
engEvalString(MyEngine, "plot(RangePoints, RangeValues)");
mxDestroyArray(DiscreteRangeOfEB);
mxDestroyArray(EBDistributionValue);
engClose(MyEngine);
system("pause");
return 0;
}
0 Comments
Answers (1)
James Tursa
on 27 Mar 2013
Haven't looked at the code in detail, but for sure you don't want to close the engine before you do the pause because that will wipe out the plot (the plot is tied to the running engine). I would swap the order of those lines as a first try:
system("pause");
engClose(MyEngine);
In fact, it is not necessary at all that you close the engine prior to your program exiting so you could get rid of these two lines entirely. Your program will exit, but the engine will still be running and your plot will still be visible on the screen. You will simply have to close the engine manually later on.
2 Comments
James Tursa
on 27 Mar 2013
These two lines are in error:
memcpy((void *) mxGetPr(DiscreteRangeOfEB), (void *) RangePoints, EBRANGE*sizeof(int));
memcpy((void *) mxGetPr(EBDistributionValue), (void *) MyArray, EBRANGE*1*sizeof(float));
You can't just copy memory from one array to another unless the underlying data is the same type. E.g., you can copy a double array (8-byte floating point) to a double array using memcpy, but you CAN'T copy an int array (4-byte integer) or a float array (4-byte floating point) to a double array this way. To copy these data you need to do it in a loop so the compiler can do the type conversion. E.g.,
double *dp;
:
dp = mxGetPr(DiscreteRangeOfEB);
for(i = 0; i < EBRANGE; i++){ dp[i] = RangePoints[i]; }
dp = mxGetPr(EBDistributionValue);
for(i = 0; i < EBRANGE; i++){ dp[i] = MyArray[i]; }
See Also
Categories
Find more on Operators and Elementary Operations 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!