Cannot read an m.file in Atom with octave although it works in Nteract - matlab

I installed the octave kernel for jupyter which works fine with nteract. In Atom I have the following packages installed: language-matlab, language-matlab-octave, hydrogen and Script. However when I load an .m file in Atom there is a red message for “No Kernel for grammar Matlab found”.

Open the settings pane in Atom and navigate to the packages section.
Next, get to Hydrogen's settings page by searching for hydrogen then clicking Hydrogen.
Scroll down to the language mapping section and set octave (or whatever the kernel's name is) to matlab for the language:

Related

I am not having autocompletion for C and C++ on Atom editor (Windows 10 x64)

I really wanted to have autocomplete for C and C++ languages on Atom text editor.Then I came across autocomplete-clang. But I am still not getting the results (the dropdown menu which shows the methods and functions) which I was searching for, after installing the package.
I have installed Clang for Windows and added it to my PATH and seems to be running as I checked in CMD prompt. Then I installed the package autocomplete-clang on Atom editor. Its not showing the results.
Am I missing something here?
Does anybody have autocompletion for C and C++ languages on the Atom editor?
Is there any other way?

How do I run MATLAB (.m) files in VSCode?

I am not able to figure out how to run MATLAB (.m) code in VSCode.
I have installed the MATLAB Extension for VSCode and added the path for the mlint file for MATLAB as stated in the extension.
"matlab.mlintpath"
VSCode identifies the file as a MATLAB file. However, the code won't run.
How can I run MATLAB code from within VSCode?
There is a way, this VSCode extension suggests that you can use it to run MATLAB code from within VSCode without having to open the MATLAB GUI. It does not invalidate the below though; you need to have a working, licensed MATLAB installation, which will be called in the background. Without a working, licensed MATLAB installation you cannot run MATLAB code.
As MATLAB is proprietary, there are only two ways to run MATLAB code: by running the code from the MATLAB IDE (with a valid license of course), or by compiling it to an executable file with the MATLAB compiler.
You might be able to get other IDEs to have syntax highlighting and typing-hints like MATLAB's own IDE through importing its mlint functionality, like you did, but that won't execute the code.
Thus no, you cannot execute MATLAB code directly within VSCode, you can only covertly call the MATLAB engine from inside VSCode.
A possible workaround would be to export your MATLAB code to C/C++ using the MATLAB Coder. You can then run the C/C++ code from within VSCode.
TL;DR:
I wrote Matlab Terminal Extension for VS Code on Linux. It can run matlab files and spawn a matlab-Terminal. You can find the repo here.
MatTer for VS Code
I recently wanted to used a matlab terminal in vscode. There even is an extension for this. But while using the extension I faced some flaws. Also the repository does not seem to be active anymore and the owner doesn't react to issues and similar.
So I decided to write my own first extension MatTer for VS Code and published it to the VS marketplace. It's in early Development, but it can already spawn a matlab-Terminal and run matlab files.
The repo is hosted on Github, see here: https://github.com/mauzigoe/matter-for-vscode
To install it via vscode just search for matter-for-vscode in Extension.
Critics and Feedback is welcomed, esp. since I'm new to this.
"#Liam the long discourse is necessary, because having the extension without a valid MATLAB license+installation won't work.T"
...because at some point you need at least ONE working matlab install just to develop the m-file :)
I suspect that you're right, and it comes down to a choice between embedding "foreign" code into an m-file, or making calls to the matlab engine from within a foreign IDE. They do have a free RTE but you still need some form of Matlab IDE to write in plus the Matlab Compiler toolbox to build the project out for the RTE.
The work-around is to use OS tools to remove Matlab and other proprietary tools from the loop entirely. That is why if you're going to be forced to pay $5k for a proprietary tool, it has to be justified in other ways than "it can get the job done".
VS Code extensions have the same problem as any other extension: if you look at the repository and see what is required to install and run the extension? You'll probably never install an extension again. It's about as wise as blithely downloading and installing any code from the Internet: it's no better than the source. I would wait until you figure-out how to call getOpen() etc within VS Code without downloading some random extension from some random source especially one that hasn't been updated in the past 2 years. Or at least use VS2019 Community:
https://www.mathworks.com/matlabcentral/answers/100603-how-can-i-compile-a-matlab-engine-application-using-microsoft-visual-studio-9-0-or-10-0
you could use the "Matlab Interactive Terminal" Extention, the link is below.
To use it, you need to follow the instruction to install a "MATLAB Engine API for Python" and you are good to go (and you need a working Matlab).
after installing the extension, you could open the Matlab terminal in the command window (by pressing Ctrl+Shift+P and input "Open a Matlab Terminal"), and you could get the same Matlab terminal as in the real Matlab IDE, and now you could run .m code in Vscode.
You can use the Code Runner extension and put this line in your settings.json:
"code-runner.executorMapByFileExtension": {
".m": "cd $dir && matlab -batch \"$fileNameWithoutExt\""
}
You would probably also want to install the MATLAB extension.

