Read multiple data from text files and storing the full data in different variables to carry tasks later individually.

3 views (last 30 days)
I would like to read multilple files with UIGETFILE command and then read any number of text files ( sample data attached). It should read the data and store it in variables which will be equal to the number of files. The variable should contain the full data from the text file ( in table format or any form which I could carry calculations with different rows and columns)
I have used this code ( but the variable is just showing path and file name, not the data inside it.)
[filename,pathname] = uigetfile('.txt', 'Select the types of fuel to be compare','Multiselect','on');
if iscell(filename)== 0
filename = {filename};
end
numfiles = size(filename,2);
for ii = 1:numfiles
filename{ii};
entirefile{ii} =fullfile(pathname,filename{ii});
fid = fopen(entirefile{ii});
% your code
fclose(fid);
end

Answers (1)

Arpit Bhatia
Arpit Bhatia on 29 Oct 2020
Hi Amanullah,
The fullfile function is only building the complete path to the selected text files and does not return the contents of the files. To read the data inside the text files, use the fileread function as show below.
entirefile{ii} = fileread(fullfile(pathname,filename{ii}));
Using fileread does not require you to use the fopen and fclose functions and hence those lines should be deleted.

Community Treasure Hunt

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

Start Hunting!