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 7159311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:12:36+00:00 2026-05-28T13:12:36+00:00

For example in testinit.m I have the following function [x, y, m] = testinit

  • 0

For example in testinit.m I have the following

function [x, y, m] = testinit

x=4
y=3
m=2

When I run testinit in the console it correctly displays the value. However when I type x it says

error: ‘x’ undefined…

  • 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-05-28T13:12:37+00:00Added an answer on May 28, 2026 at 1:12 pm

    The other answers are all possible solutions, but potentially more complicated than what you may be looking for. I think the first part of yuk’s answer addresses the real problem you are having, but I think it deserves a more detailed explanation…

    If you have a function that has output arguments, you need to actually capture those arguments in variables when you call the function. For example, if you typed this in the Command Window:

    [x, y, m] = testinit;
    

    Then you would have your three output values present for you to use. What you were probably doing was typing this:

    testinit;
    

    This would display the values (because you didn’t end each line in the function with a semicolon to suppress displaying them), but it would not store them in variables in the Command Window for you to use later.

    This is a result of how variables are stored in MATLAB, as described by the documentation on variable scope:

    MATLAB stores variables in a part of memory called a workspace. The base workspace holds variables created during your interactive MATLAB session and also any variables created by running scripts. Variables created at the MATLAB command prompt can also be used by scripts without having to declare them as global.

    Functions do not use the base workspace. Every function has its own function workspace. Each function workspace is kept separate from the base workspace and all other workspaces to protect the integrity of the data used by that function. Even subfunctions that are defined in the same file have a separate function workspace.

    So, if you want to share variables between functions, the simplest way is to pass them back and forth via their input and output argument lists.

    It should also be noted that the names you give variables in the output argument list of the function don’t have to match the names of the variables you place those output values in. For example, given this function:

    function [a, b, c] = testinit
      a = 4;
      b = 3;
      c = 2;
    

    You can make this call in the Command Window:

    [x, y, m] = testinit;
    

    And you will get x = 4, y = 3, and m = 2.

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

Sidebar

Related Questions

I have just been working with boost::bind and boost::function and noticed the following behaviour
Example: var test = 'global value'; (function() { var test = 'local value'; //
Example: You have a shortcut s to SomeProgram in the current directory. In cmd.exe
Example I have Person , SpecialPerson , and User . Person and SpecialPerson are
example: I want to see if array[5] holds a value or is empty.
Example: var sessions = require('sessions'); function load_session(req) { sessions.load(sid); } Now that the session
Example, I have billions of short phrases, and I want to clusters of them
Example I have: @test = Pakke.find([[4], [5]]) In my Pakke table I have a
Example class code: <?php class example { public function forExample() { return true; }
Example: It's really annoying to type a list of strings in python: [January, February,

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.