Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8974073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:35:50+00:00 2026-06-15T18:35:50+00:00

If I have a single Matlab source file (m-file) containing a class definition (

  • 0

If I have a single Matlab source file (m-file) containing a class definition (classdef), is there any way to specify a particular set of code that is to be executed if I run the m-file? I mean the entire file, such as via the Run button in the IDE, from a shell, or from the Matlab command-line. I don’t mean manually selecting the code to be executed.

Similar behaviour exists in Java with the static main method and in Python by having code outside the class definition (possibly inside a if __name__==__main__ block).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:35:50+00:00Added an answer on June 15, 2026 at 6:35 pm

    The short answer is “no”; MATLAB classdef M-files are just intended to define objects, not form complete programs.

    The long answer is you might be able to get specific behavior out of your classdef function if, for example, you overload the constructor to take a flag specifying whether or not to “act like a variable” or “act like a program”.

    e.g.

    classdef myClass
    ...
    methods  
        function self = myClass(varargin)
           if nargin == 1 && strcmpi(varargin{1},'run')
              ..... %run the program
           else
              ..... %make the variable
    

    OR you can make a static method called main:

       methods (Static = true)
           function main()
              %enabes: myClass.main()
              ...
           end
    

    The IDE still won’t know what to do with your M-file to “run it”, but you can run it properly from the command line, or another M-file.

    That last sentence is not 100% correct – as Egon pointed out below, you CAN make MATLAB’s IDE run that code – use a “run configuration”: http://www.mathworks.com/help/matlab/matlab_prog/run-functions-in-the-editor.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Do Objective-C or MATLAB/Octave have source file extensions besides .m ? I ask because
I have one single file called Exercises.rb def ask(prompt) print prompt, ' ' $stdout.flush
I have a single big text file in which I want to process each
I have a matlab .mat file that stores a bunch of variables. How do
For my work I have to set up a project in Matlab, which is
From Mathworks An anonymous function consists of a single MATLAB expression and any number
In MATLAB (r2009b) I have a uint32 variable containing the value 2147484101. This number
i have a single filed which i need to clone with a clone button
I have A single page that has the following structure <form runat=server><placeholder></placeholder></form> I have
i have a single video (duration: 3 seconds) and i need to create 2

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.