Open multiple text files using uigetfile and store it into a 3D array
6 次查看(过去 30 天)
显示 更早的评论
I asked a question not too long ago and got this code which is correct so far.
[FileName,PathName] = uigetfile('*.txt', 'Open text file');
file = load(fullfile(PathName,FileName));
How can I open 3 text files (files contain matrixes) and store it into a 3D array variable, for example file(:, :, i)? I have thought about using a loop to do this but I can't figure out how to write it.
0 个评论
回答(1 个)
KSSV
2017-7-7
There is an option for selecting multiple files in uigetfile. You need to switch it on. While selcting the multiple files, you need to press ctrl key.
Note that, as you are loading the data of files into 3D matrix, you should be knowing the dimensions of data already and the dimensions of the matrices in all the files should be same. Check the below code:
[FileName,PathName] = uigetfile('*.txt', 'Open text file','MultiSelect','on');
data = zeros(nx,ny,length(FileName)) ; % where nx*ny is the size of each matrix in the files selected
for i = 1:length(FileName)
file = load(fullfile(PathName,FileName{i}));
data(:,:,i) = file ;
end
I suggest you to save the data of files into a cell instead of matrix, in this case you need not to know the dimensions of the data present in text files.
[FileName,PathName] = uigetfile('*.txt', 'Open text file','MultiSelect','on');
data = cell(length(FileName),1) ;
for i = 1:length(FileName)
file = load(fullfile(PathName,FileName{i}));
data{i} = file ;
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!