Load a Text File in a GUI
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Matt Tearle
il 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?
Risposta accettata
Andrew Newell
il 5 Mag 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 Commenti
Più risposte (3)
Andrew Newell
il 23 Feb 2011
8 Commenti
Matt Tearle
il 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
il 20 Ago 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 Commenti
Vedere anche
Categorie
Scopri di più su Spreadsheets in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!