if statement not working

Hey all,
I am comparing data I have with a larger one in excel, but the results I am getting are not what I am looking for although I feel that my code is logic:
for i = 1:length(Normal)
for j = 1:length(Patient_ID)
if strcmp(Normal(i), Patient_ID (j)) == 1 && strcmp(Normal_dates(i), Date_Attended_Clinic (j)) == 1
[num2, txt2]= xlsread('ExcelMainExport_ALL.xls',1, sprintf('A%d:IP%d',j,j));
xlswrite('Data_Collected_Normal.xls', txt1, i, 'A1:IP1')
xlswrite('Data_Collected_Normal.xls', txt2, i,'A2:IP2')
warning off MATLAB:xlswrite:AddSheet
end
end
end
Now, what I'm getting in Data_Collected normal are the correct string names, so the first part of the if statement is achieved but the second one regarding the dates is not!! In my excel file I have redundant data but each one has a different date. Normal and Normal_Dates are 70X1 cell arrays, while Patient_ID and Date_Attended_Clinic are 2626X1 cell arrays

回答 (1 件)

Walter Roberson
Walter Roberson 2012 年 12 月 4 日

0 投票

Just as a wild hypothesis: is it possible that you need strcmpi() on the dates? If they do not happen to match upper/lower-case ?
In both cases are the dates exactly the same format? For example, it isn't the case that one of them starts 3/ and the other starts 03/ for the same date? And the order of the parts of the dates are exactly the same?
I am also wondering if you might at some point be converting from Excel binary date format to printable date format. If you are then you may need to take into account that Excel date numbers start at a different base than MATLAB dates do, and Excel date numbers have a leap-year bug for the year 1900, leading to a potential 1 day difference for dates from March 1 1900 onward. When an Excel binary date that is known to Excel as being a date, is converted by xlsread() then the resulting text field should be correct (except perhaps Jan/ Feb 1900?).

4 件のコメント

