Prev: plotregion
Next: how to send keyboard event
From: James Tursa on 28 Dec 2009 02:05 First, make sure you have the modules compiled (maybe you have already done this, but you didn't mention it). It is a one-time operation. e.g., mex -c MatlabAPImex.f mex -c MatlabAPImx.f mex -c MatlabAPImat.f Then be sure to include the associated object files in your mex command for readinput. e.g., mex('-f', options,'readinput.f90','MatlabAPImex.obj','MatlabAPImx.obj','MatlabAPImat.obj') Also, what is "wind"? I don't see it defined anywhere in your program. Are you trying to read a 3D variable named "wind" from the mat file, or what? James Tursa
From: James Tursa on 28 Dec 2009 07:17 "James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <hh9lbh$cp4$1(a)fred.mathworks.com>... > > First, make sure you have the modules compiled (maybe you have already done this, but you didn't mention it). It is a one-time operation. e.g., > > mex -c MatlabAPImex.f > mex -c MatlabAPImx.f > mex -c MatlabAPImat.f > > Then be sure to include the associated object files in your mex command for readinput. e.g., > > mex('-f', options,'readinput.f90','MatlabAPImex.obj','MatlabAPImx.obj','MatlabAPImat.obj') > > Also, what is "wind"? I don't see it defined anywhere in your program. Are you trying to read a 3D variable named "wind" from the mat file, or what? > > James Tursa I just noticed a couple of other things. 1) You are compiling an engine application but are using mex functions. i.e., You have a program but call the mexPrint function. You can't call any of the mex___ functions inside an engine application. 2) You have included "mex.h" in your Fortran program. You can't do that ... mex.h is strictly for C applications, not Fortran. Delete this line. Is there some reason why you are building a program instead of a mexFunction routine? If you used a mexFunction interface then passing the variable data would not need the mat file. Assuming you still want a program, try this: #include "fintrf.h" PROGRAM readinput use MatlabAPImx use MatlabAPImat character(len=63), pointer :: names(:) mwPointer mx, matfile integer*4 i, k real(8), pointer :: fp(:,:,:) !\ ! Open the mat file in read-only mode. !/ write(*,*) "... Opening mat file test.mat" matfile = matOpen("test.mat","r") if( matfile == 0 ) then write(*,*) "Unable to open mat file" stop endif !\ ! Get a list of variable names in the mat file. !/ write(*,*) "... List of variable names in the file:" names => fpMatGetNames(matfile) if( associated(names) ) then do i=1,size(names) write(*,*) names(i) enddo else write(*,*) "Unable to read the variable names" stop endif !\ ! Go get the variables and print out some info !/ write(*,*) "... Getting all the variables from the file" do i=1,size(names) mx = matGetVariable(matfile, names(i)) if( mx /= 0 ) then write(*,*) "Got the variable "//names(i) call mxDestroyArray(mx) else write(*,*) "Unable to get variable "//names(i) stop endif enddo call fpDeallocate(names) mx = matGetVariable(matfile, "wind") if( mx /= 0 ) then write(*,*) 'Got the variable "wind"' else write(*,*) 'Unable to get variable "wind"' stop endif fp => fpGetPr3(mx) if( .not.associated(fp) ) then call mxDestroyArray(mx) write(*,*) "Unable to associate pointer fp" stop endif write (*,*) 'Hello, world!' ! This is an inline comment write (*,*) , fp ! After you are done with the data, destroy the mxArray variable mx: call mxDestroyArray(mx) write(*,*) "... Done." k = matClose(matfile) END PROGRAM readinput James Tursa
From: HIEU on 28 Dec 2009 12:10 "James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <hh9lbh$cp4$1(a)fred.mathworks.com>... > > First, make sure you have the modules compiled (maybe you have already done this, but you didn't mention it). It is a one-time operation. e.g., > > mex -c MatlabAPImex.f > mex -c MatlabAPImx.f > mex -c MatlabAPImat.f > > Then be sure to include the associated object files in your mex command for readinput. e.g., > > mex('-f', options,'readinput.f90','MatlabAPImex.obj','MatlabAPImx.obj','MatlabAPImat.obj') > > Also, what is "wind"? I don't see it defined anywhere in your program. Are you trying to read a 3D variable named "wind" from the mat file, or what? > > James Tursa @James Tursa: yes, "wind" is a 3D variable from the mat file.
From: HIEU on 28 Dec 2009 12:21 "James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <hha7k0$hml$1(a)fred.mathworks.com>... > "James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <hh9lbh$cp4$1(a)fred.mathworks.com>... > > > > First, make sure you have the modules compiled (maybe you have already done this, but you didn't mention it). It is a one-time operation. e.g., > > > > mex -c MatlabAPImex.f > > mex -c MatlabAPImx.f > > mex -c MatlabAPImat.f > > > > Then be sure to include the associated object files in your mex command for readinput. e.g., > > > > mex('-f', options,'readinput.f90','MatlabAPImex.obj','MatlabAPImx.obj','MatlabAPImat.obj') > > > > Also, what is "wind"? I don't see it defined anywhere in your program. Are you trying to read a 3D variable named "wind" from the mat file, or what? > > > > James Tursa > > I just noticed a couple of other things. > > 1) You are compiling an engine application but are using mex functions. i.e., You have a program but call the mexPrint function. You can't call any of the mex___ functions inside an engine application. > > 2) You have included "mex.h" in your Fortran program. You can't do that ... mex.h is strictly for C applications, not Fortran. Delete this line. > > Is there some reason why you are building a program instead of a mexFunction routine? If you used a mexFunction interface then passing the variable data would not need the mat file. > > Assuming you still want a program, try this: > > #include "fintrf.h" > > PROGRAM readinput > use MatlabAPImx > use MatlabAPImat > > character(len=63), pointer :: names(:) > mwPointer mx, matfile > integer*4 i, k > real(8), pointer :: fp(:,:,:) > > !\ > ! Open the mat file in read-only mode. > !/ > write(*,*) "... Opening mat file test.mat" > matfile = matOpen("test.mat","r") > if( matfile == 0 ) then > write(*,*) "Unable to open mat file" > stop > endif > !\ > ! Get a list of variable names in the mat file. > !/ > write(*,*) "... List of variable names in the file:" > names => fpMatGetNames(matfile) > if( associated(names) ) then > do i=1,size(names) > write(*,*) names(i) > enddo > else > write(*,*) "Unable to read the variable names" > stop > endif > !\ > ! Go get the variables and print out some info > !/ > write(*,*) "... Getting all the variables from the file" > do i=1,size(names) > mx = matGetVariable(matfile, names(i)) > if( mx /= 0 ) then > write(*,*) "Got the variable "//names(i) > call mxDestroyArray(mx) > else > write(*,*) "Unable to get variable "//names(i) > stop > endif > enddo > call fpDeallocate(names) > > mx = matGetVariable(matfile, "wind") > if( mx /= 0 ) then > write(*,*) 'Got the variable "wind"' > else > write(*,*) 'Unable to get variable "wind"' > stop > endif > > fp => fpGetPr3(mx) > if( .not.associated(fp) ) then > call mxDestroyArray(mx) > write(*,*) "Unable to associate pointer fp" > stop > endif > > write (*,*) 'Hello, world!' ! This is an inline comment > write (*,*) , fp > > ! After you are done with the data, destroy the mxArray variable mx: > call mxDestroyArray(mx) > > write(*,*) "... Done." > k = matClose(matfile) > > END PROGRAM readinput > > > James Tursa @ James Tursa: I am grateful to you. Your detailed help is so helpful to a novice like me. I will try it again today and give you an update. Regarding my problem, I am trying to write a fortran program that can read a 3-dimensional arrays (created by matlab). Since the arrays are large, I would like to write them to a mat-file (binary format). I already have a few fortran subroutines that will take the arrays as inputs and produce results. All the subroutines have *.f90 format. That's why I am writing this f90 fortran program. Again, thank you. Hieu
From: HIEU on 28 Dec 2009 14:15
@ James Tursa I tried the code and got the following errors. Any idea? >> options = [matlabroot '\bin\win32\mexopts\intelf11msvs2005engmatopts.bat'] options = C:\Program Files\MATLAB\R2008a\bin\win32\mexopts\intelf11msvs2005engmatopts.bat >> mex('-f', options,'readinput.f90','MatlabAPImex.obj','MatlabAPImx.obj','MatlabAPImat.obj') MatlabAPImex.obj : error LNK2019: unresolved external symbol _MEXCALLMATLAB referenced in function _MATLABAPIMEX_mp_MEXCREATESPARSELOGICALMATRIX MatlabAPImex.obj : error LNK2019: unresolved external symbol _MEXPRINTF referenced in function _MATLABAPIMEX_mp_MEXPRINT MatlabAPImex.obj : error LNK2019: unresolved external symbol _MEXSETTRAPFLAG referenced in function _MATLABAPIMEX_mp_MEXGET readinput.exe : fatal error LNK1120: 3 unresolved externals C:\PROGRA~1\MATLAB\R2008A\BIN\MEX.PL: Error: Link of 'readinput.exe' failed. ??? Error using ==> mex at 207 Unable to complete successfully. As you mentioned before, is this possible to build a mexFunction routine instead in order to do the work? I have no idea how to do that and I would love to be advised. Thanks, Hieu |