MATLAB Answers


Adding variables from function to workplace - Output not working

Asked by Kate
on 10 Jun 2013

Hi there,

I'm trying to add the variables within this function to my workplace so that I can use them in subsequent functions. I don't quite understand why it isn't doing this (I've added it as an output in the function line). Any ideas?

Thanks a bunch!

 function [site, numyr, year_start, year_end, years, sitedata]=call_up
%Loading in one site-year and appending data to single matrix for multiple
% years.
%load files
clear all;
fuf(pwd, 'detail')
%Extract variables names
pieces = regexp(fn, '\/', 'split');
site = p{11};
q = regexp(site, '\.', 'split');
sitename = q{1};
% fix years
eval(['load ' v])
clear v
for i=2:numyr
 %  Create file names to load
    eval(['load ' v])
    % Append data to full matrix
      clear v
save sitedata


2 Answers

Answer by the cyclist
on 10 Jun 2013
Edited by the cyclist
on 10 Jun 2013
 Accepted answer

When you call the function from the workspace, do you include the output functions there as well, such as

[site, numyr, year_start, year_end, years, sitedata] = call_up


What you pasted here is presumably from inside your function file.

I am talking about how you call the function from the calling workspace. If you do not also call the function with the outputs specified on the left side, then the function will only return the first output.

Thanks a bunch, sorry I mis-read that. That code was from my function, not my workspace.

Is there no syntax to have all of the variables added to the workspace without specifically writing them out?

I really appreciate your guidance!

There are multiple ways of getting lots of variables out of a function without needing to explicitly list them all. One way is to use the "varargout" functionality: Another would be to put the variables in a structure, and then output the structure. Another (but slow and probably dumb) way would be to write the variables to disk, and then load them from disk again. Another solution mentioned global variables, but as you point out, that is probably not going to work for you.

Answer by Pourya Alinezhad on 10 Jun 2013
Edited by Pourya Alinezhad on 10 Jun 2013

hi, well,we know that functions don't share variables with workspace,if you want to use variables in several functions you can declare them as GLOBAL variables in all functions.for example write "global x" in 2 functions and you will see that this variable is the same in both. you can also use "Persistence " to make the variable hold it's past value from function's last call.

bear in mind that if you return a variable from a function to workspace,the another function can't see them in workspace.


Right, I understand that variables in a function only exist there unless told otherwise. My understanding is that setting an output would take those variables from my function to my workspace.

I can't set them as global or persistent because this code is designed to read thousands of sites and years. If I set variables as global they would interfere with subsequent runs I believe.

Thanks though!

so you can call the function with left hand variables available : like this : [out1,out2,out3,out4,out5,out6]=call_up;

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!