Archive for category LabVIEW & Matlab

Remove the last Line-Feed character in LabVIEW data flow

在LabVIEW中,去掉每一行末尾的换行符。

How to remove the last line-feed char in a string variable.

There’s been a discussion in the NI forum. Original post can be accessed with this link:

Remove the last Line-Feed character at the end of Array to Spreadsheet String.vi

http://forums.ni.com/t5/LabVIEW/Remove-the-last-Line-Feed-character-at-the-end-of-Array-to/td-p/257251

To summary the post, the suggested way to get rid of the ending line-feed char is using regular expression to filter the string. The referred regular expression is [\r\n]*$. This means a CRLF char (which is used as new-line symbol in Windows) at the end of a line. Use the Regular Expression block to filter out this char.

留下评论

[转][Cited]Creating C++ Shared Libraries and DLLs

使用MATLAB创建C++共享库和DLL

This is a cited article. Original version can be accessed with this link:

http://blogs.mathworks.com/loren/2011/02/03/creating-c-shared-libraries-and-dlls/

Creating C++ Shared Libraries and DLLs

Contents

A previous post demonstrated how to use MATLAB Compiler to create standalone executables. In this article, I’ll demonstrate how to use MATLAB Compiler to create C and C++ shared libraries or DLLs.

You create a shared library or DLL to add MATLAB-based functionality to an application you’re developing. You select one or more MATLAB-based functions to include in the shared library, and MATLAB Compiler generates a binary file that you link against your application. Shared libraries generated by MATLAB Compiler are compatible with the Microsoft Visual Studio development environment on Windows and with the GNU C/C++ tool suite on most UNIX platforms. The list of supported compilers changes from one release to the next; be sure to check that list before starting your project.

To see full text, please refer to the original version.

留下评论

[转][Cited]How do I create a C – shared library with MATLAB Compiler 3.0 which can be used in other projects?

使用 MATLAB Compiler 3.0 创建C共享库用于其他工程项目

This is a cited article. Original version can be accessed with this link:

http://www.mathworks.com/support/solutions/en/data/1-18CBI/index.html?solution=1-18CBI

How do I create a C – shared library with MATLAB Compiler 3.0 which can be used in other projects?

 

Subject:

How do I create a C – shared library with MATLAB Compiler 3.0 which can be used in other projects?

Problem Description:

I am using MATLAB Compiler 3.0 or earlier and am interested in creating a C-shared library for use in an IDE such as Microsoft Visual Studio 6.
I would like to create a shared library.
I would like to hand initialize code without invoking the main( ) in my MSVC (or other IDE) project.
I would like to generate the C-files that do not contain the main( ) function.
The objective is to convert the MATLAB file to a C file to be used with MSVC (or other IDE) projects.

 

To see full text please refer to the original version.

留下评论

[转][cited]凡人琐忆 – 《我和LabVIEW – 一个NI工程师的十年编程经验》

This is a cited blog site at:

http://ruanqizhen.wordpress.com/labview/

凡人琐忆 – 《我和LabVIEW – 一个NI工程师的十年编程经验》

《我和LabVIEW》内容介绍

本书特点

近几年,随着LabVIEW在中国的普及,市面上与之相关的书籍也越来越多。不过它们多以介绍LabVIEW的函数、VI的功能为主。比如,列举一个VI的功能为何,它的参数有哪些,表示什么含义等。而本书则更加侧重于介绍如何解决问题,比如针对一个具体的编程问题,本书会介绍LabVIEW中有哪些可以实现的方法,各自优缺点是什么。

本书的内容都是我在学习和使用LabVIEW过程中积累的经验。受写作时间和个人能力的限制,本书并没有覆盖LabVIEW所有细节内容和功能,也没有详细解释书中所使用到的LabVIEW自带VI或函数的参数设置及用法。不过这些内容在LabVIEW的帮助文档中均有详细介绍,所以在阅读本书时,若对某些具体的函数有疑问,可以打开LabVIEW,查阅相关的帮助文档。

http://ruanqizhen.wordpress.com/labview/

留下评论

[转][cited]用 XControl 实现面向组件的编程

This is a cited article. View original edition at:

http://ruanqizhen.wordpress.com/2006/09/05/%E7%94%A8-xcontrol-%E5%AE%9E%E7%8E%B0%E9%9D%A2%E5%90%91%E7%BB%84%E4%BB%B6%E7%9A%84%E7%BC%96%E7%A8%8B/

 
用 XControl 实现面向组件的编程

Filed under: 未分类 — ruanqizhen @ 17:58
Tags: 我和LabVIEW

    XControl 是 LabVIEW 8 中出现的新功能。关于 XControl 功能介绍和实现方法可以参见这个网址:http://zone.ni.com/devzone/conceptd.nsf/webmain/1AE46AF02D67AF468625706E006E577C
    面向组件的编程(Component Oriented Programming ,COP)技术建立在对象技术之上,它是对象技术的进一步发展。“类”这个概念仍然是组件技术中一个基础的概念,但是组件技术更核心的概念是“接口”。组件技术的主要目标是复用—粗粒度的复用,组件的核心是接口。

    LabVIEW 为我们提供了大量漂亮的控件,可以让我们非常方便地就搭建出一个程序界面来。然而,对于追求完美的用户而言,LabVIEW 提供的为数有限的控件是远远不够的。比如图1,是 LabVIEW 8.2 的一个新功能—导入导入共享库向导的界面。在它右上方有四个按钮,这四个按钮有着特殊的外观图标,在 LabVIEW 中并没有直接提供这样的按钮。要拥有这样的按钮,并保存下来以供再次使用,就只能自己来制作这样一个自定义控件。关于(用户自定义控件可以参考文章《用户自定义控件中 Control, Type Def. 和 Strict Type Def. 的区别》)


图1:LabVIEW 8.2 中 Import Shared Library 的界面

    自定义控件虽然可以定义控件的外观,但无法定义控件的行为。功能复杂一点的控件,.ctl 文件就爱莫能助了。还是以图1 为例,它的 Include Paths 控件是“一个”功能比较复杂的控件,它比 LabVIEW 自带的列表框多了编辑功能。用户添加或编辑一个路径时,这个控件要为用户在所编辑的路径项目上,显示出供编辑使用的文本框和浏览路径按钮。
    实际上这个编辑功能是由三个 LabVIEW 提供的标准控件合作完成的:一个 Listbox、一个 String 和一个 Button 控件。它们的行为(位置、是否可见,值,等等)是在程序运行时决定的:当用户选择编辑控件中某一路径时,程序就把 String 和 Button 挪到 Listbox 上需要编辑的那一项,并遮挡住 Listbox 原本的内容。这样,用户只能在 String 控件内输入内容,或者点击浏览按钮选择一个路径。编辑完成,程序把 String 控件的值写到 Listbox 上相应的项目中。

    我们虽然看不见图1 例子中的程序框图,但是可以想象,上述的一系列操作,如判断 String 和 Button 应当显示的位置;然后挪动它们、把 String 值传给 Listbox;处理用户对他们操作的消息等等,都会为这个程序添加不少复杂的代码。这些代码应该是与程序的其它部分没有任何直接关系的。但是如果把它们也写在这个界面 VI 的程序框图上,一方面影响了程序的可读性;另一方面,编程人员有可能在更改程序其它问题时,一不小心改变了这部分代码,降低了代码的安全性。
    从逻辑关系上来看,图1 中上半部分的 Listbox、String、浏览按钮以及右上方四个操作按钮,合作共同完成一个功能,与它们之外的界面控件没有什么直接关联,所以他们七个应当被作为一个整体,或者说是一个组件。这个组件需要与程序其它模块之间的接口就只是一个字符串数组—用于输入或输出一组路径。其它的数据和操作,都应当是组件私有的、外部不可见的。
    在 LabVIEW 8 之前,想分离和封装出这样一个组件是非常困难的。因为既然这七个控件都在这个 VI 的面板上,对它们的操作和相应的代码必须放在这个 VI 的程序框图上,无法与其他代码隔离开。当然也不是说绝对没有办法,比如你可以使用 sub-panel、动态注册事件等方法,强行地把它们的代码分隔开。但是这些方法既不简单直观,使用它们又可能会让程序变得更为复杂、难以阅读和维护。

    XControl 的出现终于为这个问题提供了一个比较完美的解决方案。图1 中我们提到的七个应当划分在同一组件的控件可以被制作成一个 XControl。这个 XControl 的外观就是图1 中上半部分七个控件组合在一起的样子。XControl 与用户自定义控件相比,它不仅定义了控件的外观,更重要的是,开发人员可以通过编写 LabVIEW 代码定义 XControl 的行为,这些代码是对外隐藏的。开发人员还可以定义 XControl 的属性和方法,通过 Property Node 和 Invoke Node 在程序中使用这些属性和方法。
    同样完成选取一组路径这个功能,可以有各种不同的界面。比如各种 C++ 编译器都会提供类似的功能,但它们外观各不相同。你可以利用 XControl,编写多个外观、行为大相径庭的组件。但是,只要他们的接口相同—都是一个字符串数组,用户就可以在这些组件内任意互换,选用自己喜欢的组件,而不需改动程序的任何其它部分!
    现在,XControl 仍然不太令人满意的地方是它还不支持用户自定义的事件。

    XControl 具有封装的特性。因此我在《利用 LabVIEW 工程库实现面向对象编程》一文中提到,同样可以使用 XControl 来达到面向对象的编程方法。但是 XControl 不具备继承和多态的特性。与之相比较,Library 和 LVClass 只能够把程序中的某些功能封装成模块,而涉及到包含界面的模块,就无能为力了。XControl 则非常适合制作有界面的程序组件。

