簡體   English   中英

如何使Matlab讀取帶有圖像名稱的txt文件並填充數組?

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

我的目標是從文本文件中讀取圖像的文件名作為字符串,例如:myImage.jpg。

我有這段代碼,可以讀取文件中的行數。

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;

現在使用numberOfLines如何使用for循環將每一行放入某種類型的字符串數組中。

所以,

for i = 1:numberOfLines,
    DO CODE
end

我在這里做了些什么,以便可以逐行讀取我的translate.txt文件?

謝謝

因為文件名的長度可能不同,所以您將要使用單元格而不是矩陣。

請嘗試以下操作:

% 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