Creating variable name using sprintf and field of a structure
50 views (last 30 days)
Show older comments
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 Comment
Stephen23
on 16 Sep 2014
Edited: Stephen23
on 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."
Accepted Answer
Stephen23
on 16 Sep 2014
Do not create or use variable names dynamically like this. Use a cell array or structure instead:
0 Comments
More Answers (3)
Image Analyst
on 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.
0 Comments
Iain
on 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.
0 Comments
Adam Cutbill
on 16 Sep 2014
Edited: Adam Cutbill
on 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.
0 Comments
See Also
Categories
Find more on Variables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!