Overview
I am using the MATLAB kernel in Jupyter Notebook. I would like to write a function in the notebook, rather than referring to a function that is saved in another .m file. The problem is that when I try to do so, I get the error:
Error: Function definitions are not permitted in this context.
Visual example:
In a new notebook, it looks like the following picture:
Now, it does work if I make a new .m file:
and then call then function via the notebook:
but this is inconvenient. Is there a way to define functions from within a Jupyter Notebook directly?
My software
MATLAB 2017b
Windows 10
Jupyter running in chrome
Jupyter installed via anaconda
The documentation indicates that you can use the magic:
%%file name_of_your_function.m
To take your example, your cell should be written as follows:
%%file fun.m
function out = fun(in)
out = in + 1;
end
This creates a new file called fun.m. This allows MATLAB to do what it needs (a function in a separate file), and also allows you to write your function directly in the Jupyter Notebook.
Related
How to pass the dynamic path to %run command in databricks because the function used in another notebook needs to be executed in the current notebook?
dbutils.notebook.run(path = dbutils.widgets.get(), args={}, timeout='120')
I want to use be able to run through a list of config files and use %run to import variables from config files into a databricks notebook.
But I cant find a method to dynamically change the file following %run.
I have tried specifying a parameter like this:
config = './config.py'
%run $config
But it doesn't work. I cannot use dbutils.notebook.run(config) as I won't get access to the variables in my main notebook.
Can anything think of a way to do this?
Since, you have already mentioned config files, I will consider that you have the config files already available in some path and those are not Databricks notebook.
You can use python - configparser in one notebook to read the config files and specify the notebook path using %run in main notebook (or you can ignore the notebook itself by using configparser in main notebook)
Reference: How to read a config file using python
I know about nbconvert and use it to generate static html or ipynb files with the results output. However, I want to be able to generate a notebook that stays attached to a kernel that I already have running, so that I can do further data exploration after all of the template cells have been run. Is there a way to do that?
Apparently, you can do this through the Python API. I didn't try it myself, but for someone who will be looking for a solution, this PR has an example in the comments:
from nbconvert.preprocessors.execute import executenb, ExecutePreprocessor
from nbformat import read as nbread
from jupyter_client.manager import start_new_kernel
nb = nbread('parsee.ipynb', as_version=4)
kernel_name = nb.metadata.get('kernelspec', {}).get('name', 'python')
km, kc = start_new_kernel(kernel_name=kernel_name)
executenb(nb, kernel=(km, kc))
kc.execute_interactive('a') # a is a variable defined in parsee.ipynb with 'a = 1'
Not quite sure about your purpose. But my general solutions are,
to execute the notebook in command line and see the execution at the same time,
jupyter nbconvert --debug --allow-errors --stdout --execute test.ipynb
this will show the execute through all cells in debug mode even exception happens. but I can't see the result until the end of the execution.
to output the result to a html file, and then open the html file to see the results. I found this is more convenient.
jupyter nbconvert --execute --allow-errors --stdout test.ipynb >> result.html 2>&1
if you open result.html, it will be,
and all the errors and results will be shown on the page.
I would like to learn other answers/solutions from you all. thank you.
If I understood correctly you wish to open a Python console, and connect Jupyter notebook to that kernel instance?
Perhaps your solution would be to edit jupyter scripts itself and run the server in separate thread/background task implementing some sort of connection between threads and work in the jupyter console? Currently it's impossible because main thread is running the server.
This would require some work and I don't have any solution as-is, but I will look into that and maybe edit this answer if I can make it work.
But it seems that the easiest solution is to simply add another field in the notebook and do whatever you wish to do there. Is there a reason for not doing that?
For a project I am working on I'm preparing data in MATLAB, then running the data through a separate external application (written in C++) named Model v2.exe, then performing further analysis with the output in MATLAB. I'm trying to create an M-file which does all of this, but I am struggling to get the C++ program to launch from my MATLAB code.
How can I launch an external application from within my MATLAB code?
You can either make use of the ! operator, or the system() command.
First, rename you application to something that has no spaces in the name, like modelv2.exe. Next, either make sure it is in the system path, as defined by your system environment variables, or generate a full path to it (eg: C:\Users\Phil\Desktop\modelv2.exe).
You can call an executable program from the command line using the exclamation point or the system command:
!modelv2
or:
!C:\Users\Phil\Desktop\modelv2.exe
will cause Windows to execute the program hello.exe if there is such a file in the current directory or on the system path. Alternatively:
system('modelv2');
or
system('C:\Users\Phil\Desktop\modelv2.exe');
will do the same thing.
References
"MATLAB Central - call and run an external program in matlab under windows", Accessed 2014-03-19, <http://www.mathworks.com/matlabcentral/answers/11568-call-and-run-an-external-program-in-matlab-under-windows>
I am using IPython 0.12.1 notebook to connect from a Windows machine to a Linux server where the IPython kernel runs. Since the Linux machine doesn't have an X-Server installed, I am using the following configuration:
IPKernelApp.pylab = 'inline'
which displays the figures inline.
IPython closes the figures once they are drawn in the notebook. So how can I edit the figure which was just drawn? Like adding a title. The drawing process takes a while for my case and I would appreciate a means to play with the figure before saving it without a need to redraw it.
I tried saving a handle to the figure and working with that, but I was not successful.
I ended up setting the following in the ipython notebook configuration file:
c.InlineBackend.close_figures = False
The figures are editable but I have to close them myself instead which I do by close('all')
You can enter this magic configuration:
%config InlineBackend.close_figures = False