system(['C:\cygwin\bin\gawk -F, ''NR!=1 { exchNo[$2]+=1 } END { for ( id in exchNo ) print id,exchNo[id] } '' ' Filename ]);
However, when I add an if statment with > or < the Matlab system command strips this off and hence the command fails. For example the following fails in Matlab (but works directly within cygwin):
system(['C:\cygwin\bin\gawk -F, ''NR!=1 { if ($9<10) exchNo[$2]+=1 } END { for ( id in exchNo ) print id,exchNo[id] } '' ' Filename ]);
Is there a way of stopping Matlab from removing > or <?
Thanks
Well, I don't believe you.
I use system all the time with similarly complicated commands and
Matlab simply doesn't misbehave in the fashion you assert.
May I suggest that you do the following:
comm= (['C:\cygwin\bin\gawk -F, ''NR!=1 { if ($9<10) exchNo[$2]+=1 }
END { for ( id in exchNo ) print id,exchNo[id] } '' ' Filename ]
and check that what is printed out is exactly what you expect.
Then you can do this:
system(comm)
Hi,
Thanks for the suggestion. Actually this is exactly what I do but get a syntax error:
gawk: NR!=1 { if ($9 exchNo[$2]+=1 }END { for ( id in exchNo ) print id,exchNo[id] }
gawk: ^ syntax error
As you can see some text following the < is removed resulting in the error. If I eliminate the if statement then it works and if I replace < by = then this also works. So it appears that the system command is removing the <.
When I use sed, I use double quotes,char(34), not apostrophe
apostrophe, char(39) char(39).
Maybe Matlab is getting confused with a string within a string.
Hi,
Thanks Tideman you were correct. I was using ' to escape ' and this has been working fine for my applications except when I tried the conditional if statement and the awk no longer worked. By replacing two apostrophes '' (i.e. char(39)char(39)) with the double quotes " (i.e. char(34)) the awk command works.
In my search for using awk in Matlab a number of sites recommend using the two apostrophes but the example above suggests it is better to use double quotes.
So for completeness here is the correct code to count the number of unique occurrences in column 2 of a csv file (Filename) given that column 9 must be less than a value of 10:
comm= ['C:\cygwin\bin\gawk -F, "NR!=1 { if ($9<10) exchNo[$2]+=1 } END { for ( id in exchNo ) print id,exchNo[id] } " ' Filename ];
output=system(comm)
Again Thanks for your help