The who
function returns the names of variables in matlab. It also has a built-in regexp for selecting certain items:
X10_May_data = [1 2 3];
save X10_May_data.mat X10_May_data
clear
load X10_May_data.mat
w = who('-regexp','X*')
w =
'X10_May_data'
You can then operate on w{1}
to do any substitutions you want. For example, use the strrep
function for simple modifications of a string:
newvar = strrep(w{1},'May','latest')
newvar =
X10_latest_data
For more complex modifications, use regexp
or regexprep
. When you have the new name, you can assign it with eval
:
eval([newvar '=' w{1}]) % like typing "X10_latest_data = X10_May_data"
X10_latest_data =
1 2 3
[edit] PS I agree with the comments that eval
is usually a bad idea; but sometimes you just need to get something done :) For alternative approaches, see the matlab page on the topic.