0

How can I convert this script into a MATLAB function?

clc;
clear all;

%# part 1

set1 = {'AA00XY_2','BB74XY_2','CC92XZ_2','FF03VV_2'};
set2 = {'AA00XY_1','BB74XY_1','CC92XZ_1','FF03VV_1'};

matrices = cell(1,16);  %# An empty cell array
fileString = 'C:\Users\Documents\MATLAB\matrice_';  

    fileData = load([fileString set1{1} '.mat']);  %# Load file
    matrices{1} = fileData.(['m_' set1{1}]);       %# Get matrix

    fileData = load([fileString set1{2} '.mat']);  %# Load file
    matrices{2} = fileData.(['m_' set1{2}]);       %# Get matrix

    fileData = load([fileString set1{3} '.mat']);  %# Load file
    matrices{3} = fileData.(['m_' set1{3}]);       %# Get matrix

    fileData = load([fileString set1{4} '.mat']);  %# Load file
    matrices{4} = fileData.(['m_' set1{4}]);       %# Get matrix

      fileData = load([fileString set2{1} '.mat']);  %# Load file
      matrices{5} = fileData.(['m_' set2{1}]);       %# Get matrix

      fileData = load([fileString set2{2} '.mat']);  %# Load file
      matrices{6} = fileData.(['m_' set2{2}]);       %# Get matrix

      fileData = load([fileString set2{3} '.mat']);  %# Load file
      matrices{7} = fileData.(['m_' set2{3}]);       %# Get matrix

      fileData = load([fileString set2{4} '.mat']);  %# Load file
      matrices{8} = fileData.(['m_' set2{4}]);       %# Get matrix

      fileData = load('C:\Users\Documents\MATLAB\matrice_initial_2.mat');  %# Load file

      matrices{9} = fileData.A;
      matrices{10} = fileData.B;
      matrices{11} = fileData.C;
      matrices{12}(1)=0;
      matrices{13}(1)=0;
      matrices{14}(1)=0;

      matrices{15} = fileData.D;
      matrices{16} = 0;

     [A,B,C,D] =    A002A_1_Filtre(matrices{:});

        fileName = 'C:\Users\Documents\MATLAB\matrice_A002A_2.mat';  %# Save file name
        save(fileName,'A','B','C','D');                  %# Save the data

clc;
clear all;

%# part 2

set1 = {'AA00XY_2','BB74XY_2','CC92XZ_2','FF03VV_2'};
set2 = {'AA00XY_2','BB74XY_2','CC92XZ_2','FF03VV_2'};

matrices = cell(1,16);  %# An empty cell array
fileString = 'C:\Users\Documents\MATLAB\matrice_';  

    fileData = load([fileString set1{1} '.mat']);  %# Load file
    matrices{1} = fileData.(['m_' set1{1}]);       %# Get matrix

    fileData = load([fileString set1{2} '.mat']);  %# Load file
    matrices{2} = fileData.(['m_' set1{2}]);       %# Get matrix

    fileData = load([fileString set1{3} '.mat']);  %# Load file
    matrices{3} = fileData.(['m_' set1{3}]);       %# Get matrix

    fileData = load([fileString set1{4} '.mat']);  %# Load file
    matrices{4} = fileData.(['m_' set1{4}]);       %# Get matrix

      fileData = load([fileString set2{1} '.mat']);  %# Load file
      matrices{5} = fileData.(['m_' set2{1}]);       %# Get matrix

      fileData = load([fileString set2{2} '.mat']);  %# Load file
      matrices{6} = fileData.(['m_' set2{2}]);       %# Get matrix

      fileData = load([fileString set2{3} '.mat']);  %# Load file
      matrices{7} = fileData.(['m_' set2{3}]);       %# Get matrix

      fileData = load([fileString set2{4} '.mat']);  %# Load file
      matrices{8} = fileData.(['m_' set2{4}]);       %# Get matrix

      fileData = load('C:\Users\Documents\MATLAB\matrice_initial_2.mat');  %# Load file

      matrices{9} = fileData.A;
      matrices{10} = fileData.B;
      matrices{11} = fileData.C;
      matrices{15} = fileData.D;

      fileData = load('C:\Users\Documents\MATLAB\matrice_A002A_2.mat');  %# Load file

      matrices{12}=fileData.A;
      matrices{13}=fileData.B;
      matrices{14}=fileData.C;

      matrices{16} = fileData.D;

     [A,B,C,D] = A002A_2_Filtre(matrices{:});

        fileName = 'C:\Users\Documents\MATLAB\matrice_A002A_2.mat';  %# Save file name
        save(fileName,'A','B','C','D');                  %# Save the data*


clc;
clear all;

%# part 3


set1 = {'AA00XY_2','BB74XY_2','CC92XZ_2','FF03VV_2'};
set2 = {'AA00XY_3','BB74XY_3','CC92XZ_3','FF03VV_3'};



