Note: This discussion is about an older version of the COMSOL Multiphysics® software. The information provided may be out of date.

Discussion Closed This discussion was created more than 6 months ago and has been closed. To start a new discussion with a link back to this one, click here.

External matlab function -matlab Path and ElemStruct problems

Please login with a confirmed email address before reporting spam

Hi everyone,

I’m building an external matlab function and following the guidance of the document. But the function fails to work. The matlab function is same as what the document ‘LiveLink For MATLAB Users' Guide’ (page 42) says:

function c=func1(x)
c=(x./1.6).*(x>0.6)+(x.^2+0.3).*(x<=0.6);

The .m file is save as ‘func1.m’ as the attachment shown. Then in COMSOL, I build/add a matlab node under the Global Definitions (Global Definitions – Functions –Matlab), then specify the function/argument, and finally the plot parameters. But, when I use “plot” to plot the function, it doesn’t work.


Fig. 1 Operating flow
(all the pictures and text are illustrated in the .doc file, thanks. )


Question one:
About the matlab and COMSOL path relative to the external matlab function

The matlab path:

Fig. 2 the path of matlab
(Ps. I didn’t set the path to “E:\papers\paper COMSOL”)

1. When I put the .m and .mph files to the “D:\COMSOL 4.2a\COMSOL42a”, then the error “failure loading matlab libraries, this model requires that matlab is available” appears.

Fig. 3 the loading error

2. When I put both files under the directory “E:\papers\paper COMSOL”

Fig. 4 the save path of both files
The error mentioned above disappears, while another error “internal error: cannot convert ElemStruct to int” comes,

Fig. 5 the ElemStruct error

The above phenomena puzzle me a lot, the matlab path can be change very easily and take effect immediately. Does during these change in matlab path, the COMSOL don’t responses correspondingly?

Question two:
How to solve the “internal error: cannot convert ElemStruct to int” problem

I have searched the forum and find Annika Rödder have encountered the same problem (see website www.comsol.com/community/forums/general/thread/23188/ for details). However, I haven’t understood the answer and underlying problem.

Could you give me some guidance?
Thanks for your response sincerely. Best wishes to you and your family.

FM Huang
Dec. 1st. 2011


4 Replies Last Post Dec 5, 2011, 7:50 p.m. EST

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Dec 1, 2011, 10:01 a.m. EST
Hi,


1. in the file `func1.m` the very first keyword `function` is misspelled. If corrected, the function can be plotted from inside of COMSOL.

2. If you put the m-file and the mph-file in the same directory, the m-file is automatically on the MATLAB path (just guessing, but probably this is because the mph-file directory is the current working directory of MATLAB). But I don't know, why it fails if you put all the files in the installation directory of COMSOL - but then again, why would you want to do that?

3. Concerning the “internal error: cannot convert ElemStruct to int” error: when exactly does this problem occur? If you try to run the model? If you try to plot it? And is it really the exact same m-file as that one that you have attached to your post that causes this error?



Hi, 1. in the file `func1.m` the very first keyword `function` is misspelled. If corrected, the function can be plotted from inside of COMSOL. 2. If you put the m-file and the mph-file in the same directory, the m-file is automatically on the MATLAB path (just guessing, but probably this is because the mph-file directory is the current working directory of MATLAB). But I don't know, why it fails if you put all the files in the installation directory of COMSOL - but then again, why would you want to do that? 3. Concerning the “internal error: cannot convert ElemStruct to int” error: when exactly does this problem occur? If you try to run the model? If you try to plot it? And is it really the exact same m-file as that one that you have attached to your post that causes this error?

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Dec 3, 2011, 9:14 p.m. EST
Firstly, thanks for your response.

1. I checked the the uploaded .m file. You're right, it's misspelled (I just loaded the wrong .m file).

2. Putting both in a same directory is ok except the current working directory, which puzzles me. While there is no necessary to put them in that directory.

