Problem with for loop and if

I created the following code. The code will open 4 file.txt, extract the value and do some check. The first check is to verify if on the val{1,1}{5,1} ~= 'LIMA'. The problem is that If LIMA isn't on the txt file the programme works fine but when is on the txt file i don't have any output.
Please help me. :-)
fileList = dir( '*.txt' );
for i = 1 : numel( fileList )
nameFile{i} = fileList(i, 1).name;
NAME = char( nameFile(i) );
fid = fopen( NAME );
val (i) = textscan( fid, '%s', 'delimiter', ',' );
fclose( fid );
if val{1,1}{5,1} ~= 'LIMA' %| val{1,1}{6,1} == 'VOCI'
for j = 8 : size(val{1,1},1 )
A(i,j)=str2num( val{1,i}{j,1} );
% end
end
end
end

1 commentaire

Jan
Jan le 27 Oct 2011
@Maurizio: Please post your questions here instead of sending me emails. This forum lives from public questions and answers.
Have you read my profile?!

Connectez-vous pour commenter.

Réponses (1)

Jan
Jan le 27 Oct 2011

1 vote

Do not use the == operator to compare strings. The result is an elementwise comparison. Use STRCMP instead.
You import the file data to "val(i)", but check "val{1,1}" in each iteration.
fileList = dir('*.txt');
nameFile = cell(1, numel(fileList)); % Pre-allocate!
for i = 1:numel(fileList)
NAME = fileList(i).name; % [EDITED]
nameFile{i} = NAME;
fid = fopen(NAME);
valC = textscan(fid, '%s', 'delimiter', ',');
val = valC{1};
fclose(fid);
if strcmp(val{5,1}, 'LIMA') == 0
for j = 8:size(val, 1)
A(i, j) = str2num(val{j, 1});
end
end
end

8 commentaires

Maurizio
Maurizio le 27 Oct 2011
Jan I don't know why is not working.
the txt file are:
1.txt is
A6,ANB,AUG23,204704,OMDC,VOCI,0404,0790,0468,05330,01420,14800,08550,0780
2.txt is
A6,ANA,AUG21,194704,LIMA,LIRA,0404,0790,0168,0540,01500,14800,08800,0780
3.txt is
A6,ANA,AUG22,192704,OMSR,VWCI,0104,1190,0118,0500,01512,14450,0650,0280
Can you help me please?
Jan
Jan le 27 Oct 2011
@Maurizio: Please explain, what "is not working" means exactly. Do you get an error message - in which line and which message? Or does the result differ from your expectations? Are you talking about your program or my one?
Maurizio
Maurizio le 27 Oct 2011
I'm talking about your programme..
??? Error using ==> textscan
First input can not be empty.
Error in ==> jan at 7
valC = textscan(fid, '%s', 'delimiter', ',');
Jan
Jan le 27 Oct 2011
Oh, well, I made a mistake at defining NAME. You can find such problems by your own using the debugger.
Maurizio
Maurizio le 27 Oct 2011
Thanks a lot Jan, is it possible instead of give only one strin Value (e.g. 'LIMA' more value? For example Can I create array or something with all the name and than do the strcmp?
Jan
Jan le 27 Oct 2011
Your can use:
any(strcmp(val{5,1}, {'LIMA', 'PRIMA', 'PUMA'}))
or
s = val{5,1}; if strcmp(s, 'LIMA') || strcmp(s, 'PRIMA') || strcmp(s, 'PUMA')
Maurizio
Maurizio le 28 Oct 2011
is not possible instead of put all the name create an array with all the name (LIMA,PRIMA,PUMa and than do a for loop to check?
Jan
Jan le 28 Oct 2011
Of course you can use a FOR loop also. But the shown "any(strcmp(String, Cell-String))" is nicer.

Connectez-vous pour commenter.

Question posée :

le 27 Oct 2011

Community Treasure Hunt

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

Start Hunting!

Translated by