Sunday, March 3, 2013

one 'interface' between C and matlab

One friend ask me how to realize the mixed coding using matlab and C, because, for one of his proj, he want to finish some task using C and after that he want to call matlab to do another task (related to matrix processing)as soon as possible, while the C code and matlab code should run all the time.

I do know there are many posts on how to implement the interface between C and matlab, it should configure something on matlab with many steps, sometimes it also depends on the version of matlab.
this way is feasible, but i do not like  the configuration job, so i have anothe idea

Considering his requirement is not so demanding, general situation is that:
repeat: task 1 on C--->task 2 on matlab
          task 1 on C-->task 2 on matlab...

so I come up with an solution: send a signal to matlab as long as task1 on C is finished.

For, C\C++, java, matlab, python, PHP ,C#,it is very easy to create one txt file  no matter whether this txt file already exists or not.

So, we can create one txt file with the same filename as long as the task on C is done, and we can also choose the way of generating this file, we just ovewrite this file instead create a new one if txt file already exist.

then, we can judge this file is a new one or old one through its date attribute which would lead to the task 2, on matlab, we can write this:

   time_old='0';
   time_new='0';
   while(1)
      filename=dir(['D:\Matlab_Code\','*.txt']); % 
       num_bmp=length(filename);%     
       if num_bmp==1% we suppose there are at most one 1 txt file at one time     
         time_new=filename.date;
            if ~strcmp(time_new,time_old)
             % add task 2 code here
               time_old=time_new;% do not forget this command;
            end
      end

   end
For simple task, I think this way could work well. For evry demanding task, you 'd better try to use only C, in fact, there are already many libraries developed using C for matrix processing, you can just download, configure and call them.

No comments:

Post a Comment