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

조회 수: 5 (최근 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