0

如何将此脚本转换为 MATLAB 函数?

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

实际上,我对大型数据文件进行处理,然后为了避免出现“内存不足”错误,我将每个文件分成 4 个部分,并在每个阶段开始时使用“全部清除”。所以,我想要的是有一个功能

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

而且,我的问题是我必须为其他数据文件编写相同的脚本。那么,有没有办法构建一个函数,我可以只更改文件名 AA00XY、BB74XY、CC92XZ、FF03VV、initial_2 和使用的函数名称 A002A_1_Filtre、A002A_2_Filtre、A002A_3_Filtre 和 A002A_4_Filtre 进行子处理最后一步文件“matrice_A002A_2.mat”

注意:我想在一个函数中转换我脚本的 4 个部分!!!

谢谢您的帮助。

#

纳坦回答后,我做了以下改动:

    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
4

1 回答 1

1

无需深入研究这个长代码,如果唯一改变的是“集合”名称中的数字,则可以使用 for 循环,因此每次您都会以某种方式在末尾增加数字。举个例子:

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
于 2014-05-24T06:22:09.280 回答