Solved

Matlab and C/C++ communication program

Posted on 2011-02-25
4
2,342 Views
Last Modified: 2016-03-02
hi there

I need recommendation for following scenario;

I have to write a program which  communicates between C/C++ and Matlab. C/C++ will act as server and matlab program shoul act as server. to be more specfic C/C++ program will generate
the numbers and send to the Matlab side and matlab will simultaneously plot these data.

best regards.
0
Comment
Question by:loopfinity
  • 3
4 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 34985419
I had asked this question at work, and I was told that Matlab supports TCP communication:
     http://www.mathworks.com/products/instrument/hardware/tcpip.html
and that I would have to purchase Mathworks' Instrument Control Toolbox option.

According to this link, Matlab can collect and plot data from a TCPIP server in real time.
0
 
LVL 6

Author Comment

by:loopfinity
ID: 34996940
Ok. thanks for links. but how can I integrated with C.
My C program will generate the numbers and write them on text files.
Matlab will interpret these numbers to plot.


could you please give solid example.
0
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
ID: 34998396
I thought you were sending the data via TCP/IP, your zone selection.
here is an m-file that communicates with a server via TCP/IP:


% MATLAB example using TCP/IP (matlab_tcpip_example.m)
% This simple code example demonstrates how you can use MATLAB to exchange data 
% with a remote application not developed in MATLAB. This code example is taken
% from a MATLAB Digest technical article written by Edward J. Mayhew from 
% George Mason University.  While HTTP was used as the higher-level protocol in 
% this example, you can use other protocols, as was the case in the project. 
% MATLAB supports TCP/IP using Instrument Control Toolbox.  Requires MATLAB and 
% Instrument Control Toolbox.  
%
% On line 14, substitute "www.EXAMPLE_WEBSITE.com" with an actual website with
% which you wish to communicate.

% Create TCP/IP object 't'. Specify server machine and port number. 
t = tcpip('www.EXAMPLE_WEBSITE.com', 80); 

% Set size of receiving buffer, if needed. 
set(t, 'InputBufferSize', 30000); 

% Open connection to the server. 
fopen(t); 

% Transmit data to the server (or a request for data from the server). 
fprintf(t, 'GET /'); 

% Pause for the communication delay, if needed. 
pause(1) 

% Receive lines of data from server 
while (get(t, 'BytesAvailable') > 0) 
t.BytesAvailable 
DataReceived = fscanf(t) 
end 

% Disconnect and clean up the server connection. 
fclose(t); 
delete(t); 
clear t

Open in new window

0
 
LVL 32

Expert Comment

by:phoffric
ID: 34998542
>> My C program will generate the numbers and write them on text files.
Then if your matlab can access the files directly, then you can use fscanf to read the files:
     http://www.mathworks.com/help/techdoc/ref/fscanf.html
(has code examples to read file)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Creating an OSPF network that automatically (dynamically) reroutes network traffic over other connections to prevent network downtime.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now