Open multiple text files using uigetfile and store it into a 3D array

6 views (last 30 days)
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.

Answers (1)

KSSV
KSSV on 7 Jul 2017
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

Community Treasure Hunt

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

Start Hunting!