Filter löschen
Filter löschen

Comparing Values from file to cell array

2 Ansichten (letzte 30 Tage)
Haba Medhat
Haba Medhat am 29 Mai 2013
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.

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 29 Mai 2013
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

Weitere Antworten (1)

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

Kategorien

Mehr zu Characters and Strings 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!

Translated by