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

3 次查看(过去 30 天)
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

回答(1 个)

Arpit Bhatia
Arpit Bhatia 2020-10-29
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.

类别

Help CenterFile Exchange 中查找有关 Low-Level File I/O 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by