matlab_wrapper icon indicating copy to clipboard operation
matlab_wrapper copied to clipboard

Evaluating .m file taking string as input

Open cgerlein opened this issue 8 years ago • 7 comments

Hi,

I have been trying to use teh matlab_wrapper as an alternative to the Engine, which for some obscure reason, just stopped working on me recently and I just haven't been able to fix it. I'm trying to evaluate a function that takes one input: a filename. Here is what I'm doing:

import matlab_wrapper
matlab = matlab_wrapper.MatlabSession(matlab_root='/Applications/MATLAB_R2015a.app/')
matlab.put('filename','/Volumes/Cynthia_ExtHD_2TB/Scatterometer/Amazon/Amazon_Quickscat_SIR_Data/queh-a-Ama00-001-004.sir')
matlab.eval('loadsir')

At which point I get the following error message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/matlab_wrapper/matlab_session.py", line 181, in eval
    raise RuntimeError("Error from MATLAB\n{0}".format(error_string))
RuntimeError: Error from MATLAB
MATLAB:minrhs: Not enough input arguments.
Error: in fuction loadsir in file /Users/cynthiagerlein/Dropbox (Personal)/Scatterometer/Matlab/loadsir.m line 143

My Matlab function definitely only takes one argument. Does it have something to do with the fact that it's a string...?

cgerlein avatar May 11 '16 21:05 cgerlein

You problem is mostly likely that you are not passing the variable to the function. Try matlab.eval('loadsir(filename)'). The eval command literally evaluates what you submit, so as far as it is concerned, you are trying to call the function without parameters.

grahamj1978 avatar May 11 '16 22:05 grahamj1978

Thanks, that makes sense. I was just blindly copying the example file, without realizing that it wasn't calling for an argument. I'm having trouble with the fact that the argument is a string: I tried all the combination of ' but it's not looking like any of them works:

matlab.eval('loadsir'('/Volumes/Cynthia_ExtHD_2TB/Scatterometer/Amazon/Amazon_Quickscat_SIR_Data/queh-a-Ama00-001-004.sir'))
matlab.eval('loadsir(/Volumes/Cynthia_ExtHD_2TB/Scatterometer/Amazon/Amazon_Quickscat_SIR_Data/queh-a-Ama00-001-004.sir)')
matlab.eval('loadsir'(/Volumes/Cynthia_ExtHD_2TB/Scatterometer/Amazon/Amazon_Quickscat_SIR_Data/queh-a-Ama00-001-004.sir))
matlab.eval(loadsir('/Volumes/Cynthia_ExtHD_2TB/Scatterometer/Amazon/Amazon_Quickscat_SIR_Data/queh-a-Ama00-001-004.sir'))

Both the function name and the argument have to be strings, which seems to confuse either the wrapper or Matlab, depending on the version... Any idea how to get around that problem?

cgerlein avatar May 12 '16 01:05 cgerlein

2016-05-12 03:46 +0200, Cynthia Gerlein-Safdi [email protected]:

Thanks, that makes sense. I was just blindly copying the example file, without realizing that it wasn't calling for an argument. I'm having trouble with the fact that the argument is a string: I tried all the combination of '' but it's not looking like any of them works:

matlab.eval('loadsir'('/Volumes/Cynthia_ExtHD_2TB/Scatterometer/Amazon/Amazon_Quickscat_SIR_Data/queh-a-Ama00-001-004.sir'))
matlab.eval('loadsir(/Volumes/Cynthia_ExtHD_2TB/Scatterometer/Amazon/Amazon_Quickscat_SIR_Data/queh-a-Ama00-001-004.sir)')
matlab.eval('loadsir'(/Volumes/Cynthia_ExtHD_2TB/Scatterometer/Amazon/Amazon_Quickscat_SIR_Data/queh-a-Ama00-001-004.sir))
matlab.eval(loadsir('/Volumes/Cynthia_ExtHD_2TB/Scatterometer/Amazon/Amazon_Quickscat_SIR_Data/queh-a-Ama00-001-004.sir'))

but none of it seems to work because both the function name and the argument have to be strings... Any idea how to get around that problem?

You could either properly escape the single quote characters, or use double quotes as the external quotes, or do something like this:

matlab.put('filename', '/Volumes/Cynthia_ExtHD_2TB/Scatterometer/Amazon/Amazon_Quickscat_SIR_Data/queh-a-Ama00-001-004.sir')
matlab.eval('loadsir(filename)')

I think that the last one is the preferable way. ;) Did it work?

Marek

mrkrd avatar May 12 '16 08:05 mrkrd

2016-05-12 10:18 +0200, [email protected]:

2016-05-12 03:46 +0200, Cynthia Gerlein-Safdi [email protected]:

Thanks, that makes sense. I was just blindly copying the example file, without realizing that it wasn't calling for an argument. I'm having trouble with the fact that the argument is a string: I tried all the combination of '' but it's not looking like any of them works:

matlab.eval('loadsir'('/Volumes/Cynthia_ExtHD_2TB/Scatterometer/Amazon/Amazon_Quickscat_SIR_Data/queh-a-Ama00-001-004.sir'))
matlab.eval('loadsir(/Volumes/Cynthia_ExtHD_2TB/Scatterometer/Amazon/Amazon_Quickscat_SIR_Data/queh-a-Ama00-001-004.sir)')
matlab.eval('loadsir'(/Volumes/Cynthia_ExtHD_2TB/Scatterometer/Amazon/Amazon_Quickscat_SIR_Data/queh-a-Ama00-001-004.sir))
matlab.eval(loadsir('/Volumes/Cynthia_ExtHD_2TB/Scatterometer/Amazon/Amazon_Quickscat_SIR_Data/queh-a-Ama00-001-004.sir'))

but none of it seems to work because both the function name and the argument have to be strings... Any idea how to get around that problem?

You could either properly escape the single quote characters, or use double quotes as the external quotes, or do something like this:

matlab.put('filename', '/Volumes/Cynthia_ExtHD_2TB/Scatterometer/Amazon/Amazon_Quickscat_SIR_Data/queh-a-Ama00-001-004.sir')
matlab.eval('loadsir(filename)')

I think that the last one is the preferable way.

This is also what @grahamj1978 pointed out. Thanks!

mrkrd avatar May 12 '16 08:05 mrkrd

Hey, Actually, no this last solution did not work either: I don't get an error, but the code just don't seem to run, probably because it's taking 'filename' as the filename. What did work though is the following: img = matlab.workspace.loadsir('/Volumes/Cynthia_ExtHD_2TB/Scatterometer/Amazon/Amazon_Quickscat_SIR_Data/queh-a-Ama00-001-004.sir') found HERE and apparently posted by you! Does it make sense?

cgerlein avatar May 12 '16 12:05 cgerlein

2016-05-12 14:13 +0200, Cynthia Gerlein-Safdi [email protected]:

Hey, Actually, no this last solution did not work either: I don't get an error, but the code just don't seem to run, probably because it's taking 'filename' as the filename. What did work though is the following: img = matlab.workspace.loadsir('/Volumes/Cynthia_ExtHD_2TB/Scatterometer/Amazon/Amazon_Quickscat_SIR_Data/queh-a-Ama00-001-004.sir') found HERE. Does it make sense?

Yes, using matlab.workspace is an alternative way to call MATLAB functions.

However, I'm still surprised that the other method (matlab.put('name', '/foo/bar.ext'); matlab.eval('loadsir(name)')) did not work. Could you send me a simplified version of loadsir.m that illustrates the issue? The simplified loadsir() does not have to do anything: just take a string as input and fail -- I would like to replicate your problem.

Thank you!

Marek

mrkrd avatar May 16 '16 17:05 mrkrd

Hello Marek,

The loadsir.m file is actually available here: ftp://ftp.scp.byu.edu/pub/software/matlab/loadsir.m. It calls a bunch of other functions that are in that same matlab folder if you want to just try with the whole thing. An example file can be downloaded directly from here: ftp://ftp.scp.byu.edu/data/qscat/1999/sir/queh/SAm/201/a/queh-a-SAm99-201-204.sir.gz. You need to unzip it first, loadsir.m needs a .sir file.

The matlab.workspace option worked perfectly by the way, and I was so happy with how easy it was to install the matlab_wrapper. Great job!

Let me know if you reproduce the error.

Cynthia

cgerlein avatar May 16 '16 17:05 cgerlein