matrices = cell(1,16);  %# An empty cell array
fileString = 'C:\Users\Documents\MATLAB\matrice_';  %# First part   of                                  
                                                       %#   file name


fileData = load([fileString set1{1} '.mat']);  %# Load file
matrices{1} = fileData.(['m_' set1{1}]);       %# Get matrix

fileData = load([fileString set1{2} '.mat']);  %# Load file
matrices{2} = fileData.(['m_' set1{2}]);       %# Get matrix

fileData = load([fileString set1{3} '.mat']);  %# Load file
matrices{3} = fileData.(['m_' set1{3}]);       %# Get matrix

fileData = load([fileString set1{4} '.mat']);  %# Load file
matrices{4} = fileData.(['m_' set1{4}]);       %# Get matrix


fileData = load([fileString set2{1} '.mat']);  %# Load file
matrices{5} = fileData.(['m_' set2{1}]);       %# Get matrix

fileData = load([fileString set2{2} '.mat']);  %# Load file
matrices{6} = fileData.(['m_' set2{2}]);       %# Get matrix

fileData = load([fileString set2{3} '.mat']);  %# Load file
matrices{7} = fileData.(['m_' set2{3}]);       %# Get matrix

fileData = load([fileString set2{4} '.mat']);  %# Load file
matrices{8} = fileData.(['m_' set2{4}]);       %# Get matrix


fileData = load('C:\Users\Documents\MATLAB\matrice_initial_2.mat');  %# Load file

matrices{9} = fileData.A;
matrices{10} = fileData.B;
matrices{11} = fileData.C;
matrices{15} = fileData.D;

fileData = load('C:\Users\Documents\MATLAB\matrice_A002A_2.mat');  %# Load file

matrices{12}=fileData.A;
matrices{13}=fileData.B;
matrices{14}=fileData.C;

matrices{16} = fileData.D;


[A,B,C,D] = A002A_3_Filtre(matrices{:});

  fileName = 'C:\Users\Documents\MATLAB\matrice_A002A_2.mat';  %# Save file name
  save(fileName,'A','B','C','D');                  %# Save the data



clc;
clear all;
%# part 4

set1 = {'AA00XY_2','BB74XY_2','CC92XZ_2','FF03VV_2'};
set2 = {'AA00XY_4','BB74XY_4','CC92XZ_4','FF03VV_4'};



matrices = cell(1,16);  %# An empty cell array
fileString = 'C:\Users\Documents\MATLAB\matrice_';  %# First part  of                                  
                                                       %#   file name


fileData = load([fileString set1{1} '.mat']);  %# Load file
matrices{1} = fileData.(['m_' set1{1}]);       %# Get matrix

fileData = load([fileString set1{2} '.mat']);  %# Load file
matrices{2} = fileData.(['m_' set1{2}]);       %# Get matrix

fileData = load([fileString set1{3} '.mat']);  %# Load file
matrices{3} = fileData.(['m_' set1{3}]);       %# Get matrix

fileData = load([fileString set1{4} '.mat']);  %# Load file
matrices{4} = fileData.(['m_' set1{4}]);       %# Get matrix


fileData = load([fileString set2{1} '.mat']);  %# Load file
matrices{5} = fileData.(['m_' set2{1}]);       %# Get matrix

fileData = load([fileString set2{2} '.mat']);  %# Load file
matrices{6} = fileData.(['m_' set2{2}]);       %# Get matrix

fileData = load([fileString set2{3} '.mat']);  %# Load file
matrices{7} = fileData.(['m_' set2{3}]);       %# Get matrix

fileData = load([fileString set2{4} '.mat']);  %# Load file
matrices{8} = fileData.(['m_' set2{4}]);       %# Get matrix


fileData = load('C:\Users\Documents\MATLAB\matrice_initial_2.mat');  %# Load file

matrices{9} = fileData.A;
matrices{10} = fileData.B;
matrices{11} = fileData.C;
matrices{15} = fileData.D;

fileData = load('C:\Users\Documents\MATLAB\matrice_A002A_2.mat');  %# Load file

matrices{12}=fileData.A;
matrices{13}=fileData.B;
matrices{14}=fileData.C;

matrices{16} = fileData.D;


[A,B,C,D] = A002A_4_Filtre(matrices{:});

  fileName = 'C:\Users\Documents\MATLAB\matrice_A002A_2.mat';  %# Save file name
  save(fileName,'A','B','C','D');                  %# Save the data

Actually I do processing on large data files, then to avoid the error 'out of memory ', I split each file into 4 parts and I use at the beginning of each stage 'clear all'. So, what I want is to have a function as

A002A_2 = function(AA00XY,BB74XY,CC92XZ,FF03VV,initial_2,A002A_1_Filtre,A002A_2_Filtre,A002A_3_Filtre,A002A_4_Filtre)

