how to load multiple files from directory into an array using matlab?

11 次查看(过去 30 天)
i have a directory that includes some .mat files , i want to load all these files into an array .
i tried something like this :;
x1=load('C:\Users\me\OneDrive\Desktop\project\a\first_file.mat')
x2=load('C:\Users\me\OneDrive\Desktop\project\a\second_file.mat')
... and so on for all the files in the directory , and at the end i want to have an array such that:
arr(1)=x1 ...
how can i access the directory and load all of the files at the same time into an array ?
ps: i tried using path before and dir but then i got this error :
> error using eval , undefind function 'workspacefun' for input
> arguments of type struct
thank you in advance.
  5 个评论
molan
molan 2022-8-10
as i sayed each file is a matrix , each matrix has numbers regular numbers ,
i just want to put all of these files (which mean all of these matrixes ) into an array so it would be easier for me to have an access for them , the directory has only 11 files and there are no other files there , and the order doesn't matter at all
molan
molan 2022-8-10
编辑:molan 2022-8-10
also as i mentioned previously i did :
x1=load('C:\Users\me\OneDrive\Desktop\project\a\first_file.mat');
x2= ...
for all of the 11 .mat files ,
and then i did : matrices_struct=struct('matrix',{x1,x2 .....});
but this is not efficient because what if in the future i needed to do this to 100 files !
i just simply want to load all the files into an array , where then i can access each file using arr(i)

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2022-8-10
dinfo = dir('*.mat');
filenames = {dinfo.name};
for K = 1 : length(filenames)
temp = load(filenames{i});
FN = fieldnames(temp);
FN1 = FN{1};
thisdata = temp.(FN1);
if K == 1
all_data = thisdata;
else
all_data(:,:,K) = thisdata;
end
end
The code would be easier if they all had the same variable name (and the variable name was known.) This version of the code does not assume that they all have the same variable name. This code will not error if there is more than one variable in the file -- but if there is, then which variable is loaded might not always be the first alphabetically.
  2 个评论
molan
molan 2022-8-10
Thank you very much , but i have a question , i can only run this code when i am in the directory .. how can i run this code when i am in another directory , because let’s say i want to add another directory that has other files and i want to load them into different array , but i don’t want to write a new script for each directory
I think i can do this using path and then dir( the path) but i can’t use path now because i get the error i mentioned in the question , is there another way ?
Walter Roberson
Walter Roberson 2022-8-10
directory_to_process = 'appropriate_path_goes_here';
dinfo = dir( fullfile(directory_to_process, '*.mat') );
filenames = fullfile({dinfo.folder}, {dinfo.name});
all_data = [];
for K = 1 : length(filenames)
temp = load(filenames{i});
FN = fieldnames(temp);
FN1 = FN{1};
thisdata = temp.(FN1);
if K == 1
all_data = thisdata;
else
all_data(:,:,K) = thisdata;
end
end

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by