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

Thread Subject:
naming output based on parameters passed to function

Subject: naming output based on parameters passed to function

From: Kirk

Date: 5 Aug, 2009 15:32:01

Message: 1 of 4

I have a bit of code wrapped in a loop which pulls in different parameter files and re-runs the model. My problem is that the model then saves the output to a struct, which contains two generically named elements. In order to plot the output I have to load each struct into the workspace and manually re-name each element as to not overwrite the previously loaded element.

What I need are suggestions on how to dynamically name struc elements based on the parameter file names used in the specific model run.

Here is a simplified example of what I am talking about:

% Load workspace and run model
load detroitlakes_mc_workspace
[out]=pnetcn(climIn,siteIn,pineVeg);

% Build structs
climateMeans_.tmax=tmax_mean;
climateMeans_.tmin=tmin_mean;
climateMeans_.par=par_mean;
climateMeans_.prec=prec_mean;
climateMeans_.CO2=CO2_mean;

out_.folprodcmo_mean=folprodcmo_mean;
out_.folmass_mean=folmass_mean;
out_.netpsnmo_mean=netpsnmo_mean;
out_.netcbalmo_mean=netcbalmo_mean;

% Save output
save ../../_output/northcentral/dl/pine_1xs.mat climateMeans_ out_

What I want to be able to do is save the elements climateMeans_ and out_ with a name based on the parameters passed to model in [out]=pnetcn(climIn,siteIn,pineVeg);
climIn, siteIn, pineIn

Any suggestions would be appreciated.

Subject: naming output based on parameters passed to function

From: Oleg Komarov

Date: 5 Aug, 2009 16:10:05

Message: 2 of 4

"Kirk" <kwythers.nospam@umn.edu> wrote in message <h5c8lh$78o$1@fred.mathworks.com>...
> I have a bit of code wrapped in a loop which pulls in different parameter files and re-runs the model. My problem is that the model then saves the output to a struct, which contains two generically named elements. In order to plot the output I have to load each struct into the workspace and manually re-name each element as to not overwrite the previously loaded element.
>
> What I need are suggestions on how to dynamically name struc elements based on the parameter file names used in the specific model run.
>
> Here is a simplified example of what I am talking about:
>
> % Load workspace and run model
> load detroitlakes_mc_workspace
> [out]=pnetcn(climIn,siteIn,pineVeg);
>
> % Build structs
> climateMeans_.tmax=tmax_mean;
> climateMeans_.tmin=tmin_mean;
> climateMeans_.par=par_mean;
> climateMeans_.prec=prec_mean;
> climateMeans_.CO2=CO2_mean;
>
> out_.folprodcmo_mean=folprodcmo_mean;
> out_.folmass_mean=folmass_mean;
> out_.netpsnmo_mean=netpsnmo_mean;
> out_.netcbalmo_mean=netcbalmo_mean;
>
> % Save output
> save ../../_output/northcentral/dl/pine_1xs.mat climateMeans_ out_
>
> What I want to be able to do is save the elements climateMeans_ and out_ with a name based on the parameters passed to model in [out]=pnetcn(climIn,siteIn,pineVeg);
> climIn, siteIn, pineIn
>
> Any suggestions would be appreciated.
not really clear what u want, try to simplify to a basilar scheme what u need.
oleg

Subject: naming output based on parameters passed to function

From: Donn Shull

Date: 5 Aug, 2009 16:19:03

Message: 3 of 4

"Kirk" <kwythers.nospam@umn.edu> wrote in message <h5c8lh$78o$1@fred.mathworks.com>...
> I have a bit of code wrapped in a loop which pulls in different parameter files and re-runs the model. My problem is that the model then saves the output to a struct, which contains two generically named elements. In order to plot the output I have to load each struct into the workspace and manually re-name each element as to not overwrite the previously loaded element.
>
> What I need are suggestions on how to dynamically name struc elements based on the parameter file names used in the specific model run.
>
> Here is a simplified example of what I am talking about:
>
> % Load workspace and run model
> load detroitlakes_mc_workspace
> [out]=pnetcn(climIn,siteIn,pineVeg);
>
> % Build structs
> climateMeans_.tmax=tmax_mean;
> climateMeans_.tmin=tmin_mean;
> climateMeans_.par=par_mean;
> climateMeans_.prec=prec_mean;
> climateMeans_.CO2=CO2_mean;
>
> out_.folprodcmo_mean=folprodcmo_mean;
> out_.folmass_mean=folmass_mean;
> out_.netpsnmo_mean=netpsnmo_mean;
> out_.netcbalmo_mean=netcbalmo_mean;
>
> % Save output
> save ../../_output/northcentral/dl/pine_1xs.mat climateMeans_ out_
>
> What I want to be able to do is save the elements climateMeans_ and out_ with a name based on the parameters passed to model in [out]=pnetcn(climIn,siteIn,pineVeg);
> climIn, siteIn, pineIn
>
> Any suggestions would be appreciated.

I am not quite sure I have understood what you want to do, but you can get the names of variables passed into a function with inputname ie:

climIn_name = inputname(1);

and you can use eval to assign it a value ie:

eval([inputname(1), ' = out_;'])

hope this helps,

Donn

Subject: naming output based on parameters passed to function

From: Loren Shure

Date: 13 Aug, 2009 12:49:35

Message: 4 of 4

In article <h5c8lh$78o$1@fred.mathworks.com>, kwythers.nospam@umn.edu
says...
> I have a bit of code wrapped in a loop which pulls in different parameter files and re-runs the model. My problem is that the model then saves the output to a struct, which contains two generically named elements. In order to plot the output I have to load each struct into the workspace and manually re-name each element as to not overwrite the previously loaded element.
>
> What I need are suggestions on how to dynamically name struc elements based on the parameter file names used in the specific model run.
>
> Here is a simplified example of what I am talking about:
>
> % Load workspace and run model
> load detroitlakes_mc_workspace
> [out]=pnetcn(climIn,siteIn,pineVeg);
>
> % Build structs
> climateMeans_.tmax=tmax_mean;
> climateMeans_.tmin=tmin_mean;
> climateMeans_.par=par_mean;
> climateMeans_.prec=prec_mean;
> climateMeans_.CO2=CO2_mean;
>
> out_.folprodcmo_mean=folprodcmo_mean;
> out_.folmass_mean=folmass_mean;
> out_.netpsnmo_mean=netpsnmo_mean;
> out_.netcbalmo_mean=netcbalmo_mean;
>
> % Save output
> save ../../_output/northcentral/dl/pine_1xs.mat climateMeans_ out_
>
> What I want to be able to do is save the elements climateMeans_ and out_ with a name based on the parameters passed to model in [out]=pnetcn(climIn,siteIn,pineVeg);
> climIn, siteIn, pineIn
>
> Any suggestions would be appreciated.
>

Do you know about dynamic field names? If you have a struct S, you can
add fields like this:

newFieldName = 'siteIn';
currentvalue = 3;
S.([newFieldName, intstr(currentvalue)]) = something...

and S will have a field named

siteIn3

That might help you out.


--
Loren
http://blogs.mathworks.com/loren

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us