dlmread filename not a character vector

조회 수: 3 (최근 30일)
Mike Hodny
Mike Hodny 2018년 2월 15일
댓글: Bob Thompson 2018년 2월 15일
Hello, I am having problems using dlm read within a for loop. I would like to define a directory my files are in, the basic name of my files, a sequence of numbers at the end of my filename, and the file extension. An example of the filenames is as follows.
folder1/folder2/folder3/filename_000.txt
folder1/folder2/folder3/filename_001.txt
folder1/folder2/folder3/filename_002.txt
This continues up to n files. I would then like to read each file and take out specific data, hence the dlmread function.
What I have is as follows:
file_directory = 'folder1/folder2/folder3/';
file_prefix = 'filename_';
%My numbers 000, 001, 002, 003, etc. are all contained within an array called: A
file_suffix = '.txt';
for i=1:n %n for the number of files I want to load
filename = [file_directory file_prefix A(i) file_suffix];
temp=dlmread(filename ,'', [17 0 3664 1]);
end
The code runs until temp=dlmread... wherein it gets an error of, "Filename must be a character vector."
Any way on how to fix this?
  댓글 수: 1
Bob Thompson
Bob Thompson 2018년 2월 15일
I would try defining the file name as:
filename = strcat('folder1/folder2/folder3/filename_',num2str(A(i)),'.txt');
or
filename = sprintf('folder1/folder2/folder3/filename_%d.txt',A(i));
This will eliminate the need to define the directory and such ahead of time.
Alternatively you can try to use the uigetfile() command. I personally use this option more as it allows me a little more freedom as the script user, and when combined with the directory using fullfile() I can give the directory and all through a basic for loop.
[filenames,filedir] = uigetfile('*.txt','','Multiselect','on')
filepath = fullfile(filedir,filenames);
for i = 1:n
temp = dlmread(filepath(i),'',[17 0 3664 1]);
end

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

채택된 답변

Mike Hodny
Mike Hodny 2018년 2월 15일
I figured out the problem. The working for loop is as follows:
for j=0:files
filename = [file_folder file_prefix A{1,j+1} file_suffix];
temp=dlmread(filename,'', [17 0 3664 1]);
end

추가 답변 (1개)

Walter Roberson
Walter Roberson 2018년 2월 15일
Use fullfile and sprintf together. For the numeric field use a %03d format specification in sprintf

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by