Creating variable name using sprintf and field of a structure

48 vues (au cours des 30 derniers jours)
Stelina
Stelina le 16 Sep 2014
Modifié(e) : Adam Cutbill le 16 Sep 2014
Hi there,
Lets say that from a previous script I have variables with sequential names for date and hour on my workspace: Cs20140312_1623, Cs20140312_1723.
Now I want to perform a series of operations without having to write the variable names manually for every loop, I just want to have a for loop for my filecounter and execute the commands for Cs, Ts for every file.
The date and time that i used to create the names were stored inside the structure myVar, in the field datehour. So I do this:
for filecounter=1:length(FileList)
k=myVar(filecounter).datehour
formatSpec='Cs%d'
Cs=sprintf(formatSpec,k)
% other commands that now will use Cs , Cs will have the values of the respective Csdatehour %
end
But the sprintf call instead of giving ex. Cs20140312_1623, returns Cs50Cs48Cs49Cs52Cs48Cs51Cs49Cs50Cs95Cs49Cs54Cs50Cs51.
This might have sth to do with how the function handles strings? but how to fix that?
Thanx!! Stelina
  1 commentaire
Stephen23
Stephen23 le 16 Sep 2014
Modifié(e) : Stephen23 le 16 Sep 2014
Actually you should look at the value k first... and reading the sprintf help will help clarify why it is doing this: " sprintf ...formats the data in arrays A1,...,An according to formatSpec in column order, and returns the results to string str."

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 16 Sep 2014

Plus de réponses (3)

Image Analyst
Image Analyst le 16 Sep 2014
I don't think this is advisable at all - having variables that have names derived from some dynamic process like time or loop index or whatever. It seems like what's discussed and advised against in the FAQ http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F I'd use an array instead.

Iain
Iain le 16 Sep 2014
I agree with the advice to NOT name dynamically name variables in a loop. It more often leads to pain than not.
You would be better with a couple of vectors/matrices/cell arrays. One for the date & time and one for the actual data.
I generally avoid sprintf type commands, I find it logically easier to deal with concatenation:
Cs = ['Cs' datestr(k, 'yyyymmdd_HHMM')];
The function "eval" lets you execute code that's dynamically generated, so:
eval(['current = ' Cs ';'])
would be the rest of what you need.

Adam Cutbill
Adam Cutbill le 16 Sep 2014
Modifié(e) : Adam Cutbill le 16 Sep 2014
I would save the workspace of your old script then load it in your new script. Use the "fieldnames" function to get the names of fields on your loaded object. You can loop using that. For example:
w=load('mymat.mat');
names=fieldnames(w);
names will contain the names of your variables. You may use
for i=1:size(names,1)
mystuff=getfield(w,names{i})
end
after that to access the individual variables in a loop without explicitly writing the names.
In other words, don't use sprintf or eval for this.

Catégories

En savoir plus sur Variables dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by