参考文章:
一个 XControl 的实例
我和 LabVIEW
面向对象与面向组件小议

留下评论

[original][原创]A useful way to run custom-defined .m file within LabVIEW diagram

A useful way to run custom-defined .m file within LabVIEW diagram

如何在LabVIEW中使用自定义的Matlab .m文件

by dtm

Abstract

In most versions of LabVIEW, a legacy module called ‘MATLAB Script’, located in ‘Mathematics | Script & Formula | Script Nodes’, provides accessibility to Matlab ActiveX object. After an automated initialization to call a ‘MATLAB Command Window’, LabVIEW allows user to type and run any plausible .m script in this node. This is realized based on ActiveX interface of Matlab local server. However, maintaining and debugging .m scripts in a .vi file is extremely hard, since the text editor in VI diagram is difficult to use, and no instant help or debug notifier is offered by LabVIEW. User should always prefer writing and debugging .m files in Matlab evironment as the latter has much-easier-to-use editing experience. Also, a Script Node cannot hold a .m function, which otherwise can be defined and called by another .m function or script in Matlab environment. In this article, a plausible way is suggested to manage an active and neat link between .vi and .m files, and to call custom-defined .m functions in .vi files. Examples are provided to illustrate this measure.

MATLAB Script in LabVIEW environment

inaccurate & incompleted histories about Matlab in LabVIEW

The details about how LabVIEW allow user to type in Matlab scripts depend on the version of LabVIEW. In earlier versions than 2009 (v9.0), ‘MathScript Node’ is the preferred way. Since 2009, this module has been seperated from the main software and packed into a toolkit called ‘MathScript RT Module’, while ‘MATLAB Script Node’ became left in LabVIEW standalone.

The ‘MathScript RT Module’ is pretty hard to use, since it virtually creates a Matlab runtime library itself to support LabVIEW on .m scripts. However many fantastic Matlab features are NOT supported by this guy, for example, embedded functions within a function (because although MS-RTM supports defined functions, it doesn’t understand an ‘end’ expression to enclose a function, which is supported by Matlab since some early 2008 versions). Therefore, this article is mainly focused on the legacy version, ‘MATLAB Script Node’.

How does a .m file work in a .vi file when I click ‘Run’ in LabVIEW window?

