Code-snippets in Live Scripts?

조회 수: 4 (최근 30일)
Michael
Michael 2024년 7월 3일
댓글: Michael 2024년 7월 4일
Hello,
in my Live Scripts I have some lines of codes which I use very frequently. One example is
set(get(gca, 'ylabel'), 'Rotation', 0, 'HorizontalAlignment', 'right', 'VerticalAlignment', 'middle')
to rotate the y-axis label.
In other progarmming environments it is possible to define code snippets to insert code, which is used often.
What I do not want: defining a function for such lines, as I distrubute the Live Scripts to students, and using support function leads to difficulties.
Are there any recommendations how to introduce code sippets?
Michael
  댓글 수: 2
John D'Errico
John D'Errico 2024년 7월 3일
편집: John D'Errico 2024년 7월 3일
You are looking for EXACTLY a function. But then, you tell us that you don't want a function. So what do you expect?
If you distribute live scripts, then nothing stops you from distributing functions too. Put them in a central repository, and have them link to it. Or distribute your functions with the scripts. I fail to see the problem.
If what you really want is exactly what you have in some other programming environment, then use it. WTP?
Michael
Michael 2024년 7월 3일
Soory for the confusion, it seems to a problem in the wording and a not very good exapmle..
I am not looking for MATLAB function, but a functionality in the editor. In coding IDEs you can easily access templates for frequently used code constructions e.g. if-else or switch-case statements as easiest example. And you can add your own code to these templates or code snippet.
So it should help during coding by not always typing the same statements. Actually there is the possibility with automatic completations to find functions. So I want to use this for my own code if possible.
That's what I am looking for. I hope I made my point clear now.

댓글을 달려면 로그인하십시오.

답변 (1개)

Dinesh
Dinesh 2024년 7월 3일
Hello,
These lines that you use very frequently can be defined as a string variable. Then, you can make use of the "eval" function and pass this string variable as a parameter to execute that line. Here's how you can do it for your "set()" call:
rotateYLabel = "set(get(gca, 'ylabel'), 'Rotation', 0, 'HorizontalAlignment', 'right', 'VerticalAlignment', 'middle');";
% Execute the code snippet using "eval" any number of times
eval(rotateYLabel);
Here's the documentation link for "eval": https://www.mathworks.com/help/matlab/ref/eval.html
Note: When you use "eval", you might see a warning that it is inefficient. As of now, I don't see any other workaround apart from using "eval".
  댓글 수: 3
Dinesh
Dinesh 2024년 7월 4일
@Michael, oh, so you looking for an in-built accessibility feature to store your snippets and retreive them whenever you want using shortcuts.
In the Github link mentioned in the above link, you can see examples of how to use this add-on.
Michael
Michael 2024년 7월 4일
Yes Dinesh you are right, that's what I was looking for.
The app is described very well, unfortunatly it does not work, I have to lookm at this.
Thank you for the link!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by