And, My problem is that I have to write the same script for other data files. So, is there a way to build a function where I can just change the file names AA00XY,BB74XY,CC92XZ,FF03VV,initial_2 and the name of the function used A002A_1_Filtre,A002A_2_Filtre,A002A_3_Filtre and A002A_4_Filtre for the sub-processing to get on the last step the file "matrice_A002A_2.mat"

NB: I want to transform the 4 parts of my script in a single function!!!

Thank you for your help.

#

After Natan answer, I made ​​the following changes:

    clc;
    clear all;

    A = 0;
    B = 0;
    C = 0;
    D = 0;

    fileName = 'C:\Users\Documents\MATLAB\matrice_A002A_2.mat';  %# Save file name
    save(fileName,'A','B','C','D');                  %# Save the data

    for n=1:4
        set1 = {'AA00XY_2','BB74XY_2','CC92XZ_2','FF03VV_2'};
        set2 = {['AA00XY_' num2str(n)]  ,['BB74XY_' num2str(n)]  ,['CC92XZ_' num2str(n)]  ,['FF03VV_' num2str(n)]};


    matrices = cell(1,16);  %# An empty cell array
    fileString = 'C:\Users\Documents\MATLAB\matrice_';  %# First part of                                  
                                                               %#   file name


      fileData = load([fileString set1{1} '.mat']);  %# Load file
      matrices{1} = fileData.(['m_' set1{1}]);       %# Get matrix

      fileData = load([fileString set1{2} '.mat']);  %# Load file
      matrices{2} = fileData.(['m_' set1{2}]);       %# Get matrix

      fileData = load([fileString set1{3} '.mat']);  %# Load file
      matrices{3} = fileData.(['m_' set1{3}]);       %# Get matrix

      fileData = load([fileString set1{4} '.mat']);  %# Load file
      matrices{4} = fileData.(['m_' set1{4}]);       %# Get matrix


        fileData = load([fileString set2{1} '.mat']);  %# Load file
        matrices{5} = fileData.(['m_' set2{1}]);       %# Get matrix

        fileData = load([fileString set2{2} '.mat']);  %# Load file
        matrices{6} = fileData.(['m_' set2{2}]);       %# Get matrix

        fileData = load([fileString set2{3} '.mat']);  %# Load file
        matrices{7} = fileData.(['m_' set2{3}]);       %# Get matrix

        fileData = load([fileString set2{4} '.mat']);  %# Load file
        matrices{8} = fileData.(['m_' set2{4}]);       %# Get matrix


        fileData = load('C:\Users\Documents\MATLAB\matrice_initial_2.mat');  %# Load file

        matrices{9} = fileData.A;
        matrices{10} = fileData.B;
        matrices{11} = fileData.C;
        matrices{15} = fileData.D;

        fileData = load('C:\Users\Documents\MATLAB\matrice_A002A_2.mat');  %# Load file

        matrices{12}=fileData.A;
        matrices{13}=fileData.B;
        matrices{14}=fileData.C;

        matrices{16} = fileData.D;


       [A,B,C,D] =  eval(['A002A_' num2str(n) '_Filtre(matrices{:});']); 

        fileName = 'C:\Users\Documents\MATLAB\matrice_A002A_2.mat';  %# Save file name
        save(fileName,'A','B','C','D');                  %# Save the data

    end
Daniel Heilper
  • 1,182
  • 2
  • 17
  • 34
bzak
  • 563
  • 1
  • 8
  • 21
  • Can you clarify? Converting a script to a function is a very simple act in Matlab, so I don't think that's all you're having a problem with. But I'm having trouble deciphering what you're having a hard time with. – TTT May 23 '14 at 18:52
  • my problem is that I have to write the same script a hundred times, and each time I have new data to be included (AA00XY ',' BB74XY ',' CC92XZ ',' FF03VV are arrays of new data). A002A_2 is just a name given to the final Result. So I want a smarter way to do not be forced to rewrite the same program. – bzak May 23 '14 at 19:09
  • initial_2 is a result obtained previously. – bzak May 23 '14 at 19:15

1 Answers1

1

Without diving into this long code, you can just use a for loop if the only thing that change is the numbers in the name of the "sets", so each time you'll increment the number at the end in some sort. Just as an example:

for n=1:10
    set1 = {['AA00XY_' num2str(n+1)],['BB74XY_' num2str(n+1)],['CC92XZ_' num2str(n+1)],['FF03VV_2' num2str(n+1)[};
    set2 = {['AA00XY_' num2str(n)]  ,['BB74XY_' num2str(n)]  ,['CC92XZ_' num2str(n)]  ,['FF03VV_' num2str(n)]};
    ...
    ...
    % the rest of the code
bla
  • 25,846
  • 10
  • 70
  • 101