bzak
bzak

Reputation: 563

How can I convert this script into a MATLAB function?

How can I convert this script into a MATLAB function?

clear all;

set={AB02XY_1,ZT99ER_1,UI87GP_1};

fileData1 = load([fileString set{1} '.mat']);  
fileData2 = load([fileString set{2} '.mat']);  
fileData3 = load([fileString set{3} '.mat']);  

[A,B] = myfunction1_1(fileData1,fileData2,fileData3);

fileName = 'C:\Users\Documents\MATLAB\matrice_AAA001.mat';  
save(fileName,'A','B');

clear all;  

set={AB02XY_2,ZT99ER_2,UI87GP_2};

fileData1 = load([fileString set{1} '.mat']);  
fileData2 = load([fileString set{2} '.mat']);  
fileData3 = load([fileString set{3} '.mat']);  

fileData4 = load('C:\Users\Documents\MATLAB\matrice_AAA001.mat');

[A,B] = myfunction1_2(fileData1,fileData2,fileData3,fileData4);

fileName = 'C:\Users\Documents\MATLAB\matrice_AAA001.mat';  
save(fileName,'A','B');

I do processing on large data files, then to avoid the error 'out of memory ', I split each file into two parts and I use at the beginning of each stage 'clear all'. So, what I want is to have a function as AAA001 = function (AB02XY, ZT99ER, UI87GP, MyFunction1). 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 AB02XY, ZT99ER, UI87GP, and the name of the function used 'MyFunction1' for the sub-processing to get on the last step the file AAA001.

NB: I simplified my script, but actually I divide each file into 5 parts. So I want to transform the 5 parts of my script in a single function!!!

Thank you for your help.

Upvotes: 0

Views: 3622

Answers (2)

Jonas
Jonas

Reputation: 74930

Here's one way to do this. Call the function

 output = function ({'AB02XY', 'ZT99ER', 'UI87GP'}, 5, MyFunction1);

Note that I assume that you want 5 file parts

function out = myMainFunction(fileList, nParts, fun2run)
%# myMainFunction calculates output from multiple split files
%
%# SYNOPSIS out = myMainFunction(fileList, nParts, fun2run)
%#
%# INPUT    fileList: cell array with file body names, e.g.
%#                    'AB02XY' for files like 'AB02XY_1.mat'
%#          nParts  : number of parts in which the files are split
%#          fun2run : function handle or name. Function has to 
%#                    accept lenght(fileList) arguments plus one
%#                    that is the output of the previous iteration
%#                    which is passed as a structure with fields A and B
%#
%# OUTPUT   out : whatever the function returns
%#
%# Brought to you by your friends from SO


%# input checking should go here

if ischar(fun2run)
   fun2run = str2func(fun2run);
end

nFiles = length(fileList);


for iPart = 1:nParts

data = cell(nFiles,1);
for iFile=1:nFiles

data{iFile} = load(sprintf(%s_%i.mat',fileList{iFile},iPart));

end

if iPart == 1
   %# call fun2run with nFiles inputs
   [out.A,out.B] = fun2run(data{:});
else
   %# fun2run wants the previous output as well
   [out.A,out.B] = fun2run(data{:},out);
end

end %# loop parts

Upvotes: 1

FelipeFG
FelipeFG

Reputation: 520

If I understand correctly, the main challenges in this function are to properly assembly the file names, and to pass the function that should be called, right? If you pass the name of the data files as string, you can use sprintf to assemble a filename, like in:

dataSetName = 'AAA001';
dataFilename = sprintf('C:\path\to\datafolder\%s.mat', dataSetName);

For the function, you can pass a function handle as a parameter to your function. For example, consider you define a function:

function c = apply_fun(fun, a, b)
c = fun(a, b);
end

You can, for example, max or mean as func, like this:

>> apply_fun(@max, 1, 2)

ans =

     2

>> apply_fun(@min, 1, 2)

ans =

     1

That is, a reference to max is passed (with @max), and then it's used inside the apply_fun function we defined.

Moreover, you don't need clear all inside a function, for it already has another scope.

Hope this helps you!

Upvotes: 0

Related Questions