VSCode: How to export a python file that was imported from a Jupyter Notebook back to Jupyter format?

Probably a silly question, but I couldn't find it. Visual Studio Code editor has a really nice way to work with Jupyter Notebooks. I can edit the cells directly in vscode and run them. Now it would be easy to work with version control.
But I couldn't find a way to convert it back to a Notebook! How do I generate a notebook back from the generated python file?
I understand that the notebook wouldn't have the output cells in it.
There is an option in the interactive Python window that has the notebook output:
This is really cool, now you can work in a Python file and have a really nice interface with your version configuration system (Git).
Yes, it was a silly question :-)
The Jupytext library supports the percent-based cell format used by VSCode-Python, as well as other text-based notebook interchange formats:
https://github.com/mwouts/jupytext
If you need more control over how conversion to ipynb is done (or you need to have cross-references) then you can give a try to Pandoctools. It can export VSCode *.py documents to any Pandoc output format or to Jupyter notebook.
For example you can create and register Jupyter kernel. For example is can be named "nn". That should be the same kernel that you selected in VSCode (there you select it by path but VSCode still uses installed kernels specs under the hood). Then add hat to the Python file, split document to cells, provide settings and set Markdown cells (commented metadata line would export to pdf instead of ipynb; I recommend to open ipynb in nteract native app):
"""
---
kernels-map:
py: nn
jupyter:
kernelspec:
display_name: nn
language: python
name: nn
pandoctools:
# out: "*.pdf"
out: "*.ipynb"
...
# Markdown section title 1
Some **static** Markdown text.
"""
# %% {echo=False}
import IPython.display as ds
import math
import sugartex as stex
# %% {markdown}
"""
# Markdown section title 2
The quick brown Fox jumps over the lazy dog.
"""
# %%
ds.Markdown(stex.pre(f'''
Some **dynamic** Markdown text with SugarTeX formula: ˎα^˱{math.pi:1.3f}˲ˎ.
It works because of the `Markdown` display option and `sugartex` Pandoc filter.
Acually `stex.pre` is redundant here but it is needed when the text is imported
or read from somewhere instead of being written in the same document.
'''))
Then convert the file via pandoctools: drag and drop file to pandoctools shortcut/executable or "open with" pandoctools executable.
Also see:
Two introduction articles are at the beginning of this README,
examples of input to output conversion that have cross-references!
how to use Pandoctools and it's CLI,
how to use Knitty that collects Jupyter outputs and change it's settings.
Use jupytext library:
Install:
pip install jupytext
Now open CMD or internal terminal to the folder with your .py file
Run this:
jupytext --set-formats py:percent,ipynb filename.ipynb

Convert ipynb to pdf in Jupyter

