Comparing Values from file to cell array

2 次查看(过去 30 天)
I have set of data such as:
1893 [] C:\RADIOSPOTS\AUDIO\AUDIO\1893.WAV
2514 [] C:\RADIOSPOTS\AUDIO\AUDIO\2514.WAV
3115 Maryknoll C:\RADIOSPOTS\AUDIO\AUDIO\3115.WAV
1891 [] C:\RADIOSPOTS\AUDIO\AUDIO\1891.WAV
1890 [] C:\RADIOSPOTS\AUDIO\AUDIO\1890.WAV
3119 [] C:\RADIOSPOTS\AUDIO\AUDIO\3119.WAV
2891 Shepherd Express C:\RADIOSPOTS\AUDIO\AUDIO\2891.WAV
1893 [] C:\RADIOSPOTS\AUDIO\AUDIO\1893.WAV
I want to get all the rows that have a number starting with "3" and save that into a new array and disregard all the other rows. I started with a for loop with an if statement nested in it that would look something like this:
for cmp=1:Row
if strcmp (3, C(cmp))
PSA(cmp)=C(cmp)
else
nonpsa(cmp)=C(cmp)
end
end
where C is a 95x3 array holding all information, num is only the first column of C (holding only the numbers) and Row is the number of rows in C and num.
However, this doesn't detect the rows that start with '3' and saves only the first column of data in the "nonpsa" array size 1xRow.

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2013-5-29
f=fopen('tes1.txt')
line1=fgetl(f)
res=[];
k=0;
while ischar(line1)
if ischar(line1)
res =char(res,line1)
idx=regexp(line1,'[1-9]');
if line1(idx(1))=='3'
k=k+1
out{k,1}=line1
end
end
line1 = fgetl(f);
end
out

更多回答(1 个)

Andrei Bobrov
Andrei Bobrov 2013-5-29
f = fopen('yourfile.txt');
c = textscan(f,'%s', 'delimiter', '\n');
fclose(f);
c1 = regexp(c{:},'^3.*','match');
out = [c1{:}]';

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by