Matlab and Visual Sudio
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
James Tursa
2013-3-27
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 个评论
James Tursa
2013-3-27
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]; }
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operators and Elementary Operations 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!