Load a Text File in a GUI

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

I don't understand how the first three sentences relate, here. Do you want to batch convert txt files to xls so you can run the existing GUI code on them? If you read them as text into MATLAB variables, how would you process them using the GUI code -- wouldn't the GUI code include loading the data from xls files? Can you explain this a bit more please? Thx
Daniel
Daniel on 23 Feb 2011
I don't want to batch convert all the files outside of MATLAB and then open them as Excel files in my GUI. I would like to open my GUI and load a single text file(.txt) at a time. I would like all the data from the file to be stored in a single variable. The code I have already written would then be able to process the variable.
Basically, my GUI...
- Currently processes .xls files
- I need to process .txt files
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?

Sign in to comment.

 Accepted Answer

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 Comments

Thanks for the comment... I incorporated some of the advice I got and came up with the following code... It works! :)
fid1 = fopen(fname);
ok=0;num=[0,0,0,0];
list_spike=zeros(5,2);list_step=zeros(5,2);list_gate=zeros(5,2);tnum=zeros(5,1);
m1=0;m2=0;m3=0; m=0;
num(1)= 16;
num(2)= 1;
num(3)= 17;
num(4)= 18;
fprintf(fid1,'\n');
while ~feof(fid1),
line=fgets(fid1);
line1=[]; line2=[];
channelstr=findstr(line,'CHANNEL');
spike_channel=findstr(line,num2str(num(1)));
step_channel=findstr(line,num2str(num(3)));
gate_channel=findstr(line,num2str(num(4)));
if isempty(step_channel),step_channel=0; end
if isempty(gate_channel),gate_channel=0; end
if isempty(channelstr),channelstr=0; end
if isempty(spike_channel),spike_channel=0; end
if m==0
if channelstr>0 && spike_channel>0;
ok=1;
end
if channelstr>0 && step_channel>0;
ok=2;
end
if channelstr>0 && gate_channel>0;
ok=3;
end
end
if length(line)>18 && ok>0
p=findstr(line,' ');
line1=line(1:p(1));
line2=line(p(2):p(3));
switch ok
case 1
if str2num(line2) == num(2)
m1=m1+1;m=1;
spike(m1,1)=str2num(line1);
cluster(m1,1)=str2num(line2);
end
case 2
m2=m2+1;
time(m2,1)=str2num(line1);
step(m2,1)=str2num(line2);
m=1;
case 3
m3=m3+1;
list_gate(m3,1)=str2num(line1);
list_gate(m3,2)=str2num(line2);
m=1;
end
end
r = length(list_gate);
for i4=1:r;
if list_gate(i4,2)==gtsch;
start1(1,1)=list_gate(i4,1);
elseif list_gate(i4,2)==gtech;
end1(1,1)=list_gate(i4,1);
end
end
if length(line)<5 && m==1, m=0; ok=0; end
end
fclose(fid1);
else
%Error
end
Congratulations!

Sign in to comment.

More Answers (3)

Try xlsread.
EDIT: Simply
A = importdata(filename)
might do the trick.

8 Comments

Daniel
Daniel on 23 Feb 2011
I currently use xlsread to open Excel files in my GUI. I need to open Text files. I have tried textread and textscan but haven't been successful.
What do you mean you haven't been successful? Error or unexpected result?
Daniel
Daniel on 23 Feb 2011
I get errors or it only loads some of the values. I think part of the problem could be that some rows have two columns and some rows have six columns. MATLAB doesn't seem to like the different number of columns. The other problem is... In the two column rows, the columns are separated by spaces. In the six column rows, the columns are separated by tabs.
Daniel
Daniel on 23 Feb 2011
That creates a 1x1 struct. It contains the string and number contained in te first line but does not contain any additional information.
The result depends on the file format. You could try setting the delimiter to a character that is not in your files, for example, A = importdata(filename,';'). Another option is to read in a line at a time using FGETL.
Daniel
Daniel on 23 Feb 2011
I feel that the file format is bad but it was established before I joined the lab.
If I use a delimiter that is not in my file I get a single column but all of the rows. I would then have to convert the single column to multiple columns. This is tough (for me) because there are different sig figs in diffirent rows and it changes from file to file. Any ideas?
The FGETL is an interesting idea. I had not looked at that before. I'll give it a shot. I hope it doesn't mind empty rows and files of different lengths.
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?
All the files I've worked with are in the two/six column format. It changes back and forth within one file. There are empty rows between some of the two/six column changes.
Sorry about the delayed response, I had family health issues that took priority. Thank you for your help!

Sign in to comment.

Daniel
Daniel on 28 Apr 2011

0 votes

I tried using 'fgetl.' I was able to display all the lines of my text file using the code from the Help file. However, I had two problems. First, I had trouble storing the information to a variable. I ended up only being able to store the last line. Second, the entire row of information was squished into one cell (column). It seems promising... Any help is greatly apprecited. Thanks!
YOGESH
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?

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!