Code covered by the BSD License  

Highlights from
putvar, uigetvar

5.0

5.0 | 3 ratings Rate this file 48 Downloads (last 30 days) File Size: 4.47 KB File ID: #27106

putvar, uigetvar

by

 

27 Mar 2010 (Updated )

Move (get or put) variable(s) directly between a function workspace and the base workspace

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information
Description

Sometimes I want to work with a variable from the base workspace, but inside a function. This problem arose for me when I wrote a gui tool, that occasionally would need a variable from the base workspace. I realized a tool was needed to put up a dialogue box that would list all of the variables in the base work space, and allow you to choose one to bring into the function workspace.

uigetvar is such a tool. It allows you to specify a list of classes to show in the uigetvar box as a restriction so if you only wish to see the 'char' class variables, you can easily do so.

Once I wrote uigetvar, it became logical to define the counterpart operator, putvar. The putvar tool allows you to assign a variable from a function workspace directly into the base workspace. I can envision this tool having value in one of two ways.

First, during a debugging session, one might wish to save one or more variables from the function workspace into the base workspace. This way those variables will still exist after the debugger has terminated and you have exited from the function.

A second use for this tool is as an alternative way to return a variable (or variables) from a function, possibly a GUI, during the operation of that GUI.

As an example of the operation of this code, here is a simple function that uses putvar. Note that you can provide either a variable in the workspace as input, or the name itself of the variable. Either mode of operation works.

% ==================
function testputvar
A = 3;
B = 23;
C = pi/2;
D = 'The quick brown fox jumps over the lazy dog';
putvar(A,'C',D)
% ==================

Save the function testputvar.m on your search path.

Then at the command line in MATLAB, try this. First, clear your workspace. Clear ensures that no variables exist initially in the base workspace. Then run the function testputvar, and finally execute the who command, all at the command line.

>> clear
>> testputvar
>> who

Your variables are:
A C D

The output from who tells it all. Inside the function testputvar, we had defined four variables, A, B, C, and D. But after testputvar terminates, its variables will fall into the bit bucket, disappearing into limbo. The putvar call inside testputvar ensures that the variables A, C, and D are returned into the base workspace, yet no return arguments were provided.

In the event that you wanted to assign all of the variables from the caller workspace, just do this inside your function...

W = who;
putvar(W{:})

I had considered wrapping a GUI around putvar, probably called uiputvar, but I don't see much benefit there, whereas the uigetvar tool truly does need to be a GUI.

Acknowledgements

This file inspired Caller2base.

MATLAB release MATLAB 7.10 (R2010a)
Other requirements There is nothing in this tool that prevents it from being used by older versions of MATLAB.
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (3)
26 Nov 2012 Fabian  
26 Apr 2012 Tamanaco  
31 Oct 2010 Benjamin

I used putvar and it is perfect. Right now I am calling it once the workspace variable has been filled and that seems to work fine. About putvar, are there any issues with assigning different types of variables, for instance adding single quotes to the variable like in the example ('C').

Thanks for the great tool.

Updates
25 Jan 2012

Included uigetvar

Contact us