Detailed explanation to this question can be Googled and found in NI website. Here’s an eye-candy story for you:

  1. When you double-click and open a .vi file that contains MATLAB Script Node, LabVIEW asks Windows for a Matlab local server. A seperate background process called ‘matlab.exe’ is executed, so if you have already launched Matlab by double-clicking the icon you’ll see two ‘matlab.exe’ in the task manager. The server also creates a monitoring window of which the title is ‘MATLAB Command Window’. In fact, this window can be accessed in LabVIEW’s menu ‘Tools | MATLAB Command Window’ in earlier versions; but because of the seperation of MathScript RT Module you can no longer see this item anymore in later versions.
  2. When you type Matlab scripts in MATLAB Script Node in .vi file you are basically typing in LabVIEW’s text editor. Barely any connections with Matlab occurs in this step.
  3. After you finish writing the program, you click the ‘Run’ button in LabVIEW, and the latter will send the text that you have just typed into Matlab server. In the background, an ActiveX interface is used by these two softwares as ‘data tunnel’ without your witness. The server performs its duty to parse your script and run it. It’s just like what you do in normal Matlab window (pressing F5 or clicking ‘Run’ button).
  4. When the .m program ends, Matlab collects the output (or error) data and returns it back to the interface. Then LabVIEW gets what you need (or some awesome errors :-).
  5. When the VI program ends, Matlab server will not stop immediately. It will stay in the memory until user closes the calling .vi file or just clicks the ‘x’ on its own window.
  6. If you accidentally ‘x’ed the poor ‘MATLAB Command Window’, you will receive an error running the VI, because by clicking ‘close’ on this window you make the Matlab server no longer exist. To correct this problem, you must close the calling VI and open it again. If it works, ‘MATLAB Command Window’ will appear again; otherwise, you should consider re-launching LabVIEW or restarting your computer. No tricks here, but the truth is Matlab server is just such fragile.

In one word, LabVIEW uses ActiveX ‘data tunnel’ to utilize Matlab features, in a way that user doesn’t need to care about inner connections between these two softwares. LabVIEW will work as if it has the same (actually, most) features of standalone Matlab.

To make a comparison, let’s consider MathScript RT Module. To me the story is like this: MSRTM itself performs as a Matlab server, not a fully functioned but an ‘NI version’ of Matlab server. So, in other words, you don’t even need to install Matlab if you have this toolkit. Of course, no Matlab toolboxes!

Facts about MATLAB Script Node

  • It supports limited data types of input / output – real, complex, 1D & 2D of real / complex, path, and string. You can modify data type after adding an input / output by right-clicking the variable’s name textfield.
  • It takes time to run your code. What I mean is it costs some milliseconds to transfer your code to Matlab server. Despite of command and data transfer, Matlab will cost your time as normal. But don’t be scared – it’s really just a few millis.
  • If Matlab generates an error, the node will pass it through ‘error out’ pin, which can then be read as ‘red x’ dialog pops.
  • If you are expecting to get some output in ‘MATLAB Command Window’, you won’t see any. The node will devour the text output. Figures will appear, but I don’t think it wise to pop up M figures in a VI program; instead you should consider transferring figure data to VI program and display them with a ‘picture’ indicator on the front panel.
  • Stability of the ActiveX object is unreliable. For several times I suffered from crash after long-time opening a VI (I left it open but not running, and ‘standby’-ed my laptop).

Running custom-defined M files in VI program

Limitation of MATLAB Script Node

You CAN type Scripts in LabVIEW, as the node’s name stands; but you CANNOT define functions and then call them, because LabVIEW doesn’t store your code as .m file to be looked up when some other codes call it. This is a consequence of directly utilizing Matlab features.

How to customly define functions in MATLAB Script Node?

As the above said, you can’t …. except

that you can define them in Matlab and save them to .m file!

That’s what the rest of the article will talk about.

…and just how?

Here’s your steps. I’ll explain right after.

Step 1. In Matlab environment, define your function as usual. For example, we define a function foo with the following code

function y=foo(x)
y=x+1;
end

Remember: always ‘end’-ing a function definition is a good habit.

You should save the code in ‘foo.m’.

Step 2. In LabVIEW environment, create a VI like this (figure 1)

figure 1 Note: The path is where your function ‘foo’ is located. Usually it’s your Matlab working folder.

Step 3. Run the VI and you’ll see the result.

How it works?

When Matlab server parses the code, it first adds your code’s path to its path list, then calls function ‘foo’. Since no legacy function is named ‘foo’, your path is searched for the expected .m file. Then ‘y’ gets its value and you can see the output on the front panel.

