The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 Forum for Origin C
 not enough memory error
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

tjojojohn

USA
Posts

Posted - 11/14/2007 :  1:55:49 PM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Version (Select Help-->About Origin):
Operating System:XP

Hi all!

I am a newbie in Origin C coding. I was trying to read a mtrix and print the individual cell values of the same. (is there any existing function for this?) The matrix dimention that i try to test is 748 X 1121. I've been going though different threads in this forum and was able tp write the following code. It compiles and links properly. But when I run it, i get "not enough memoy for operation" error. I am not sure if i am going in the right direction...I use the evaluation version origin 8 and my laptop has 1GB Ram.The matrix has the gray scale values of a picture and they are integers. Any help/advise will be highly appreicated..

Thanks in advance!

Joe

void rtest()
{
int nRows, nCols;
MatrixLayer ml(Project.ActiveLayer());
Matrix mat(ml);
Matrixobject mo;
vector vr,vrows;
int ny;
if(ml)
{
mo = ml.MatrixObjects(0);
nRows = mo.GetNumRows();
nCols = mo.GetNumCols();
printf("rows %d cols %d \n",nRows,nCols);
for (int x=1;x<nRows+1;x++)
{
mat.GetRow(vr,x);
vrows = vr;
ny =vrows.GetSize();
for(int y=0;y<ny;y++)
{
printf("Val %d \n",vrows[y]);
}
}
}
else
{
printf("Invalid matrix\n");
return;
}
}

The output of the execution:

>>rtest
rows 748 cols 1121
not enough memory for operaion
#Command Error!

Mike Buess

USA
3037 Posts

Posted - 11/14/2007 :  10:49:04 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Joe,

1. Matrix is double by default and if the internal data type is integer you must declare it as such. That will get rid of the "not enough memoy for operation" error.
2. Next you'll see an "index out of range" error that flags the line mat.GetRow(vr,x). Origin indices start with zero so that line produces an error when x=nrows because there is no row with the index 'nrows'.

Other tips:
3. MatrixLayer is derived from Datasheet which has its own GetNumCols() and GetNumRows() so MatrixObject mo isn't necessary.
4. Vectors are also double by default so you should printf vrows[y] (if necessary) as shown below.

void rtest()
{
int nRows, nCols;
MatrixLayer ml(Project.ActiveLayer());
Matrix<int> mat(ml);
vector vr,vrows;
int ny;
if(ml)
{
nRows = ml.GetNumRows();
nCols = ml.GetNumCols();
printf("rows %d cols %d \n",nRows,nCols);
for(int x=0; x<nRows; x++)
{
mat.GetRow(vr,x);
vrows = vr;
ny = vrows.GetSize();
for(int y=0; y<ny; y++)
{
printf("Val %g \n",vrows[y]);
}
}
}
else
{
printf("Invalid matrix\n");
return;
}
}

Mike Buess
Origin WebRing Member
Go to Top of Page

joseph_king

China
Posts

Posted - 11/15/2007 :  12:35:36 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi tjojoJohn,

Thanks for reporting this.
And we have changed the error message as such "Invalid data element memory access. This is due to wrong internal data type declared for vector or martix. For example, you may need to use Matrix<int> instead of just Matrix if the corresponding Origin MatrixObject is int type instead of the default double type."

OriginLab Technical Services
Go to Top of Page

tjojojohn

USA
Posts

Posted - 11/16/2007 :  11:08:12 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Mike,

Thanks for your suggestions and tips. Looks like the matrix that I tried to test is an invalid one as it doesn't pass the IsValid()test. I converted the matrix to a workbook and again back to a matrix. And tried the function and it worked fine.

That gives a cool start to me. Thanks again!

Best regards,

Joe
Go to Top of Page

Mike Buess

USA
3037 Posts

Posted - 11/16/2007 :  5:46:38 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
quote:
I converted the matrix to a workbook and again back to a matrix.
That just changes the internal data type to double and you can accomplish the same thing with Matrix> Set Properties. However, declaring mat as Matrix<int> will work just as well.

Mike Buess
Origin WebRing Member
Go to Top of Page

tjojojohn

USA
Posts

Posted - 11/20/2007 :  5:00:48 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thanks Mike!

Best regards,

Joe
Go to Top of Page

tjojojohn

USA
Posts

Posted - 11/28/2007 :  1:43:25 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Mike,

I have a quick question. I have the following function to import a image in to a matrix. I am able to choose the file and import the image to the matrix ( as LONG internal datatype). Towards the end of the function, I need to convert the internal datatype to BYTE. I added another function - conv_byte for the same.

