Thursday, July 8, 2010

Compiling Matlab mpgwrite with VS2010 for 64 bit Windows

I was having trouble producing Matlab movies of reasonable quality on my 64 bit Windows 7 laptop until I was able to compile and run mpgwrite (source available from Matlab Central http://www.mathworks.com/matlabcentral/fileexchange/309-mpgwrite).

It wasn't obvious how to get this to compile and run so in case someone else would like to do this I've documented what I did:

1. Ensure Visual Studio 2010 installation includes the "X64 Compilers and Tools" (and option under the Visual C++ feature in the Visual Studio 2010 installer).

2. Configure the mex script for VS2010.
Matlab R2009b and earlier doesn't directly support Visual Studio 2010 but you can still use it by running
mex -setup
from Matlab.
a. Answer n to the first question (Would you like mex to locate installed compilers [y]/n? )
b. Select "Microsoft Visual C++ 2008 SP1 " (option 5)
c. The script will output:
The default location for Microsoft Visual C++ 2008 SP1 compilers is
C:\Program Files (x86)\Microsoft Visual Studio 9.0,
but that directory does not exist on this machine.
Use C:\Program Files (x86)\Microsoft Visual Studio 9.0 anyway [y]/n?

Answer n
d. Locate your Visual Studio 2010 installation folder and enter this in response to the next question. Visual Studio 2010 was probably installed to C:\Program Files (x86)\Microsoft Visual Studio 10.0
e. Finally, you will be asked to confirm the settings and Matlab will create a file mexopts.bat with the configuration information.

3. From Microsoft Visual Studio 2010\Visual Studio Tools open "Visual Studio x64 Win64 Command prompt (2010)".

4. Change to the src folder of the downloaded the mpgwrite source code.

5. Edit the file makefile and replace $(MCC) $(MCFLAGS) after the call to mex with
-v -DWIN32
That is, your makefile should contain:

mpgread:
mex -v -DWIN32 mpgwrite.c mfwddct.c \
postdct.c huff.c bitio.c mheaders.c iframe.c \
pframe.c bframe.c psearch.c bsearch.c block.c \
mpeg.c subsampl.c jrevdct.c frame.c fsize.c

5. Run
nmake -f makefile

6. This should generate the file
mpgwrite.mexw64
Copy this file to a folder on the matlab path.

You should now be able to generate mpeg movies from matlab.


% Mpeg generation example
clear F;
fig=figure;
t = (0:1000)./10;
h = line( ...
'color','b', ...
'LineStyle','-', ...
'erase','xor', ...
'LineWidth',0.5, ...
'xdata',t,'ydata',[]);
for k=1:100
set(h,'ydata', sin(t-k).*t )
drawnow
F(k) = getframe(gca);
end
mpgwrite(F,F(1).colormap,'sine.mpg')


Friday, July 2, 2010

Outlook 2010 fails to retrieve email from pop3 server

After upgrading from Outlook 2007 to Outlook 2010 we were unable to connect to pop servers on port 110.

A network monitoring utility (SmartSniff) showed that Outlook 2010 starts it's communication with a pop3 server by issuing a CAPA command. This command is supposed to retrieve a list of the server capabilities but when issued on the our network the TCP connection is immediately dropped when CAPA is sent. This can be seen by starting a telnet session with the pop server and sending the CAPA command.

Further investigation showed that Cisco routers can be configured to block invalid pop3 commands and that the router considers CAPA to be an invalid command.

The Cisco router has probably been configured to do this by running a variation of the following:

ip inspect name inspection-name pop3 [alert {on | off}] [audit-trail {on | off}] [reset] [secure-login] [timeout number]


We should just need to turn off the inspection of pop3 packets to resolve this problem.

Thursday, November 19, 2009

Calling a service from a silverlight application

I was working on a Silverlight application that I intended to use on my GoDaddy hosted site and having trouble with calls to my WCF service (hosted on the same GoDaddy site).

Eventually, I found the following to work:

Uri GetServiceUri(String serviceName)
{
String fullPath = HtmlPage.Document.DocumentUri.ToString();
int index = fullPath.LastIndexOf('/');
Uri uri = new Uri(fullPath.Substring(0, index + 1) +
serviceName);
return uri;
}


Service2Client CreateService2Client()
{
return new Service2Client(new BasicHttpBinding(),
new EndpointAddress(GetServiceUri("Service2.svc")));
}



Friday, October 30, 2009

WPF binding to a fast changing data source

I was using wpf data binding to display data in a real time monitoring application and found the rate at which the data source was changing resulted in a difficult to read control.

Josh Smith has a good article on ways to reduce the frequency of binding updates and the option of calling UpdateTarget on the controls binding expression was the most appropriate solution for my application. The WPF user controls that I was applying this to had a large number of controls and so I needed a way of finding the binding expressions for all the controls.

Philipp Sumi has a function (FindChildren) to find all child control of a specified type which I used to locate and subsequently update all relevant binding expressions for the control type I was using (TextBlock)

public partial class BindingSample : UserControl

    {
        List<BindingExpression> _bindingExpressions = new List<BindingExpression>();

        public BindingSample()
        {
            InitializeComponent();

            foreach (var tb in this.FindChildren<TextBlock>() )
            {
                BindingExpression be = tb.GetBindingExpression(TextBlock.TextProperty);
                if (be != null)
                    _bindingExpressions.Add(be);
            }
        }

        // Force binding update 
        // NB: Not using OneWay binding because updates are too frequent
        //     An alternative would be to use the converter UpdateThresholdConverter
        //     but this would not necessarily result in the last value being displayed when updates stop.
        public void UpdateTarget()
        {
            _bindingExpressions.ForEach(e => e.UpdateTarget());
        }
    }

XAML

            <StackPanel Style="{StaticResource DisplayLabelValue}">
                <Label Style="{StaticResource DisplayTitle}">Speed</Label>
                <TextBlock Style="{StaticResource DisplayValue}" Text="{Binding Path=Speed, Mode=OneTime, StringFormat=F1}"/>
            </StackPanel>

Source code for FindChildren (by Philipp Sumi)

Wednesday, July 22, 2009

Creating a LIB file for a DLL

I was trying to use a DLL that was supplied with a LIB file that Visual Studio 2008 refused to link to. To get around this I needed to create a new LIB file from the DLL.
Microsoft has instructions to do this (http://support.microsoft.com/kb/131313) but this wasn't working for me. In the end I found the following worked for me.

1. dumpbin /export Filename.dll (do this from a Visual Studio Command prompt)
2. Use the output of dumpbin to produce a file Filename.def (with the functions listed by dumpbin in the EXPORTS section). I found that I needed to edit to remove all leading underscores on the exported functions.
3. lib /def:filename.def
4. This will produce 2 files, filename.lib & filename.exp.
You should now be able to link to this file.

Tuesday, June 16, 2009

Entity Framework & SQL Server CE problem

The SQL Server Compact Edition throws a very unhelpful SqlCeException exception with the message:

"A parameter is missing. [ Parameter ordinal = 1 ]"

when using the entity framwork SaveChanges() method if you set a decimal property to a value with more decimal places than the database column has been configured to store.

For example, if you have a database table "header" with a Numeric 18/3 column "Height" and execute the following code, the "A parameter is missing" will be thrown.

  EntityFrameworkSQLCEEntities entities = new
EntityFrameworkSQLCEEntities();
            
  Header header = Header.CreateHeader(Guid.NewGuid(), "Test");
  header.Height = Convert.ToDecimal(3.4555);
  //header.Height = Decimal.Round(Convert.ToDecimal(3.4555), 3);
  entities.AddToHeader(header);

  entities.SaveChanges();

Rounding the decimal to the supported number of decimal places prior to setting the property "fixes" this problem but couldn't this be better handled by the Entity Framework and/or SQL Server Compact Edition?



App.Config DataDirectory macro

By default, the |DataDirectory| macro is substituted at run-time with the folder that contains the exe.  You can override this default by changing the setting the "DataDirectory" property on the app domain as follows:

   AppDomain.CurrentDomain.SetData("DataDirectory", thePath );