필터 지우기
필터 지우기

Reading data from Character data

조회 수: 3 (최근 30일)
Reza Tavakoli
Reza Tavakoli 2021년 3월 19일
댓글: Reza Tavakoli 2021년 3월 19일
Hi,
I need to read a csv file from a directory. Name of the file is an output of a code which is written on a charecter or string data.
How can I write a code which reads the name of the file from character data and then find the file in the direcotory and load it.
let's say the charecter data name is 'MyFile' and name of the file that I need to read from the directory is 'File No.3'. The direcotry is a folder in one of my laptop drives. basically my code should be something like belwo but this is not working:
Data=readtable("G:\operations\'MyFile'");
Thanks for any assistance

답변 (1개)

Walter Roberson
Walter Roberson 2021년 3월 19일
projectdir = 'G:\operations';
MyFile = 'File No.3';
filename = fullfile(projectdir, MyFile);
Data = readtable(filename);
  댓글 수: 3
Walter Roberson
Walter Roberson 2021년 3월 19일
What we know is the file name is written on 'MyFile' charecter type.
I do not know what you mean by that, unless you mean that the variable named MyFile contains the file name. And if that is what you mean, then that is what my sample code already takes care of.
Do I need to show more clearly that MyFile does not have to be a fixed constant?
projectdir = 'G:\operations';
MyFile = sprintf('File No.%d', randi(9)); %file name can be computed
filename = fullfile(projectdir, MyFile);
if ~exist(filename)
error('Oh, darn, file "%s" does not exist', filename);
end
Oh, darn, file "G:\operations/File No.5" does not exist
Data = readtable(filename);
Reza Tavakoli
Reza Tavakoli 2021년 3월 19일
cheers Walter, I did a minor change to what you wrote and it is working now

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

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by