3. The “internal error: cannot convert ElemStruct to int” error occurs when i plot the function. when i defined a external matlab function, i want to plot it only in COMSOL to check it works or not (or whether there is error) . A few people encountered the "internal error: cannot convert ElemStruct to int” error, (Alain Nguyen from www.comsol.com/community/forums/general/thread/7946/ & Annika Rödder form www.comsol.com/community/forums/general/thread/23188/). According to the guidance of Annika Rödder who solve the question by creating a new computer name with no special character. Hence i try it again, only to fail.
Could you give me some ideas. Thanks in advance.

FM Huang
Dec.4th.2011


Hi,


1. in the file `func1.m` the very first keyword `function` is misspelled. If corrected, the function can be plotted from inside of COMSOL.

2. If you put the m-file and the mph-file in the same directory, the m-file is automatically on the MATLAB path (just guessing, but probably this is because the mph-file directory is the current working directory of MATLAB). But I don't know, why it fails if you put all the files in the installation directory of COMSOL - but then again, why would you want to do that?

3. Concerning the “internal error: cannot convert ElemStruct to int” error: when exactly does this problem occur? If you try to run the model? If you try to plot it? And is it really the exact same m-file as that one that you have attached to your post that causes this error?


Firstly, thanks for your response. 1. I checked the the uploaded .m file. You're right, it's misspelled (I just loaded the wrong .m file). 2. Putting both in a same directory is ok except the current working directory, which puzzles me. While there is no necessary to put them in that directory. 3. The “internal error: cannot convert ElemStruct to int” error occurs when i plot the function. when i defined a external matlab function, i want to plot it only in COMSOL to check it works or not (or whether there is error) . A few people encountered the "internal error: cannot convert ElemStruct to int” error, (Alain Nguyen from http://www.comsol.com/community/forums/general/thread/7946/ & Annika Rödder form http://www.comsol.com/community/forums/general/thread/23188/). According to the guidance of Annika Rödder who solve the question by creating a new computer name with no special character. Hence i try it again, only to fail. Could you give me some ideas. Thanks in advance. FM Huang Dec.4th.2011 [QUOTE] Hi, 1. in the file `func1.m` the very first keyword `function` is misspelled. If corrected, the function can be plotted from inside of COMSOL. 2. If you put the m-file and the mph-file in the same directory, the m-file is automatically on the MATLAB path (just guessing, but probably this is because the mph-file directory is the current working directory of MATLAB). But I don't know, why it fails if you put all the files in the installation directory of COMSOL - but then again, why would you want to do that? 3. Concerning the “internal error: cannot convert ElemStruct to int” error: when exactly does this problem occur? If you try to run the model? If you try to plot it? And is it really the exact same m-file as that one that you have attached to your post that causes this error? [/QUOTE]

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Dec 5, 2011, 5:50 a.m. EST

3. The “internal error: cannot convert ElemStruct to int” error occurs when i plot the function. when i defined a external matlab function, i want to plot it only in COMSOL to check it works or not (or whether there is error) . A few people encountered the "internal error: cannot convert ElemStruct to int” error, (Alain Nguyen from www.comsol.com/community/forums/general/thread/7946/ & Annika Rödder form www.comsol.com/community/forums/general/thread/23188/). According to the guidance of Annika Rödder who solve the question by creating a new computer name with no special character. Hence i try it again, only to fail.
Could you give me some ideas. Thanks in advance.


