简体   繁体   English

如何使Matlab读取带有图像名称的txt文件并填充数组?

[英]How to get matlab to read a txt file with image names and populate an array?

My objective here is to read from a text file filenames for images as strings, EX: myImage.jpg. 我的目标是从文本文件中读取图像的文件名作为字符串,例如:myImage.jpg。

I have this block of code that reads how many lines are in the file. 我有这段代码,可以读取文件中的行数。

listOfImages = fopen('translate.txt', 'r');
count = 0;
%This while loop calculates the amount of lines within our text file
while ~feof(listOfImages)
    line = fgetl(listOfImages);
    if isempty(line) | strncmp(line, '%', 1)
       continue
    end
    count = count+1;
end
numberOfLines = count;

now using numberOfLines how do i put each line into some sort of array of strings using a for loop. 现在使用numberOfLines如何使用for循环将每一行放入某种类型的字符串数组中。

so, 所以,

for i = 1:numberOfLines,
    DO CODE
end

what do i put here to make it so i can read my translate.txt file row by row? 我在这里做了些什么,以便可以逐行读取我的translate.txt文件?

thanks 谢谢

Because the file names will likely be different lengths you will want to use a cell instead of a matrix. 因为文件名的长度可能不同,所以您将要使用单元格而不是矩阵。

Try the following: 请尝试以下操作:

% read the text into names, breaking on newlines
fid = fopen('translate.txt');
names = textscan(fid,'%s','delimiter','\n');
names = names{1};
fclose(fid);

for f = 1:length(names)
    disp(names(f));
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM