How to get the output variable name in the load() in matlab?

17 views (last 30 days)
I want to load an ascii-file using this syntax:
load('10-May-data.dat').
The returned output variable name should be X10_May_data.
Is there a way to get the variable name in matlab?
If I want to use regular expression to do the translation, how can I do it? For example, put an X before any underscores or digits in filename and replaces any other non alphabetic characters with underscores.
Thanks

Answers (2)

Walter Roberson
Walter Roberson on 3 Feb 2016
genvarname(regexprep('10-May-data', '-', '_'))
But you should not be using dynamically generated names as the names of variables.

Steven Lord
Steven Lord on 3 Feb 2016
The easiest and safest way is to call LOAD with an output argument so you know at the time you write your code what the variable name will be.
mydata = load('10-May-data.dat');

Community Treasure Hunt

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

Start Hunting!