Yasmin Tamimi
Yasmin Tamimi 2012 年 12 月 4 日
編集済み: Yasmin Tamimi 2012 年 12 月 4 日
Both dates are of the same format. I am using xlsread to read the columns, so what do you suggest? How can I correct the resulting text field? For example, instead f getting WALM201162 05/03/08, I'm getting WALM201162 17/09/04. One thing I have noticed and I don't know it effect on the results, WALM201162 has two dates in excel(17/09/04 with no error sign) and (05/03/08 with an error: the cell contains date string represented with only two digits a year) although both dates have the same format but I don't know why there is an error only for the second!!
Walter Roberson
Walter Roberson 2012 年 12 月 4 日
In the Excel file, is the date being stored as string or as binary data?
Could you give an example of a date that you know to be present in both files? And could you show the numeric (first output) and text (second output) from xlsread for that date, for both files, entries that you expect to match?
Yasmin Tamimi
Yasmin Tamimi 2012 年 12 月 4 日
It has a string format, like: 05/03/08. As for the txt, when I type >> txt(:,3) ans 'Date Attended Clinic' '27/01/11' '30/06/05' This is a portion of it, I discard the first line and make my cell array start from the first date. And whan I type num([1:10],:) I get numbers and nans.
Yasmin Tamimi
Yasmin Tamimi 2012 年 12 月 4 日
編集済み: Walter Roberson 2012 年 12 月 4 日
This is the beginning of my m-file before starting with the loop:
Normal = {'AMES190356' 'BARE280757' 'BEND071036' 'BENY301040' 'BIRD131236' 'BLEW180238' 'BOCE110236' 'BRAS220657' 'BURJ200540' 'CALP110152' 'CAUJ010773' 'CAUS070772' 'CAUV250878' 'CEVW070741' 'CHAJ280753' 'CHIK190335' 'CLAJ120522' 'CLIJ290138' 'COYJ210361' 'CRAR100130' 'DERJ050348' 'DUNL250151' 'EBBP230147' 'FARH310372' 'FARL291266' 'GEED081150' 'HAYJ131146' 'HULJ110336' 'KANE260340' 'KAUB010968' 'KILD211060' 'KOSP050166' 'LEAB120651' 'LINJ081243' 'LOCK090455' 'MCAM051130' 'MCCP170542' 'MCDG090650' 'MCDJ141235' 'MCGD210743' 'MCGJ100852' 'MCKD271229' 'MILD230535' 'MYLB091123' 'NATP050545' 'PLAG310852' 'PLAH130446' 'PLUC231161' 'PREG210332' 'RITK130747' 'ROHP100640' 'ROML030944' 'SALE300349' 'SEMS170640' 'SIMG200162' 'SPOC030851' 'STEJ190439' 'TALI090561' 'TRER250550' 'WARW020140' 'WARW150559' 'WEBR170151' 'WHIT050961' 'WISS270252' 'WYLJ030834' 'ZANA230361' 'ZANV090361' 'CHAD311258' 'SCHJ300639' 'WALM201162'};
Normal_dates = {'30/10/07' '19/03/08' '10/12/07' '10/12/07' '02/05/06' '25/07/07' '19/03/08' '29/08/07' '07/05/08' '26/04/06' '13/12/07' '26/04/06' '11/12/07' '15/08/07' '07/05/08' '04/06/08' '25/10/07' '24/02/08' '28/04/06' '05/09/07' '06/02/08' '17/02/08' '05/04/05' '14/12/07' '14/11/07' '17/02/08' '30/11/05' '11/07/05' '28/05/08' '15/11/07' '13/12/07' '19/12/07' '24/02/08' '28/05/08' '131207' '05/05/08' '23/11/07' '12/12/07' '15/02/06' '07/05/08' '04/06/08' '06/02/08' '19/03/08' '17/02/08' '10/12/07' '24/02/08' '24/02/08' '22/02/08' '14/05/08' '05/03/08' '02/11/07' '14/05/08' '17/02/08' '04/06/08' '06/02/08' '23/10/07' '17/02/08' '14/11/07' '18/07/07' '06/11/07' '01/07/05' '24/02/08' '05/09/07' '03/05/06' '28/03/07' '11/12/07' '27/02/08' '05/03/08' '22/02/08' '05/03/08'};
eCAN = {'ALLJ180854' 'ARTJ240134' 'BAKC210548' 'BISP120958' 'BROF070136' 'BROJ280451' 'BROL190345' 'BUTJ220449' 'CALB100239' 'CHEI260340' 'COLV191128' 'COOB051044' 'COOV050439' 'CRAR021146' 'DELP150440' 'EARC301261' 'EBED160631' 'ELIM030340' 'FLOM301233' 'FULE180535' 'FURL090860' 'GODL070956' 'GREL270832' 'HALE200551' 'HALN171036' 'HARA270231' 'hard290737' 'HARL010141' 'HARS301146' 'JACE290825' 'JACF021040' 'JACM030935' 'JACM040143' 'JONT250947' 'KEFM110651' 'LAFW020559' 'LAUM060252' 'LOWN130639' 'MAKJ031044' 'MITD130348' 'MONS060538' 'MONS170831' 'MOOG090443' 'MULJ110167' 'NEWM210138' 'PLAK270154' 'ribj070732' 'RICB030241' 'ROHD240443' 'RYAK080229' 'SALG300349' 'SMIP280543' 'SUMA270231' 'SYMM260538' 'TREA160535' 'TURS180543' 'VASD191153' 'VONE130433' 'WAIJ090156' 'WALG210740' 'WALK051046' 'WATM261136'};
eCAN_dates = {'07/05/08' '14/05/08' '22/08/07' '31/10/07' '29/11/07' '07/11/07' '07/11/07' '22/11/07' '14/11/07' '27/06/05' '29/11/05' '27/06/05' '24/10/07' '13/11/07' '12/03/08' '05/05/08' '05/11/07' '04/12/07' '31/10/07' '29/11/07' '28/05/08' '01/11/07' '19/03/08' '14/05/08' '19/11/07' '27/06/05' '04/04/05' '11/12/07' '17/10/07' '21/11/07' '08/08/07' '07/05/08' '12/03/08' '10/12/07' '19/03/08' '23/04/08' '17/10/07' '28/05/08' '13/12/07' '24/10/07' '19/03/08' '23/05/07' '10/12/07' '04/04/07' '19/12/07' '19/03/08' '16/08/07' '14/05/08' '02/11/07' '20/11/07' '27/02/08' '08/10/08' '23/10/07' '05/11/07' '28/05/08' '05/09/07' '14/05/08' '31/10/07' '22/02/08' '17/10/07' '23/10/07' '14/11/07'};
dCAN = {'ALLR090437' 'ATHS230458' 'BARR011055' 'BEGF230742' 'CARM150957' 'COOT250933' 'DUCA291132' 'HARB310830' 'JACL110922' 'MARL090239' 'SIMD310835' 'HANK091230'};
dCAN_dates = {'10/12/07' '24/10/07' '17/02/08' '24/04/06' '12/11/07' '24/10/07' '24/10/07' '04/12/07' '21/11/07' '22/08/07' '06/11/07' '02/05/06'};
[num, txt, raw] = xlsread('ExcelMainExport_ALL_errorignored.xlsx');
[num1, txt1, raw1] = xlsread('ExcelMainExport_ALL_errorignored.xlsx',1, 'A1:IP1'); % Copy the headers to all excel sheets to compare
Patient_ID = txt([2:end],1);
Date_Attended_Clinic = txt([2:end],3);

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

タグ

質問済み:

2012 年 12 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by