Adding variables from function to workplace - Output not working

6 vues (au cours des 30 derniers jours)
Kate
Kate le 10 Juin 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
clc;
clear all;
pwd
fuf(pwd, 'detail')
fn=fuf(['/Users/katharynwoods/Documents/MATLAB/flux_sensitivity_analysis/flux_anom_scripts/flux_data/WSA/US-FR2/*daily*.mat'],'detail')
load(char(fn{1}))
%Extract variables names
pieces = regexp(fn, '\/', 'split');
p=pieces{1};
site = p{11};
q = regexp(site, '\.', 'split');
sitename = q{1};
numyr=length(fn);
year_start=str2num(q{2})
year_end=year_start+numyr-1
years=year_start:1:year_end;
fix years
v=([sitename,'.',int2str(years(1)),'.synth.daily.mat']);
eval(['load ' v])
y_tot=data_d;
clear v
for i=2:numyr
% Create file names to load
v=([sitename,'.',int2str(years(i)),'.synth.daily.mat']);
eval(['load ' v])
% Append data to full matrix
y_tot=[y_tot;data_d];
clear v
end
sitedata=y_tot;
%save
save sitedata
end

Réponse acceptée

the cyclist
the cyclist le 10 Juin 2013
Modifié(e) : the cyclist le 10 Juin 2013
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
  5 commentaires
Kate
Kate le 10 Juin 2013
Modifié(e) : Kate le 10 Juin 2013
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!
the cyclist
the cyclist le 10 Juin 2013
Modifié(e) : the cyclist le 10 Juin 2013
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: http://www.mathworks.com/help/matlab/ref/varargout.html. 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.

Connectez-vous pour commenter.

Plus de réponses (1)

Pourya Alinezhad
Pourya Alinezhad le 10 Juin 2013
Modifié(e) : Pourya Alinezhad le 10 Juin 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.
  2 commentaires
Kate
Kate le 10 Juin 2013
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!
Pourya Alinezhad
Pourya Alinezhad le 10 Juin 2013
so you can call the function with left hand variables available : like this : [out1,out2,out3,out4,out5,out6]=call_up;

Connectez-vous pour commenter.

Catégories

En savoir plus sur Workspace Variables and MAT-Files dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by