Hm - I can reproduce that bug (on Linux, didn't test it on Windows) if I put the mph and the m-file in a directory that has special characters (in my case 'äöü') in the name - just as mentioned in the second thread linked by you.

So, are you sure that there are no special characters in the path to your m-file (I'm asking because you are talking about the computer name, not the file path to the m-file directory)?

[QUOTE] 3. The “internal error: cannot convert ElemStruct to int” error occurs when i plot the function. when i defined a external matlab function, i want to plot it only in COMSOL to check it works or not (or whether there is error) . A few people encountered the "internal error: cannot convert ElemStruct to int” error, (Alain Nguyen from http://www.comsol.com/community/forums/general/thread/7946/ & Annika Rödder form http://www.comsol.com/community/forums/general/thread/23188/). According to the guidance of Annika Rödder who solve the question by creating a new computer name with no special character. Hence i try it again, only to fail. Could you give me some ideas. Thanks in advance. [/QUOTE] Hm - I can reproduce that bug (on Linux, didn't test it on Windows) if I put the mph and the m-file in a directory that has special characters (in my case 'äöü') in the name - just as mentioned in the second thread linked by you. So, are you sure that there are no special characters in the path to your m-file (I'm asking because you are talking about the computer name, not the file path to the m-file directory)?

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Dec 5, 2011, 7:50 p.m. EST
Hi,Julian Mehne,

Thanks again for your response. I have solve the problem, though i don't understand the underlying reason. it's shame to admit that I didn't put the .m file into the the right directory of the matlab because of some reasons. This is the first mistake I have made. After repairing /reinstalling the software and changing the directory of matlab using "LiveLink for matlab" (because it will open the matlab) again, almost all work successfully.

Furthermore, the .mph can't be put on the computer desktop, even the .m file is right in the directory of matlab, "internal error: cannot convert ElemStruct to int” error will occur. according to my trail and test, other place where the .mph is put in ok on the condition that the .m file is in the directory of matlab. This is the second mistake I have made. I didn't know why the desktop don't work.

Thanks a lot for your help and tips.
Best wishes to you and your family.

FM Huang
Dec.6th.2011



3. The “internal error: cannot convert ElemStruct to int” error occurs when i plot the function. when i defined a external matlab function, i want to plot it only in COMSOL to check it works or not (or whether there is error) . A few people encountered the "internal error: cannot convert ElemStruct to int” error, (Alain Nguyen from www.comsol.com/community/forums/general/thread/7946/ & Annika Rödder form www.comsol.com/community/forums/general/thread/23188/). According to the guidance of Annika Rödder who solve the question by creating a new computer name with no special character. Hence i try it again, only to fail.
Could you give me some ideas. Thanks in advance.


Hm - I can reproduce that bug (on Linux, didn't test it on Windows) if I put the mph and the m-file in a directory that has special characters (in my case 'äöü') in the name - just as mentioned in the second thread linked by you.

So, are you sure that there are no special characters in the path to your m-file (I'm asking because you are talking about the computer name, not the file path to the m-file directory)?


Hi,Julian Mehne, Thanks again for your response. I have solve the problem, though i don't understand the underlying reason. it's shame to admit that I didn't put the .m file into the the right directory of the matlab because of some reasons. This is the first mistake I have made. After repairing /reinstalling the software and changing the directory of matlab using "LiveLink for matlab" (because it will open the matlab) again, almost all work successfully. Furthermore, the .mph can't be put on the computer desktop, even the .m file is right in the directory of matlab, "internal error: cannot convert ElemStruct to int” error will occur. according to my trail and test, other place where the .mph is put in ok on the condition that the .m file is in the directory of matlab. This is the second mistake I have made. I didn't know why the desktop don't work. Thanks a lot for your help and tips. Best wishes to you and your family. FM Huang Dec.6th.2011 [QUOTE] [QUOTE] 3. The “internal error: cannot convert ElemStruct to int” error occurs when i plot the function. when i defined a external matlab function, i want to plot it only in COMSOL to check it works or not (or whether there is error) . A few people encountered the "internal error: cannot convert ElemStruct to int” error, (Alain Nguyen from http://www.comsol.com/community/forums/general/thread/7946/ & Annika Rödder form http://www.comsol.com/community/forums/general/thread/23188/). According to the guidance of Annika Rödder who solve the question by creating a new computer name with no special character. Hence i try it again, only to fail. Could you give me some ideas. Thanks in advance. [/QUOTE] Hm - I can reproduce that bug (on Linux, didn't test it on Windows) if I put the mph and the m-file in a directory that has special characters (in my case 'äöü') in the name - just as mentioned in the second thread linked by you. So, are you sure that there are no special characters in the path to your m-file (I'm asking because you are talking about the computer name, not the file path to the m-file directory)? [/QUOTE]

Note that while COMSOL employees may participate in the discussion forum, COMSOL® software users who are on-subscription should submit their questions via the Support Center for a more comprehensive response from the Technical Support team.