How can I have a pushbutton load the variables in a .mat file into the matlab workspace?

5 views (last 30 days)
I have a drop down menu with various selections and based on the drop down I have a switch case to perform a load call for a .mat file however it goes through the function but does not out any variables to my worspace. Is there a way to have the pushbutton load variables from different files to the workspace based upon the selection in the dropdown menu above it?

Answers (1)

Walter Roberson
Walter Roberson on 29 Nov 2018
Your callback is a function, and variables you create there get placed in the workspace of that callback function unless you specifically move them elsewhere.
You cannot really talk about "my workspace" in GUIs. There is the base workspace, and there is the global workspace. You might have a workspace of a function executing as a result of creating the GUI, but it is more typical to have that function return and to work entirely by callbacks (except when creating .exe in which case you still rely upon callbacks but you wait for the work to be done and then close down the GUI.) Most of the time, there is the workspace of the function that is currently executing and you can trace back in the call chain to a callback function that MATLAB executed when an event occured. But you that is about as far back as you can go; there is no "there" there, no workspace for the variables to be put into.
Therefore you need to share values or stash variables.
  2 Comments
Joseph Mazzella
Joseph Mazzella on 29 Nov 2018
I apologize I am refrencing sending the variables to the base workspace. The idea is to have different numbers be loaded for one variable name based upon the dropdown I want. What is the proper way to load a .mat and its variables by a callback and then share those with the base workspace
Walter Roberson
Walter Roberson on 29 Nov 2018
Example:
datastruct = load(AppropriateFileName);
assignin('base', 'CheeseProductionPerBee', datastruct.Chz_per_b);

Sign in to comment.

Categories

Find more on Scope Variables and Generate Names in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!