Import multiple data files (.sto) together

조회 수: 35 (최근 30일)
Sanchana Krishnakumar
Sanchana Krishnakumar 2020년 8월 5일
댓글: Sanchana Krishnakumar 2020년 8월 13일
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.

채택된 답변

Gaurav Garg
Gaurav Garg 2020년 8월 10일
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.

추가 답변 (1개)

Nipun Katyal
Nipun Katyal 2020년 8월 10일
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

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by