Load a Text File in a GUI
2 次查看(过去 30 天)
显示 更早的评论
I have a GUI that processes Excel files. I need run 100+ old files that are in .txt format. I would like to be able to load the entire file into a variable and then process it with the current code. The file could be ~8,000 rows and 6 columns with some empty cells. What's the best way to tackle this?
3 个评论
Matt Tearle
2011-2-23
I wasn't suggesting converting outside MATLAB - I was thinking of running a MATLAB script that would automatically convert all the txts into xlses.
But it seems like you'd prefer to modify your gui so that you can select either xls or txt and have it work either way. Have I got it?
采纳的回答
Andrew Newell
2011-5-5
Based on your description of the file, this code might be able to read the file:
fid = fopen('testInput.txt');
data2 = []; data6 = [];
while ~feof(fid)
tline = fgetl(fid);
nums = str2num(tline);
if length(nums)==2
data2 = [data2; nums];
elseif length(nums)==6
data6 = [data6; nums];
elseif length(nums) ~= 0
error('Invalid number of entries in row.')
end
end
fclose(fid);
This will result in two matrices, data2 for the lines with two columns and data6 for the lines with six columns.
更多回答(3 个)
Andrew Newell
2011-2-23
8 个评论
Matt Tearle
2011-2-23
Are the files all in the same format insofar as the two/six column thing is concerned? That is, is it guaranteed that the file will contain either two columns separated by spaces or six columns separated by tabs? And does it change back and forth within one file?
YOGESH
2011-8-20
as you are talking about fgetl, i came across
[tline, lt] = fgets(fid);
in this case, what is 'lt'? what should be its length?
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!