I am new to ipython notebook, and I would like to convert my ipynb to pdf. But I get the following error when I try to Download as PDF via LaTex.
nbconvert failed: pdflatex not found on PATH
There is no documentation anywhere how to add pdflatex to my PATH. I use windows. Thank you!
A simple and surprisingly good solution is to print the notebook to pdf through the browser with ctrl+p. Just make sure your plots and figures are not on interactive mode otherwise they will not be displayed (set them to %matplotlib inline).
Exporting jupyter notebooks through latex is quite troublesome and takes a lot of tinkering to get something remotely close to publish ready. When I absolutely need publication quality I do it on a latex editor, but this tutorial goes in great length about doing it on jupyter.
A few useful tips to get better results:
Higher resolution plots
Hide your code-cells from the pdf
Take a look at these extensions to improve your jupyter documents
For Mac OS X, the solution for me was to install MacTex first and then export the path to find it:
### TeX
export PATH="/Library/TeX/Distributions/.DefaultTeX/Contents/Programs/texbin:$PATH"
You can add this to your .bash_profile or similar config file to load it every time.
See more here https://github.com/jupyter/nbconvert/issues/406
As said by Thomas K in the comments, you need to have Latex installed, and after add the path to the directory containing pdflatex.exe file to the PATH variable of your system.
I have looked for a lightweight distribution and tried installing TeXworks, but I didn't find any pdflatex.exe file.
So I have tried TeX Live, which worked fine creating the pdflatex.exe file under the target installation directory. This path should be like C:\...\texlive\2016\bin\win32.
Finally, you should just add this path to the PATH environment variable of your system (you can use the link shared by Thomas K).
As said here, you need to quit jupyter notebook and open a new command prompt after making any path changes, in order for jupyter to find the newly added item to the PATH.
Then, in Jupyter, you can check your environment variables by running the following (refer to this link for details):
import os
os.environ['PATH'].split(';')
and check if it contains the path to pdflatex.exe file.
If you get some trouble when exporting your notebook to pdf due to missing files/packages (this happened to me), refer to this link to search and install them under TeX Live.
For Linux, the reported error is due to the lack of XeLatex, part of the texlive-xetex package.
Installation in ubuntu will be:
sudo apt install texlive-xetex
Instead of using nbconvert what you can do is :
Download your ipynb file as HTML from File option.
Right-click and select print or use Ctrl+P.
Save as PDF
Easy.
Here is the full solution that worked for me (for Mac).
brew cask install mactex
$ cd ~/
$ touch .bash_profile
This will open the bash profile on TextEditor
$ open -e .bash_profile
Paste the following to the top and save
export PATH="/Library/TeX/Distributions/.DefaultTeX/Contents/Programs/texbin:$PATH"
Close any notebook you have and reopen it
Here is the notebook explaining it step by step:
https://github.com/ybaktir/notes/blob/master/Convert%20Jupyter%20Notebook%20to%20Pdf.ipynb
I agree that latex installation (at least on windows) is painful and the result in my case was not a great looking document. The ctrl-p method alone doesn't work great if you're running in JupyterLab, but if you export the notebook to HTML, then print from the browser, choosing PDF, the result is quite good.
I know my solution is not at a level. But it works !!
in your browser of notebook tab, simply do "ctrl + p" to get download in pdf
First export the notebook file to HTML (available through File> Download as..).
If you are using JupyterLab, then this is available under File > Export Notebook As....
Use (any) free online converters to convert html file to a pdf file. (One such free online converter is sejda (https://www.sejda.com/html-to-pdf)
Note, there are many such converters are available online.

External editor for IPython notebook

I am using IPython notebook and I want to edit programs in an external editor.
How do I get the %edit file_name.py to open an editor such as Notepad++.
Running %edit? will give you the help for the %edit magic function.
You need to set c.TerminalInteractiveShell.editor, which is in your ipython_config.py. I'm not quite sure where this is located in Windows; on OS X and Linux, it is in ~/.ipython. You'll want to set the variable to be the full path of the editor you want.
Alternatively, you can create an environment variable EDITOR in Windows itself, and set that equal to the full path of the editor you want. iPython should use that.
I'm using Windows 7 and 8 (and 10TP) and Python 3.4.2.
I started with ipython locate to tell me where ipython thought config files suggested elsewhere should be. When I saw it was different I read around and came up with the following:
On my system, the ipython locate gave me c:\users\osmith\.ipython, not the _ipython you'll see mentioned in the YouTube videos done with Windows XP,
Look in the directory ipython locate specifies for a profile directory; if you aren't actively doing anything with ipython profiles, it should be .ipython\profile_default, if you are using profiles, then I leave it to you to s/profile_default/${YOUR_PROFILE_NAME}/g
Check the profile_default directory for a ipython_config.py file, if it's not there, tell IPython to initialize itself: ipython profile create
Open the config file in a text editor,
If you are the kind of person who hasn't messed around with their console overly much and installs things in standard places, you can skip straight to this step by typing: ipython profile create followed by start notepad .ipython\profile_default\ipython_config.py.
Search for the string c.TerminalInteractiveShell.editor,
The comment above this indicates you can also use the EDITOR environment variable, but hard coding file paths never hurt anyone so lets do eet:
Copy the line and remove the leading hash and spaces from the copy.
Replace the text between the apostrophes ('notepad') with the path of our desired editor, e.g.
c.TerminalInteractiveShell.editor = 'c:/program files (x86)/noddyeditor/noddy.exe'
There is a catch here, though; some modern editors get a bit fancy and automatically and, when invoked like this, detach from the console. Notepad++ and Sublime Text, for example. Sublime accepts a "--wait" option, which works some of the time; this tells the command invocation to hang around until you close the file, for some definition of until and some other definition of close.
However, the following setting will work most of the time for sublime text:
c.TerminalInteractiveShell.editor = '"c:/program files/sublime text 3/subl.exe" --wait'
(assuming c:\program files\ is where your sublime text 3 directory is)
Try the 'Pycharm' editor
This works for me.