Load a Text File in a GUI
2 views (last 30 days)
Show older comments
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 Comments
Matt Tearle
on 23 Feb 2011
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?
Accepted Answer
Andrew Newell
on 5 May 2011
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.
More Answers (3)
Andrew Newell
on 23 Feb 2011
8 Comments
Matt Tearle
on 23 Feb 2011
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
on 20 Aug 2011
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 Comments
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!