Load a Text File in a GUI

2 ビュー (過去 30 日間)
Daniel
Daniel 2011 年 2 月 23 日
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 件のコメント
Daniel
Daniel 2011 年 2 月 23 日
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
Matt Tearle
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
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.
  2 件のコメント
Daniel
Daniel 2011 年 5 月 17 日
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
Andrew Newell
Andrew Newell 2011 年 5 月 17 日
Congratulations!

サインインしてコメントする。

その他の回答 (3 件)

Andrew Newell
Andrew Newell 2011 年 2 月 23 日
Try xlsread.
EDIT: Simply
A = importdata(filename)
might do the trick.
  8 件のコメント
Matt Tearle
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?
Daniel
Daniel 2011 年 4 月 28 日
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!

サインインしてコメントする。


Daniel
Daniel 2011 年 4 月 28 日
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 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?

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by