Now the problem is, when conv_byte is invoked, the image in the matrix is replaced with zeroes in all the cells. It also gives a datatype mismatch error. I tried changing the declaration of the matrix from Matrix<byte> image(ml); -> Matrix<long> image(ml); in the conv_byte. The error disappeared, but the resulting matrix is all with zeroes in it.

I only have manual way of doing it now - as Image -> Convert to data -> choose the byte as datatype.

Could you please advise on what I am doing wrong and suggest if there is any way to automate this conversion?

Thanks in advance.

Best regards,

Joe

void test_imp()
{

MatrixPage mp;
//Page pg(mp);

string filenam=filetest();
if (filenam != "")
{
mp.Create("Origin");
import_image(mp.GetName(),filenam );
MessageBox(GetWindow(), filenam, "Loaded image file");

//Call function to convert internal data type

conv_byte(); //problem!!!
}
else
{
string str;
str="No file selected!";
MessageBox(GetWindow(), str, "No file");
}

}

bool import_image(LPCSTR lpcszMatrix, LPCSTR lpcszFile)
{
Page pg(lpcszMatrix);
if( !pg || EXIST_MATRIX != pg.GetType() )
return false;
string strPage = lpcszMatrix;

printf("matrix %s file %s\n",lpcszMatrix, lpcszFile);
if( !lpcszFile )
return false;
using image = LabTalk.Image;
image.FileName$ = lpcszFile;
int iErr = image.import.matrix(strPage);
if( iErr )
{
printf("Import Error %d\n", iErr);
return false;
}
pg.LT_execute("matrix -ii"); // show matrix as image
return true;
}

string filetest()

{
string strFilename = GetOpenBox("*.bmp");
if(strFilename.IsEmpty()) // user cancel
return "";

string strPath = GetFilePath(strFilename);
string strName = GetFileName(strFilename);

string strSaveName = strPath+strName;

return strSaveName;
}

void conv_byte()
{
int ny;
atrixLayer ml = Project.ActiveLayer(); // Declare MatrixLayer object
Matrix<byte> image(ml); // Declare Matrix object
if (ml)
{
ny = ml.GetInternalDataType();
printf("data type %d \n",ny);
ml.SetInternalData(FSI_BYTE);
image.Attach(ml); // Attach object to Matrix image
}
}
Go to Top of Page

cpyang

USA
1406 Posts

Posted - 11/28/2007 :  5:38:14 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
I create a default Matrix and fill it with LabTalk

matrix -v x+y;

and run the following code


void conv_byte()
{
int ny;
MatrixLayer ml = Project.ActiveLayer();
if (ml)
{
ny = ml.GetInternalDataType();
printf("data type %d \n",ny);
ml.SetInternalData(FSI_BYTE);

Matrix<byte> image(ml);
printf("cell(1,2)=%d\n", image[1][2]);
}
}



and see no problem.


CP


Go to Top of Page

cpyang

USA
1406 Posts

Posted - 11/28/2007 :  6:04:42 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
It seems that if you are using Origin 8, the best would be to use the X-Functions that you can find from the menus. When you select any Image menu, for example, you can see on status bar the X-Function name. Then you can find out how to use it by

img2m -h

for example. The following LabTalk code might be the simplest way to load an image and convert it into a byte matrix. Origin C code can then be written to process this matrix.

 

dlgfile g:=*.bmp; // opens dialog to select a file and put into fname$
newbook m:=1; // create a new matrix book
impImage; // image into active matrix book using fname$
img2m o:=<input> t:=1;// convert image into byte data



CP


Go to Top of Page

tjojojohn

USA
Posts

Posted - 11/28/2007 :  6:40:55 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi CP,

Thanks for your response.

Function works well when I fill the matrix the way you mentioned. But if I import an image directly in to the matrix (File-> Import ->Image), the internal data type of the matrix becomes ULONG.
And when I run the function to convert to BYTE (conv_byte), the internal datatype becomes Byte, but the cell values become zero.

I am not sure if I am doing it in the right way. After importing image when I look at the data view (View -> Data mode), I see the cell values in 3 different sets of numbers. For example, value in cell(1,1) is 182 206 23. Possibly RBG values? When I imported the similar image in version 6 the internal data type by defualt was SHORT and I didn't have to convert datatype. But in version 8, since the data type is LONG and the default data is in RBG sets(?), I need to convert it to BYTE.

Please advise.

Best regards,

Joe
Go to Top of Page

tjojojohn

USA
Posts

Posted - 11/28/2007 :  8:20:55 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi CP,

Thats a very simple solution worked perfectly as per my need..Thanks a ton!

Best regards,

Joe
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000