필터 지우기
필터 지우기

Detecting new .txt file in a folder how can i read all the files through fileread command

조회 수: 19 (최근 30일)
Let's say i have 3 .txt files in a folder given path, How can i read it all as the following is giving me the error
folder= 'C:\Users\nha4abt\Desktop\Alarm_Script'; %folder location to get the .txt file
olddir=dir([folder filesep '**/*.txt' ]); %Only check the .txt file
l=length(olddir);
nameslist={olddir.name}
y= fileread(namelist(1))

채택된 답변

Paul Shoemaker
Paul Shoemaker 2018년 8월 27일
편집: Paul Shoemaker 2018년 8월 27일
Ahmad,
Hard to say exactly what the issue is without having more information, but it looks like your "nameslist" variable is only using the file name, and is not including the full path to each file. Unless that folder containing your txt files is on your Matlab path, Matlab will not be able to find them with just the filename.
Take a look at defining nameslist as follows and see if it solves your problem.
nameslist=strcat({olddir.folder},filesep,{olddir.name}); % FULL file name, including folder
Also, it looks like you need curly braces to access your fileread command, like so:
y= fileread(nameslist{1}); % nameslist is a cell, so get text using curly braces
Paul Shoemaker
  댓글 수: 1
ARN
ARN 2018년 8월 28일
Thanks, i was having trouble with accessing the cell, which was done by curly bracket (i was using small brackets). There is no problem with folder location as it was defined on first line.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Stephen23
Stephen23 2018년 8월 28일
편집: Stephen23 2018년 8월 28일
You used the wrong kind of indexing, because if nameslist is a cell array then parentheses returns a cell array:
y = fileread(namelist(1))
^^^ parentheses!
You need to use curly braces to access the contents of a cell array:
y = fileread(namelist{1})
It is recommended to use fullfile rather than concatenate strings, e.g.:
dir(fullfile(folder,'**/*.txt'));

카테고리

Help CenterFile Exchange에서 File Name Construction에 대해 자세히 알아보기

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by