Code-snippets in Live Scripts? (2024)

20 views (last 30 days)

Show older comments

Michael on 3 Jul 2024 at 7:43

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2134026-code-snippets-in-live-scripts

  • Link

    Direct link to this question

    https://www.mathworks.com/matlabcentral/answers/2134026-code-snippets-in-live-scripts

Commented: Michael about 10 hours ago

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 Comments

Show NoneHide None

John D'Errico on 3 Jul 2024 at 12:41

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2134026-code-snippets-in-live-scripts#comment_3201476

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2134026-code-snippets-in-live-scripts#comment_3201476

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 on 3 Jul 2024 at 13:20

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2134026-code-snippets-in-live-scripts#comment_3201516

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2134026-code-snippets-in-live-scripts#comment_3201516

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.

Sign in to comment.

Sign in to answer this question.

Answers (1)

Dinesh on 3 Jul 2024 at 8:55

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2134026-code-snippets-in-live-scripts#answer_1480466

  • Link

    Direct link to this answer

    https://www.mathworks.com/matlabcentral/answers/2134026-code-snippets-in-live-scripts#answer_1480466

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 Comments

Show 1 older commentHide 1 older comment

Michael on 3 Jul 2024 at 10:38

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2134026-code-snippets-in-live-scripts#comment_3201366

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2134026-code-snippets-in-live-scripts#comment_3201366

Thank you for your sugestion.

I think I was not clear enough. I am looking for a solution to insert a coed snippet durin g editing a Live Script, like a text module, which I acn access with a shortcut or so.

Your suggestion is form the effect similra to a fuunction and not what I am looking for.

Dinesh about 12 hours ago

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2134026-code-snippets-in-live-scripts#comment_3202206

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2134026-code-snippets-in-live-scripts#comment_3202206

@Michael, oh, so you looking for an in-built accessibility feature to store your snippets and retreive them whenever you want using shortcuts.

Maybe this can help you: https://www.mathworks.com/matlabcentral/fileexchange/70524-matlab-snippets/

In the Github link mentioned in the above link, you can see examples of how to use this add-on.

Michael about 10 hours ago

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/2134026-code-snippets-in-live-scripts#comment_3202296

  • Link

    Direct link to this comment

    https://www.mathworks.com/matlabcentral/answers/2134026-code-snippets-in-live-scripts#comment_3202296

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!

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABEnvironment and Settings

Find more on Environment and Settings in Help Center and File Exchange

Tags

  • code snippet

Products

  • MATLAB

Release

R2024a

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Code-snippets in Live Scripts? (8)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

Code-snippets in Live Scripts? (2024)

References

Top Articles
Instant Pot Chuck Wagon Dinner - 365 Days of Slow Cooking and Pressure Cooking
The Chuckwagon – Western Recipes – Legends of America
The Blackening Showtimes Near Century Aurora And Xd
Federal Fusion 308 165 Grain Ballistics Chart
From Algeria to Uzbekistan-These Are the Top Baby Names Around the World
DENVER Überwachungskamera IOC-221, IP, WLAN, außen | 580950
Alpha Kenny Buddy - Songs, Events and Music Stats | Viberate.com
What's Wrong with the Chevrolet Tahoe?
Corporate Homepage | Publix Super Markets
State Of Illinois Comptroller Salary Database
Globe Position Fault Litter Robot
12 Best Craigslist Apps for Android and iOS (2024)
Epaper Pudari
Luna Lola: The Moon Wolf book by Park Kara
Morgan And Nay Funeral Home Obituaries
Unlv Mid Semester Classes
Craftology East Peoria Il
"Une héroïne" : les funérailles de Rebecca Cheptegei, athlète olympique immolée par son compagnon | TF1 INFO
Directions To Advance Auto
My Homework Lesson 11 Volume Of Composite Figures Answer Key
Cbssports Rankings
Noaa Duluth Mn
Myhr North Memorial
Greyson Alexander Thorn
Ou Class Nav
Utexas Iot Wifi
Prey For The Devil Showtimes Near Ontario Luxe Reel Theatre
Chime Ssi Payment 2023
Kirsten Hatfield Crime Junkie
Local Collector Buying Old Motorcycles Z1 KZ900 KZ 900 KZ1000 Kawasaki - wanted - by dealer - sale - craigslist
Pioneer Library Overdrive
Meta Carevr
Shelby Star Jail Log
Cinema | Düsseldorfer Filmkunstkinos
2021 Tesla Model 3 Standard Range Pl electric for sale - Portland, OR - craigslist
Metra Union Pacific West Schedule
Ket2 Schedule
Top-ranked Wisconsin beats Marquette in front of record volleyball crowd at Fiserv Forum. What we learned.
2008 Chevrolet Corvette for sale - Houston, TX - craigslist
Weapons Storehouse Nyt Crossword
Cl Bellingham
Page 5662 – Christianity Today
Tokyo Spa Memphis Reviews
Blackstone Launchpad Ucf
Nba Props Covers
Ehc Workspace Login
Caphras Calculator
Oakley Rae (Social Media Star) – Bio, Net Worth, Career, Age, Height, And More
Canvas Elms Umd
Beds From Rent-A-Center
Nfhs Network On Direct Tv
Philasd Zimbra
Latest Posts
Article information

Author: Lakeisha Bayer VM

Last Updated:

Views: 5920

Rating: 4.9 / 5 (49 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Lakeisha Bayer VM

Birthday: 1997-10-17

Address: Suite 835 34136 Adrian Mountains, Floydton, UT 81036

Phone: +3571527672278

Job: Manufacturing Agent

Hobby: Skimboarding, Photography, Roller skating, Knife making, Paintball, Embroidery, Gunsmithing

Introduction: My name is Lakeisha Bayer VM, I am a brainy, kind, enchanting, healthy, lovely, clean, witty person who loves writing and wants to share my knowledge and understanding with you.