This video shows how we can use Algorithms written in matlab 'M' code can be used in .Net applications. (View the video in 'double size' resolution).
Matlab Code snippet
--------------------
function [ Price,AccruedInt ] = MyBndPrice(Yield, CouponRate, Settle,...
Maturity, Period, Basis)
[Price, AccruedInt] = bndprice(Yield, CouponRate, Settle,...
Maturity, Period, Basis);
end
.Net Code snippet
-------------------
private void btnBndPrice_Click(object sender, EventArgs e)
{
MWNumericArray yields = GetYields();
MWNumericArray coupon = new MWNumericArray(Convert.ToDouble(txtCoupon.Text) ) ;
MWNumericArray period = new MWNumericArray(cmbPeriod.SelectedIndex + 1 ) ;
MWNumericArray basis = new MWNumericArray(cmbBasis.SelectedIndex);
MWCharArray settlement = new MWCharArray(txtSettlement.Text);
MWCharArray maturity = new MWCharArray(txtMaturity.Text);
MyFinLib.MyFinLibclass finLib = new MyFinLib.MyFinLibclass();
MWArray[] result = finLib.MyBndPrice(2, yields, coupon, settlement, maturity, period, basis);
Display(result);
}
private double[] GetYields()
{
string[] yieldStr = txtYield.Text.Split(',');
double[] yields = new double[ yieldStr.Length ];
for (int i = 0; i < yieldStr.Length; i++)
yields[i] = Convert.ToDouble(yieldStr[i]);
return yields ;
}
private void Display(MWArray[] result)
{
DataTable dt = new DataTable();
dt.Columns.Add("Price");
dt.Columns.Add("Accrued Interest");
string[] price = result[0].ToString().Split('\n');
string[] accuredInt = result[1].ToString().Split('\n');
for (int i = 0; i < price.Length; i++)
{
DataRow row = dt.NewRow();
row[0] = price[i];
row[1] = accuredInt[i];
dt.Rows.Add(row);
}
dataGridView1.DataSource = dt;
}
Can I get this C# file please?
ReplyDeleteHi Mukul,
ReplyDeletetry
MessageBox.Show(string.format("Sum={0} , Avg={1}",result[0].ToString(),result[1].ToString()));
OR
put a break point on 'MWArray[] result = Proj1.MyFun(2, a, b, c);'
and run your application in debug mode (press F5) , this is the best way to know the output
Thanks a lot BaijuMax :)
ReplyDeleteI am really glad that you are maintaining this blog!
I really appreciate your concerns for the researchers all round the world who have problems in creating the standalone applications for Matlab. It is a life saver!
Hi Baiju,
ReplyDeleteI have a MATLAB program which actually runs for 10-15 minutes.
I am using VC# to call the Matlab function and everything works just great.
However the only problem is that i cant see the progress of the program ... The user clicks OK button and then he has to wait for 10-15 minutes for the results. Can i have a progress bar in the VC# application which can take intermittent outputs from the Matlab function so that i can use the data to show the progress bar.
Right now, my Matlab program takes in 5 inputs from the VC# text boxes. Then VC# calls the matlab function which calculates something and iterates for 10,000 iterations. If it was possible to return the iteration parameter from matlab to VC# after each iteration was performed then I could use this information to set the progress bar or any dynamic output ...
Can you please help me with this?
On Tue, Oct 6, 2009 at 6:13 PM, baiju max baijumax@gmail.com wrote:
ReplyDeleteyou can write overloaded constructor for your form2 and pass the required fields in the constructor when your creating instance of the class.
for example:
public partial class Form2
{
////Overloaded constructor
public Form(string form1TextBoxValue):this()
{
////////////////
}
///Default constructor
public Form()
{
Init.......blahh
}
}
///
Form2 Form2obj = new Form2(txtBox.text);
Form2obj.Show();
Cheers,
BAIJU K M
On Tue, Oct 6, 2009 at 8:31 AM, Mukul Tripathi mukul.nifft@gmail.com wrote:
Dear Baiju,
Thanks a lot for your reply... i will try this out.
I am having a new problem now:
I want to add an additional form to my application (which originally has just Form1.cs). So I created the new form named 'Form2.cs' and added to the project. Now when I click the OK button on the Form1, the Form2 is supposed to open. So, i used following lines of code:
Form2 Form2obj = new Form2();
Form2obj.Show();
And it was working fine for me.
However, when I am trying to access the values in the textboxes of Form1 within the Form2obj, I cannot do that.
When I am trying to create a new object of Form1 from within the Form2, i loose the values that i entered in the Form1.
within the form2 class definition when i write
Form1 form1obj =new Form1();
then a new object is created and i loose the original values that i entered in the form1.
My question was that is it possible to create global objects for both Form1 and Form2 so that when I fill in the values in either form1 or in form2 they can by accessed from both simultaneously?
If you find this email confusing then lemme know and i will send you screen shots and the code that i am using.
Thanks always,
Mukul
On Mon, Oct 5, 2009 at 9:04 PM, baiju max baijumax@gmail.com wrote:
Hi Mukul,
Sorry for delay in reply , (I'm so busy with office assignments)
Could you try below solution in between
1) write the progress that you want to report to a text file from matlab algo.
2) use the 'FileSystemWatcher' class's onChanged event to read the text file (to which progress is written.)
[for details read http://www.c-sharpcorner.com/UploadFile/mokhtarb2005/FSWatcherMB12052005063103AM/FSWatcherMB.aspx]
In nutshell, whenever you modify the txt file matlab , 'Onchnaged' event will fire and you will be able to proper of the same from vc#.
cheers
BAIJU K M
On Sun, Oct 4, 2009 at 9:59 AM, Mukul Tripathi mukul.nifft@gmail.com wrote:
Thanks a lot Baiju! I will wait for your response...
Thanks for helping me out always!
On Sat, Oct 3, 2009 at 11:25 PM, baiju max baijumax@gmail.com wrote:
We can do that by raising events from matlab and handling the same from vc#.
I will get back to you with samples.
cheers
BAIJU K M
Hi Mukul,
ReplyDeleteuse waitbar to show the progress.
for example , convert below .m file to dotnet assembly using deploytool and try from VC# .
StartProgram.M
----------------
function StartProgram(numIterations)
h = waitbar(0,'Please wait...');
//simulating a long running program.
for n=1:numIterations
waitbar(n/numIterations)
pause(2);
end
close(h)
end
Thanks a lot Baiju, I will implement it and keep you updated.
ReplyDeleteThe waitbar works awesome for me, Baiju! Tons of thanks for the help!
ReplyDeleteI was using a Pi Chart as the output to simulate the waitbar till now.
So this problem is solved for me, waitbar looks more professional so I am using it :)
Hello Baiju, I have two more questions for you...
ReplyDeleteQ1. I am using an excel file which i read and write from a fixed location in my harddisk, and i can do it perfectly fine. But I have to save the file at one particular location always which i do not want. Can i have an open file dialog from where i can choose the excel file
which i want to work on? (like its in any windows application ...from where we click open and choose the file we want to open)
i looked for fileopendialog but I am not sure how to use it.
Q.2 I also want to pass on this information to matlab too... can we open an open file dialogue in matlab ?
Excel.Workbook xlsBook = xlsApp.Workbooks.Open(@"C:\Users\Mukul\data.xls", ...);
this is what i am using right now
In short: can you give me an example, how to replace @"C:\Users\Mukul\data.xls with the user input and then pass on the same information to matlab function so that matlab also opens the same file.
Q1 Ans:- check this article http://www.statisthink.com/makale.aspx?makaleid=7eb6a105-649a-4e50-8d9e-4c9d48b69faa
ReplyDeleteQ2 Ans:- a) add an extra (file name) parameter in Matlab function. (And pass the filename from VC# to Matlab) .
b) To open file dialog box from matlab try this command 'uigetfile'
Thanks Baiju!
ReplyDeleteAlso, as an answer to Q1. I found this useful : http://msdn.microsoft.com/en-us/library/aa984392%28VS.71%29.aspx
Hi Baiju,
ReplyDeleteToday, I have two questions for you:
1. I successfully opened an excel file using openfiledialog in VC# and could read and write in it. Now I want to pass the same file-name from VC# to MATLAB so that I can open the same excel file that i selected from VC# from within MATLAB and do further processing.
2. I have returned a double dimensional array as output from a matlab function and I am trying to display it into a textbox in VC#. But I am unable to refer to the values within the array.
Say for example if i return only one value from the MATLAB function as 'result' and i want to display its content in the textbox i can use following:
textbox1.Text=result[0].ToString();
However if variable 'result' is the output from the MATLAB function and it is '2 by 2' array and i have to display all the four elements. Then using usual array notation the first element of the array should be:
textbox1.Text=result[0][0].ToString();
but its not letting me do this. Am i missing something?
Hi,
ReplyDeleteI am getting the error message below...am I missing something here...I've added the necessary references
The type or namespace name 'MyFinLibclass' does not exist in the namespace 'MyFinLib' (are you missing an assembly reference?)
I tried creating a web service from your exact code and got "The type initializer for threw an exception." errors. Any ideas? (Visual Studio 2010 Framework 4.0)
ReplyDelete