debug matlab function called from c# - c#

I call a *.m function from C# :
matlab.Feval("shoulderDetector", 3, out result, pic, colorPicR, colorPicG, colorPicB);
When I compile, this line opens the Command Window for matlab and sends the 4 variables(All variables are from live camera) to shoulderDetector.m that calls another 3 matlab funcitions... There is an error there "matrix exceeds it indices..." Is there a way knowing what's going on with data in matlab without printing everything in the CMD matlab window ?
If not, what is the best way detecting a bug ?

Unless you want to debug in software designed for debugging Matlab code, there is no way to do it. I would put debugging output to the console in the Matlab code where you think you can catch the issue, (e.g. near the problematic matrix) and output the indexes and any other information that might be helpful.

Unfortunately there is no good solution debugging solution for Matlab when you come across different programming languages.
I reccomend to replace your function temporarily with this one:
function varargout=genericSaveArgs(varargin)
varargout=cell(nargout,1);
callArgs=varargin;
save('callArgs.mat','callArgs');
end
which saves the parameters, then call your original function using:
cc=load('callArgs.mat')
[a,b,c]=shoulderDetector(cc.callArgs{:})

Related

Serial Port: Different answers from device

I have a little problem with translating my code. I'm trying to make some relays (set on a custom made PCB) to turn on and off while I press a button.
The problem is I made the code that's doing it (so it is turning on and off depending on my actions) but I also need to read the answer from the machine. The point is I had an older version of the code written in C#, and when sending the code through that program, I get the OK answer, while using the new version of the code written in C++, I get only the first letter of the answer.
I was following this using a serial port monitor to see what the machine sends back, and I had the same result (is not like my code only reads one letter). So, the point is: the C# code is getting the full answer ("AWOK11") while the C++ code gets only the first letter of it ("A").
C# Code done in SharpDevelop and C++ code done in Qt Creator.
C++ code:
QSerialPort portverf;
portverf.setBaudRate(QSerialPort::Baud9600);
portverf.setDataBits(QSerialPort::Data8);
portverf.setParity(QSerialPort::NoParity);
portverf.setStopBits(QSerialPort::OneStop);
portverf.setFlowControl(QSerialPort::NoFlowControl);
portverf.setPortName("COM41");
portverf.open();
port.write(command);
QString result = portverf.readAll();
Thanks in advance for helping!
The method QIODevice::readAll() reads all available data from the device. If there was only an A available, it will return an A. It won't wait for additional characters. Since you read right after writing, reading only the first character is to be expected.

Issues in using DLL files

The matlab code below has been converted to a DLL file and it's being used in c# program in visual studio. While running the c# program it's showing an error as
undefined variable or function name imtool, error in trail.m
trail.m is the name of program in matlab.But when this "trail" is runned in matlab it's showing the desired output. Can you please find out a solution.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool
imtool close all; % Close all figure windows created by imtool.
The command imtool is not supported for use with MATLAB deployment products, including MATLAB Compiler and MATLAB Builder NE for .NET. Typically, most regular MATLAB and toolbox commands are supported, but not prebuilt GUIs such as imtool.
However, if the use of imtool is within a block of code surrounded by if (~isdeployed), then this shouldn't be a problem, as it will not be executed by the deployed component.
Your code snippet is incomplete, and doesn't have an end for the if. Can you confirm whether your imtool is within the if (~isdeployed) block? Or perhaps, since you're attempting to close some imtool windows, you have other uses of imtool within trail.m?

.NET 4.0 Automating/Scripting certain actions

I am a junior at university and quite new to the .NET framework.
Currently at my work in IT, there is a certain process in which an employee checks a MS office file, opens x applications, one after the other, and copies y files and waits on z conditions, one by one.
This process is quite long and tedious and very prone to human error. As such, I was wondering if .NET allows for some application to script this given sample procedure:
open a program,
input a string argument from an excel file,
get the output of the program,
paste the output of the program into another program,
get the output of the 2nd program,
open the output as a folder,
etc
The user should do as little work as possible (supplying some file paths and log-in credentials once and pressing some Start button).
If so, if someone could recommend a few good libraries/API to look at, it would be much appreciated.
Thank you for your time.
edit 1: System.Diagnostic.Process seems to not handle argument passing very well
Try using Powershell, it is much better suited to what you are trying to do. Good place to start:
http://technet.microsoft.com/en-us/library/ee221102
I think you could do that more easily with AutoIt.
.NET/c# isn't really suited for that task as others have already pointed out.

Converting a C# (Windows application) into commandline application?

