Import multiple data files (.sto) together

23 views (last 30 days)
Hi, I'm trying load multiple .sto files together and get data from them to process each of them. I used the following code to import the data, but this stores all of the data into one big struct. However I would need them into sepearate variable names in my workspace. Please help.
files = dir('*.sto');
for i=1:length(files)
filename = files(i).name;
eval('a = importdata (filename)');
out(i) = a;
end
Now the out file looks like the following image. I would like to have each of this 2560x7 data as the respective original filenames before the import in my workspace.
The files variable in the code contains all the names of the original files that are being imported. Is there a way to use the file varaible to store each value respectively instead of using out(i) and storing all of them together.
Thanks in advance.

Accepted Answer

Gaurav Garg
Gaurav Garg on 10 Aug 2020
Hi Sanchana,
You can use genvarname to construct valid variable names. In your case,
filename = genvarname ( files(i).name, who) ;
should give you a unique variable name for each file.

More Answers (1)

Nipun Katyal
Nipun Katyal on 10 Aug 2020
Although it is recommended not to allot variable names dynamically and it is better to access similar files using data stores, if you need to assign the variable names as file names this is how you can do it,
files = dir('*.sto');
for i=1:length(files)
% genvarname creates a legal identifier from the given string, here filename.
v = genvarname(files(i).name);
eval([ v ' = importdata (files(i).name)']);
end

Community Treasure Hunt

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

Start Hunting!