Controlling Neuron From Matlab in windows

The basics of how to develop, test, and use models.
Post Reply
JimH
Posts: 54
Joined: Tue Apr 10, 2007 3:36 pm
Location: Duke University

Controlling Neuron From Matlab in windows

Post by JimH »

On a basic level one can control neuron in Matlab via (type code in command window):

Code: Select all

![path]nrniv.exe -isatty
where in my case I used

Code: Select all

!C:\nrn60\bin\nrniv.exe -isatty
However, my goal was to modify parameters in Matlab, initialize values appropriately with parameter variation, then to run the neuron code, and to process the output in Matlab.

So far I have only been examining conduction velocity, but I think this approach could be expanded to other uses.

Approach:
write general scripts that control a main matlab function
this Matlab function initializes all values to default, unless passed into it
via a variable length cell array
This function then proceeds to vary the value that is specified to be changed by the script
To do that all parameters that could be affected are replaced in the .hoc files I use using a replace parameters function that I wrote.
Once all parameters are replaced I use the following code, which took a little bit of fumbling with to get.

Code: Select all

[status,result] = system('C:\nrn60\bin\nrniv.exe -nobanner runMain.hoc -c quit()')
The proceeding code runs the file I'd like and afterwards runs the quit() function. If there is an error in running the main program, it completes, then the quit() function is called and control is released back to Matlab. Before I had the quit() function inside the .hoc file, although this only worked if there were no errors, otherwise Matlab would wait and wait.

Now unfortunately, the status bit will be useless, because the quit() function will execute fine. However, all output will be passed to "result". I haven't actually completed this part yet, but I plan on analyzing the output from "result". I am planning on using print in the Neuron code at the end of the file to output some string essentially to show that it ran. This string should not be something that might be output during an error. Then I can look through "result" for that string, and if it is not present presume there was an error, and act accordingly.

Finally, there is the part of communicating data between Neuron and Matlab. For that I found the following functions/objects/methods helpful:
Vector.record() -> allows recording of a parameter during a simulation
Matrix.setcol()
Matrix.fprint() -> Matrix doesn't allow for recording, but I like the way that it prints to a file better than the vector, using the fprint command and the setcol commad I can put vectors into specific columns in my file
File.wopen
File.close -> these two functions are needed for opening and closing a file

Using dlmread() in Matlab and the space delimeter I can get the values that were recorded during stimulation and written to a file.

Some final thoughts/notes:
The "-nobanner" used when calling nrniv just gives me less to look through in the results. Another "trick" that I didn't understand at first when told to me was that by using {} around things like load_file, run, File.wopen, the outputs from those are suppressed. When I first found out about this I inserted print statements all over my code trying to figure out where the "offenders" were, before inserting {} around them until I really started to understand what procedures were showing outputs.

For example

Code: Select all

{load_file("noload.hoc")}
would prevent one more "1" from showing up in "results." This could of course be seen as well in the command window that opens up when you double click on a file to run it. Thus, by using {} there is that much less to handle in results. If somehow that output was meaningful to you, it would be possible to make a print call before hand, with some identifying string, for parsing out of "results."

Last thought, if you can do it in hoc code, do it. All that file writing takes up more computation time. I hope that helps. Feel free to make any comments/corrections/suggestions.
JimH
Posts: 54
Joined: Tue Apr 10, 2007 3:36 pm
Location: Duke University

Post by JimH »

I added the following to the code for detecting an error.

In hoc code (last line):

Code: Select all

print "No_Errors!"
In Matlab:

Code: Select all

[status,result] = system('C:\nrn60\bin\nrniv.exe -nobanner runMain.hoc -c quit()');
    if ~isempty(findstr('No_Errors!',result))
       [do whatever here]
    else
        error(result)
    end
In Matlab I look through the result string and look for the string "No_Errors!". If it isn't present I assume an error occurred, if it is found (isn't empty), then I run whatever code I would normally.
ted
Site Admin
Posts: 5784
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Post by ted »

Nice work. Thanks for posting this. I'm not sure it belongs in "Getting started" but will leave
it there for now. I will, however, create a new item in "Hot tips" that points to it.
Post Reply