Monday, April 16, 2012

Calculate value of symbolic equation in MATLAB

I have following MATLAB code:



>> syms x
>> z = 20.*exp(x)+cos(x)
>> derivative = diff(z)


How can I calculate value of derevative for any number, e.g. 6?



Following commands



>> clear all
>> x = 6
>> derevative = 20*exp(x)-sin(x)


works fine, but if x is symbolic I don't use x = 6 such as above example.





How to display a progress bar during loading another table view with a table view

I have a table view cell , during click it , another table view will open , you can select something as the value of the table view cell. The issue here is the data in the second table view is big and it will take long time to load. So after I click the cell , the screen will froze there which is not user friendly. I want to displaying a progress bar during load the second table view. But I can not find a good place to add that. I am wondering in which method should I add the code to display the progress bar.





Can I consolidate these two jQuery on() events with a toggle()?

There has got to be a way to do these in one toggleClass():



$('div').on("mouseenter", ".myButton", function(){
$(this).addClass('rollOver');
});
$('div').on("mouseleave", ".myButton", function(){
$(this).removeClass('rollOver');
});


...but it's within an on() because the myButton is created dynamically.





Webrick:: Access to public folders (css, js etc)

Webrick serves "/" path, but I want to have direct access to css, js and other public folders.



if I use DocumentRoot, will handle all public paths too (like css/style.css), because it hadles root path:



server = WEBrick::HTTPServer.new(
:DocumentRoot => Dir::pwd,
:Port=>8080
)


I need to mount_proc my root:



server.mount_proc('/') {|req,resp|  ...


How to give access to public folders?





Correctly send user to 404 if dynamic content is not found (ASP.NET MVC)

I have implemented 404 handling for the general case in ASP.NET MVC 3, for when a controller/view is not found. But how should it be handled inside the controller if the user is trying to access something that can't be found? For example www.foo.bar/Games/Details/randomjunk will call this inside GamesController:



public ActionResult Details(string id) // id is 'randomjunk'
{
if(DoesGameExist(id) == false)
// Now what?


I could just do a return Redirect('/Errors/Http404'); but that doesn't seem like the correct way to do it. Should you throw an exception, or something else?



We could have a special view in this case, but to start with we need a good way we can apply to several cases.



Edit: I want to show my friendly 404 page I already have for the general case.





Take picture with android camera (intent) out of memory error

I'm having two troubles with the below code. It just take picture "onclick" using intent of camera android and it display the image on the ImageView.



1) After two or three pictures without leaving the activity, it crash with an outOfMemory error often when i'm rotating the display.
2) When I take picture first time, it refresh the imageview but when i do second or third time...it doesn't refresh the picture until I rotate the screen
3) I would like to save picture on internal storage instead of external, but I don't understand how to do cause I tried several tutorial and it stucks the camera!



public class HandScryActivity extends Activity {

private static int TAKE_PICTURE = 1;
private MtgMatch myMatch;
private File handFile;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.handscry);
// Disable screen saver
getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
// Load match
myMatch = MtgMatch.getSingletonMtgMatch();
handFile = new File(Environment.getExternalStorageDirectory(), "test.jpg");
if (myMatch.getHandUri() != null) { loadPicture(); }
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
loadPicture();
}

// Handles onGame clicked buttons
public void btnHandClick(View v) {
Button clickedButton = (Button) v;
// according to clicked button
switch (clickedButton.getId()) {
case R.id.btnBackToGame:
this.finish();
break;
case R.id.btnTakePicture:
myMatch.setHandUri(Uri.fromFile(handFile));
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, myMatch.getHandUri());
startActivityForResult(intent, TAKE_PICTURE);
break;
default:
break;
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE) {
// Display image
if (resultCode == RESULT_OK) {
loadPicture();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
}

// Put the photo inside frame
private void loadPicture() {
ImageView img = (ImageView) findViewById(R.id.imgHand);
img.setImageURI(myMatch.getHandUri());
}

}




Symfony2 validator, NotBlank but allow null

I'm having trouble validating a value to allow NULL but not an empty string with the Symfony2 validator component.



I've integrated the component in a Silex application and used the Property Constraint target to validate some properties of my Application Entities (not a Doctrine Entity).



I've added this static method to my Entity class to validate name and service_id on my Entity, problem is that when service_id is NULL which should be valid the NotBlank constraint kicks in and reports a violation.



static public function loadValidatorMetadata(ClassMetadata $metadata)
{
// name should never be NULL or a blank string
$metadata->addPropertyConstraint('name', new Assert\NotNull());
$metadata->addPropertyConstraint('name', new Assert\NotBlank());

// service_id should either be a non-blank string or NULL
$metadata->addPropertyConstraint('service_id', new Assert\NotBlank());
}


Bottomline, I'm looking how to allow either a String or NULL as service_id but not allow an empty string.



PS: I've also tried the MinLength(1) constraint but that allows empty strings unfortunately.