Showing posts with label Matlab. Show all posts
Showing posts with label Matlab. Show all posts

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.

communication between 3 PCs using UDP on Matlab

Years ago, I tried communication between PCs  using VC++ based on widows socket. It was not very difficult. But I never thought Matlab could also implement this. Today, I tried, it works:

For Server:
clear;
clc;
close all;

u=udp('172.21.168.xxx', 4013,'LocalPort', 14012);
fopen(u)
u1=udp('172.21.170.yyy', 4013,'LocalPort', 14013);
fopen(u1)
fwrite(u1, 1);
fwrite(u, 1);
fclose(u)
fclose(u1)

Above, '172.21.168.xxx' is the client1 ip, and 4013 is the client1 port no, while 14012 is the server port number;
'172.21.170.yyy' is the client2 ip, and 4013(any one if it is open to user) is  the client2 port no, while 14013 is the server port number;


For Client1:
u = udp('172.21.170.zzz',14012,'LocalPort',4012);
u.Timeout=1000;
fopen(u)
A = fread(u, 10);
fclose(u) 

Above, '172.21.170.zzz' is the server ip, port number is corresponded to the server port. u.Timeout=1000  means it will wait at most 1000s if it dose not receive any thing. however, as long as it recieve thedata from the server, it will jump to the next command directly.
fclose(u)  never forget to colse it when finished.

For Client2:
u = udp('172.21.170.zzz',14013,'LocalPort',4013);
u.Timeout=1000;
fopen(u)
A = fread(u, 10);
fclose(u) 

Saturday, March 2, 2013

abnormal characters when using matlab to read txt files

when I read Chinese txt files using fopen function on matlab, many abnormal characters appear while there is no problem using the same code on the other PC. I know it is because of the default system encoding and decoding setting. It should be done if you configure it through control panel.

however, you can also solve the problem from the angle of Matlab, just still use fopen func, only difference is to add one or two parameters which we seldom pay attention to.

fileID = fopen(filename, permission, machineformat, encoding)

You can choose one of the following option for the 4th parameter encoding in above fopen function according to your situation.


'Big5'         'SO-8859-1'    'windows-932'
'EUC-JP'       'ISO-8859-2'   'windows-936'
'GBK'          'ISO-8859-3'   'windows-949'
'Macintosh'    'ISO-8859-4'   'windows-950'
'Shift_JIS'    'ISO-8859-9'   'windows-1250'
'US-ASCII'     'ISO-8859-13'  'windows-1251'
'UTF-8'        'ISO-8859-15'  'windows-1252'  
                              'windows-1253' 
                              'windows-1254' 
                              'windows-1257'
For my case, I choose the last one, 'UTF-8', and it works!

Thursday, February 28, 2013

Traverse all the files under one folders using matlab and C++ on windows

I usually need to process batches of files in my work. sometimes use matlab, some times use C++:

1. For MATLAB, it is very easy:

clear;
clc;
close all;
MyDir='D:\Matlab_Code\RA_6\recent-valid\'; 
% the folder where the targeted files are stored, do not forget the '\' in the end;
AllFileName=dir([MyDir,'*.txt']);
% AllFileName it is a array whose each element is a structure with many attributes
% all the infomation about txt files would be included in AllFilename
% You can even get the numbers of files just by size the AllFileName without traversing all the files
for i=1:length(AllFileName)
     Fid=fopen([MyDir,AllFileName(i).name]); % there are many optional parameters  you can choose
     Content=fgetl(Fid);
     while ischar(Content)
          Str= Content;
          % add your code here
          Content=fgetl(Fid);% if you miss this, it could run into a dead loop
     end
     fclose(Fid);% never forget to close the file
 end



2. For C\C++ on windows, it is almost same:


#include 
#include 
#include 
using namespace std;
int main (void)
{

    _finddata_t fileDir; // it is included in io.h, much similar to the AllFileName in the matlab code

    char* dir="C:\\CZG\\Matlab_Code\\HSS\\*.m";

    // notice the end of the above line: 
    // if you write as  *.m, then only m file would be found
    // if you write as  *.m*, hen all the extention format start with m could be found, such as .mat, .m file could be found at the same time

     long lfDir;

    if((lfDir = _findfirst(dir,&fileDir))==-1l) // in case there is no m file found

       printf("No file is found\n"); 

    else{

           printf("file list:\n");

           do{

               printf("%s\n",fileDir.name);

             }

           while( _findnext( lfDir, &fileDir ) == 0 );// the common way to tranverse the files, and you can not get the number of files without tranversing files, which is different from matlab

        }

    _findclose(lfDir);  // still do not forget to close

    unsigned int i=fileDir.size;

    printf("%d\n",i);

    return 0;

}

 another method is that you can put all the file names to one txt files using system function, then you can access all the files one by one through reading this txt file.

#include <iostream>
#include  <string>
#include <fstream>
using namesapce std;
int main()
{
system("dir /a-d /b D:\\zhuanfa_singapore\\step_1\\*.* >D:\\zhuanfa_singapore\\step_2\\list.txt");
// all the file names under D:\\zhuanfa_singapore\\step_1 will be stored in
//D:\\zhuanfa_singapore\\step_2\\list.txt
my_in.open("D:\\zhuanfa_singapore\\step_2\\list.txt",ios::in);
string str2="D:\\zhuanfa_singapore\\step_1\\";
 while(!my_in.eof())
 {
     string str1;
     getline(my_in,str1);
     string str3=str2+str8;
     fstream _file;
     _file.open(str3.c_str(),ios::in);
       if(!_file)
      {
         cout<<str3<<"not exist!"<<endl;
      }
      else
      {
         cout<<str3<<"already exsit!"<<endl;

      }
 }
 my_in.close();
return 0;
}


C\C++ on linux seems a little different, i will record that in another post.