M-file code formatting tool

Is there any similar to AStyle formatting tool for C/C++/C# languages which can beautify matlab m-file code?
Ctrl+I is too simple for my needs

2 件のコメント

Jan
Jan 2011 年 10 月 22 日
To answer this, we have to know your needs.
Personally I prefer an indentation, which is as trivial as possible - simply eye-popping.
Yuriy Chesnokov
Yuriy Chesnokov 2011 年 10 月 22 日
編集済み: Guillaume 2018 年 11 月 28 日
the same as in AStyle
to convert some unreadable or unformatted code to uniform style
example:
[V,D]=eig(Kn);clear Kn;
V=sortrows([V' diag(D)],size(X,1)+1);V=flipud(V);
latent=V(:,end);
coeff= V(:,1:end-1)';
clear V;clear D;
to uniform:
[V, D] = eig(Kn);
clear Kn;
V = sortrows([V' diag(D)], size(X,1)+1);
V = flipud(V);
latent = V(:,end);
coeff = V(:,1:end-1)';
clear V;
clear D;

サインインしてコメントする。

回答 (7 件)

David Varga
David Varga 2016 年 6 月 6 日

4 投票

I know it is an old question, and I just put this here as an addition.
You could check: MBeautifier
I have just created it for personal use, it is a lightweight M-Script based tool that can be usable to format Matlab M-Code directly in the Matlab editor

5 件のコメント

Yi Cao
Yi Cao 2018 年 3 月 29 日
Yes, that's exactly what I want, very useful!
Alexandre Kozlov
Alexandre Kozlov 2018 年 10 月 9 日
Great tool!
Just some remarks: * It doesn't seems to take parameters file into account, you have to cange rules directly in the code of MBeautyConfigurationRules.m. * Sometimes it doesn't recognize tokens, for instance "A(j-1)" is correctly beautified as "A(j - 1)", but "A{j-1}" is not. * Sometimes it adds empty lines at the beginning and at the end of the file, I didn't found any rule when it does. * I didn't found an option to indent the whole function's body (it is the default behaviour of Matlab not to indent, but I prefer to indent)
Nevethenless, it's a great tool which saves me a lot of time! Thanks again!
gwoo
gwoo 2018 年 11 月 28 日
That feature (to pad tokens inside { } exists in the xml settings near the bottom.
To indent a whole function's body you need to set that in the smart indenting settings of matlab itself.
see here:
Set to INDENT ALL FUNCTIONS
hasan fawaz
hasan fawaz 2019 年 7 月 30 日
I love you man :D Life Saver :D !
Ruben Lange
Ruben Lange 2020 年 4 月 28 日
Hi David,
I know I am very late to this, but I don't really know how to add the root directory to my matlab path as you describe on the github page...
I also don't understand how to do the shortcuts part.
Could you (or someone else) help me with this?
Thanks in advance!
Ruben

サインインしてコメントする。

Florian Schanda
Florian Schanda 2021 年 3 月 26 日

3 投票

Hi, there is https://github.com/florianschanda/miss_hit which can format code and much more.
Troy_Daniel
Troy_Daniel 2019 年 11 月 21 日

1 投票

I know this question is quite old, and there is already a solution for Maltab GUI Editor. However, I prefer to edit M-files with vim, I'm frustrated to find that there was no a proper solution for commandline usage. So I imlement one using C++, which achieve quite simple formatting. The code is available on Github https://github.com/TroyDanielFZ/Matlab-M-Source-Formator .
This answer is posted in case that someone else need this implement for editors like vim.
Brian Harris
Brian Harris 2022 年 8 月 23 日
編集済み: Brian Harris 2022 年 8 月 23 日

1 投票

vscode has a pretty good matlab formater built in (formats on save). Underpinning the vscode plugin is a python script which you can run on the command line (matlab_formatter.py <matlab_file_name>)
Jan
Jan 2011 年 10 月 22 日

0 投票

Did you try the "Crimson Editor" or XEmacs already?

1 件のコメント

Yuriy Chesnokov
Yuriy Chesnokov 2011 年 10 月 22 日
no, do they support matlab code formatting? as in my comments to your question above?

サインインしてコメントする。

Narendra Kulkarni
Narendra Kulkarni 2014 年 9 月 29 日

0 投票

Hi, I am facing the same problem. There arent any good tools to beautify matlab code or are there?

2 件のコメント

Image Analyst
Image Analyst 2014 年 9 月 30 日
編集済み: Image Analyst 2014 年 9 月 30 日
I don't understand why control-I is not acceptable for you and Yuriy. What's wrong with the way it does it? It fixes the indenting, though it doesn't add spaces around operators such as = and +. Is that the problem?
Evgeny Mirkes
Evgeny Mirkes 2018 年 1 月 11 日
編集済み: Evgeny Mirkes 2018 年 1 月 11 日
Because indentation is not enough for beauty code.

サインインしてコメントする。

Julian Hapke
Julian Hapke 2023 年 12 月 11 日

0 投票

Here's a MATLAB snippet that depends on the undocumented tree2str and mtree, but produces a reasonable result:
tree2str(mtree(FILENAME, '-file', '-comments'))

カテゴリ

ヘルプ センター および File ExchangeFile Operations についてさらに検索

製品

タグ

質問済み:

2011 年 10 月 22 日

回答済み:

2023 年 12 月 11 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by