Load a Text File in a GUI
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Matt Tearle
am 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?
Akzeptierte Antwort
Andrew Newell
am 5 Mai 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.
2 Kommentare
Weitere Antworten (3)
Andrew Newell
am 23 Feb. 2011
8 Kommentare
Matt Tearle
am 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
am 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 Kommentare
Siehe auch
Kategorien
Mehr zu Spreadsheets finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!