My Tips and Tricks for C#, C, C++, MatLAB, Java, LaTeX, Python and more!
Monday, June 22, 2020
How to set CurrentCulture for all threads in a domain in .NET 4.5
In your Main-Method add the following lines:
System.Globalization.CultureInfo.DefaultThreadCurrentCulture = new System.Globalization.CultureInfo("en-US");
System.Globalization.CultureInfo.DefaultThreadCurrentUICulture = new System.Globalization.CultureInfo("en-US");
References:
morteza/how-to-set-currentculture-for-all-threads-in-a-domain-in-net-4-5
https://docs.microsoft.com/en-us/dotnet/api/system.globalization.cultureinfo?view=netcore-3.1
Sunday, June 21, 2020
Tips and Tricks with the Windows TaskManager
Because every once in a while, you need to use it!
https://old.reddit.com/r/techsupport/comments/gqb915/i_wrote_task_manager_and_i_just_remembered/
https://old.reddit.com/r/techsupport/comments/gqb915/i_wrote_task_manager_and_i_just_remembered/
Tuesday, April 28, 2020
Need <, >, & in an XML-Message? Use the XML escape characters!
For Errors like this in SoapUI?
Name cannot begin with the ' ' character, hexadecimal value 0x20.
Then please try escaping the critical characters!
Have a look here:
https://stackoverflow.com/questions/1091945/what-characters-do-i-need-to-escape-in-xml-documents
" "
' '
< <
> >
& &
https://validator.w3.org/#validate_by_input
Sunday, December 15, 2019
ORACLE: Remove Whitespaces from a string
To remove whitespaces from a string use a regex:
REGEXP_REPLACE(text,'(^[[:space:]]*|[[:space:]]*$)')
Source:
https://stackoverflow.com/
REGEXP_REPLACE(text,'(^[[:space:]]*|[[:space:]]*$)')
Source:
https://stackoverflow.com/
Wednesday, November 13, 2019
Thursday, August 1, 2019
Replace/Remove Timestamps in Files with Notepad++
Text:
31.07.2019 21:47:47 Roses are Red
31.07.2019 21:47:50 Trees are Green
31.07.2019 21:47:50 Sky is Blue
31.07.2019 21:47:50 ...and a Black Hole is lurking in the centre of every fucking Galaxy!
Find what: \d{2}.\d{2}.\d{4} \d{2}:\d{2}:\d{2}
Replace with: <what/you/like>
Replaced Text
00.00.0000 00:00:00 Roses are Red
00.00.0000 00:00:00 Trees are Green
00.00.0000 00:00:00 Sky is Blue
00.00.0000 00:00:00 ...and a Black Hole is lurking in the centre of every fucking Galaxy!
Saturday, June 8, 2019
Tutorial: How to use a compiled Matlab function in C#
In this small tutorial I will explain you:
- How to compile a Matlab function into a DLL
- Use the generated Matlab dll into a small C#-console program
Requirements:
- VisualStudio 2015 or greater
- Matlab 2018b
Creating the .Net Matlab DLL
Create a matlab-file: MatlabThreeFunc.m
With the following content:
function [x2, sinx, expx]=MatlabThreeFunc(xIn)
% [x2, sinx, expx]=MatlabThreeFunc(xIn)
% Matlab test function
x2=xIn.*xIn;
sinx = sin(xIn);
expx = exp(xIn);
end
As you see this simple function calculate the squared-, sin- and the exp- values of the input array xIn.
Navigate to the folder containing the file/function just created and execute the fololwing command:
mcc -W 'dotnet:MyMatlab.V2018b,WrapperClass,4.5,Private,local' -T link:lib ./MatlabThreeFunc.m;
This will create a .Net dll, named V2018b.dll (version 4.5 of the Framework), containing a Class named WrapperClass and located in the namespace: MyMatlab.V2018b
In my My personal convention is to use part of the namespace (V2018b) to indicate which Matlab version I used to generate the dll. This is important when distributing your application.
In my case, I want to run the application on other Computer I need to install the required Matlab Runtime 9.5 (=> https://www.mathworks.com/products/compiler/matlab-runtime.html)
Using the DLL in a C# project
Create a new Console Application:
Before you add a reference to the Just created DLL (=> V2018b.dll) it is important that you remove the VisualStudio default setting Prefer 32 Bit, otherwise you'll get a Runtime Type Exception from the Matlab dll. (=> Since Version 2015b, Matlab is ONLY 64 bit!)
Remove"Prefer 32Bit"
Add the TWO Matlab-References to your project:
- V2018b.dll
- MWArray.dll (this is located under : C:\Program Files\MATLAB\R2018b\toolbox\dotnetbuilder\bin\win64\v4.0\MWArray.dll)
And now the code:
CODE:
using System;
using MathWorks.MATLAB.NET.Arrays;
namespace MatlabFuncInCSharp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Running...");
// a basic double-array for the tests
double[] dArrayInput = new double[5] { 0.1, 0.2, 0.3, 0.4, 0.5 };
// convert the C#-array into the equivalent MatLab class
MWNumericArray matArrayInput = new MWNumericArray(dArrayInput);
Console.WriteLine("Instantiating WrapperClass...");
MyMatlab.V2018b.WrapperClass oWrapperClass = new MyMatlab.V2018b.WrapperClass();
int iExpectedNumberOfOutputs = 3; // we want ALL 3 outputs of "MatlabThreeFunc"
Console.WriteLine("Callinng MatlabThreeFunc...");
MWArray[] matOutput = oWrapperClass.MatlabThreeFunc(iExpectedNumberOfOutputs, matArrayInput);
// matOutput contains 3 elements
MWNumericArray matX2Array = (MWNumericArray)matOutput[0]; // casting required!
MWNumericArray matSinXArray = (MWNumericArray)matOutput[1]; // casting required!
MWNumericArray matExpXrray = (MWNumericArray)matOutput[2]; // casting required!
Console.WriteLine("Converting output into C#-Arrays...");
// back to the C#-world...
double[] dX2array = (double[])matX2Array.ToVector(MWArrayComponent.Real);
double[] dSinXarray = (double[])matSinXArray.ToVector(MWArrayComponent.Real);
double[] dExpXarray = (double[])matExpXrray.ToVector(MWArrayComponent.Real);
Console.WriteLine("Press any key to exit!");
Console.ReadKey();
}
}
}
I hope you found this post useful. A small video would have been wayyyyyyyyyyy better but I don't have the time for it.
References:
- Mathworks: https://www.mathworks.com/help/compiler_sdk/ml_code/mcc.html#butg_5w
- Mathworks' samples: C:\Program Files\MATLAB\R2018b\toolbox\dotnetbuilder\Examples\VS15\NET
Subscribe to:
Posts (Atom)