It looks stupid to add the Matlab working path into the list, since by definition it has already been listed by Matlab itself. But this is the very trick to enable you to store your M-code along with LabVIEW project. Imagine how it will be if I put a function in the project’s folder. I’ll show you by the end of this article.

Instance searching path list

The function ‘addpath’ will only cover one VI. That is, say, if you runned the code in figure 1 for once, closed the corresponding VI, created a new VI, and typed in only the second line of code, it will NOT work anymore. This is similiar to the thought of instance – Matlab server is more like a series of need-based instances rather than a life-long service. The server will reset whenever the VI stops and starts again.

Therefore adding path is required for every related VI. Fortunately, Matlab accepts re-adding a path into searching path list (see Matlab help on addpath for more details). You only need to ensure the path exists in the list, without being afraid of confrontations. So just ‘addpath’ in every MATLAB Script Node.

SubVIs make things easier

Yes, you are suggested to create subVIs for each individual m-functions. By using subVI, a Matlab-based function turns into a LabVIEW G-language-style diagram block. You can use it simply like a normal VI function. And what’s more, you don’t need to addpath in each nodes which call customly defined M functions; when the caller VI runs, a preparing subVI that actually does the adding path work is called at first, then other codes runs, using the same Matlab server instance.

A brief example

The following code is a part of my college work. The meaning doens’t matter, but the file and program structure does.

File structure

The files are listed in figure 2.

figure 2

M-files

All the M-files are stored in one folder ‘MatlabFunctionLibrary’, located in the LabVIEW project directory. The NTFS junction is created as described in another post of mine. It’s written in Chinese but the fig ures are denoted in English. The software used in here is ‘Link Shell Extension’, which can be freely got over the Internet. The junction works as if the folder is just another one in the directory, but it in fact points to another directory. It’s different from ‘shortcuts’ which only points to the icon, but not the real directory.

libmlibf.lvlib:mlibInitialize.vi

Described in figure 3. Adds working path into Matlab server’s searching path list, and calls a non-sense function defined in that folder, to check if  the path is successfully added into the list. If the string gets its desired value, Matlab server  is running normally; otherwise subsequent VIs should not run.

figure 3

libmlibf.lvlib:mlibBeamFix.vi

Described in figure 4. Does some calculations using a custom-defined function ‘mlibBeamFix’, stored in the folder defined above.

figure 4

This VI does not require the command ‘addpath’, supposing that block will be added by user in the caller VI.

libPyrocamData.lvlib:Class_PyrocamData_Beam_FixCData.vi

Described in figure 5. Performs some array computations with a Matlab function. The custom defined M function has been wrapped into a subVI in figure 4.

figure 5

GUI.lvlib:GUI_Pyrocam_Realtime_View.vi

Described in figure 6. The actual user’s front panel, which calls an M function. Here, the M function appears very similiar to a normal LabVIEW VI.

figure 6

How this VI will work

  1. When ‘Run’ button is clicked, ‘libmlibf.lvlib:mlibInitialize.vi’ is called to add working path into the searching path list.
  2. When a ‘refresh CData’ request is triggered, ‘libPyrocamData.lvlib:Class_PyrocamData_Beam_FixCData.vi’ is called to call ‘libmlibf.lvlib:mlibBeamFix.vi’ to run the M script.
  3. Matlab server parses the M script and does the calculation.
  4. Results are returned.

Conclusion

In this article, an effective way of linking LabVIEW VIs and Matlab M-files is proposed. SubVIs are designed to wrap custom-defined .m functions, which can then be called by other VIs as if they were normal VI functions.

Reference

I’m sorry to say that this article is freely-written with barely no references. It’s very late now and I’ve got very, very tired. No more energy to provide you with bunch of quotes. But you can use the few links below and Google to further your study.

MATLAB script by NI – http://zone.ni.com/reference/en-XX/help/371361E-01/gmath/matlab_script_node/

Calling the MATLAB Software from LabVIEW by NI – http://zone.ni.com/reference/en-XX/help/371361E-01/lvhowto/creating_a_matlab_script/

 

This is an original article by dtm.

– END –

2条评论