I am looking to convert a C# (Windows platform application) into a commandline version.
The scenario is: I have implemented a C# (Windows application) in VS 2010. The output of this application is to generate a txt (log) file (in simple explanation).
Now the case is, there is one other application which need to use my this C# application, by calling my C# application from the command line at the run time.
My question is, how is it possible to convert an already existing C# application into commandline application, so that this C# application can be called from the calling (other) program? There is one input parameter which need to be passed on the commandline to my C# application. And then this C# application will process the data according to input parameter and then generate the output log(txt) file.
Added explanation
I am really impressed by the solutions here. Just a bit more expertise is required from readers. I want one application only to work as both commandline application as well Windows-application (forget to mention it before, sorry!), depending on the number of input parameter pass to the application. From this point of view, I have two options to implement it,
1) Make separate functions for both applications (commandline and windows-forms). Call them according to the input parameter pass. In each function implement the complete functionality of each application without disturbing (or going into the code of other application). Also I will be able to re-use 2 main functions, already built in windows-form application into my commandline application after some editing.
Disadvantage: This will make the code size nearly 50% more than case 2.
2) The second idea is same as describe by one of the expert here, to use the same application/functions for commandline as that of already built windows-form application. The only way to distinguish is to look at the input parameter pass, and decide accordingly whether to show the GUI interface or just use the commandline input (and do processing).
Disadvantage: This case will make the code bit messy and difficult to maintain/implement due to extra adding of check for number of input parameter decisions.
Which strategy should I follow for implementation?
Sure - just:
Create a new VS2010 command-line project
You'll now have a "main ()" (or, in MS-Land, "_tmain()") function instead of a root class.
Cut and paste the relevant code into "main()" (or into a function called by main (), or into a class created from main() - your choice).
Do a search-and-destroy mission to find anyplace where you're doing GUI input, and substitute command line parameters instead.
Parse your command line.
Voila! Done!
You don't have to convert it. Your application can stay as a Windows application. You simply need to handle command line arguments.
To get the command line arguments from ANYWHERE in the application, just use Environment.GetCommandLineArgs();
You want to get value from command line is not a good reason to convert winform app to console app. You may use,
string[] args = Environment.GetCommandLineArgs();
However you can change application type by opening project properties (right click on project name) and change the Output type.
Just don't show the GUI if you get paramater passed in, as when called from the other program.

Showing help for a command-line utility

I have a command-line utility that gets quite a bit of downloads from my site. I'm trying to show the usage when a user uses the /? or /help parameters. I have a function called ShowUsage() that has nicely formatted text on the parameters available.
I see that ShowUsage() gets called fine from Visual Studio 2008, when I'm using the command-line parameters in the project properties. However the exe does not display the help text when run from the command-line. Here's a shortened version of ShowUsage():
private static void ShowUsage()
{
Console.WriteLine(Environment.NewLine);
Console.WriteLine("Text File Splitter v1.4.1 released: December 14, 2008");
Console.WriteLine("Copyright (C) 2007-2008 Hector Sosa, Jr");
Console.WriteLine("http://www.systemwidgets.com");
Console.WriteLine("");
}
I tried a bunch of different things from my searches in Google, but none are working. I know this has to be something simple/easy, but for the life of me, I can't figure this one out.
EDIT:
The code that calls ShowUsage():
if (!Equals(cmdargs["help"], null) || !Equals(cmdargs["?"], null))
{
ShowUsage();
}
I have a class that parses the parameters into the cmdargs array. I confirmed that the parameters are in the array. It's something inside ShowUsage() that is preventing from showing the text.
I'm going to try the debug trick, and see what I find.
I'm not using Console.Out anywhere.
d03boy - Just personal preference. It makes the text less cluttered onscreen, at least to me.
EDIT #2
Ok, some more information... I'm running VS2008 in Vista Ultimate 64 bit. I just check the project properties and it is set to "Windows Application." This utility is 32 bit.
I'm going to experiment with creating a separate project in the solution that is a true console program, as some of you have advised.
Can you define "not working"? Simply not doing anything? Throwing an exception? I would expect the problem to be in the Main(...) method - i.e. ShowUsage() isn't being called. Another common issue is not rebuilding it in the correct configuration, so bin/release (or whatever) isn't updated.
Have you built the app as a console exe? i.e. is the "Output Type" = "Console Application" in project properties? It needs to be in order to access the console...
For info, I find the easiest way to do a console help screen (once it gets beyond a handful of lines) is to embed a .txt file into the exe; then I just write out the text file (perhaps still using string.Format if I want to replace tokes).
Alternatively, there is the alternative string format:
Console.WriteLine(#"
Text File Splitter v1.4.1 released: December 14, 2008
Copyright (C) 2007-2008 Hector Sosa, Jr
http://www.systemwidgets.com
");
Are you redirecting Console.Out to somewhere else?
Try throwing a System.Diagnostics.Debugger.Launch() in the ShowUsage method so you can see if it's getting hit at runtime.
Can you reproduce the issue with a simple exe, only accepting those help parameters?

Categories