简体   繁体   中英

How to merge two functions in MATLAB?

I have two functions in my MATLAB code and I want that output of one function is used as an input of another function. How can I implement this?

medianfilter1.m

The code is:

function ans = medianfilter(angstanding)
[m,n] = size(angstanding);
ans = zeros(m,n);
x = angstanding(:,1);
y = angstanding(:,2);
z = angstanding(:,3);
svm = angstanding(:,4);

ans(:,1) = medfilt1(x);
ans(:,2) = medfilt1(y);
ans(:,3) = medfilt1(z);
ans(:,4) = medfilt1(svm);

Second Function is: windowing.m

Code:

function [wx,wy,wz,wsvm] = windowing(currentdata)


% i = 1;
% obseen = 128;

 [r,c] = size(currentdata);

%  display(r);
% while obseen <= r
%     i = i +1;
%     obseen = obseen + 64;
%     
%   
% end;
% display(i);
residue  = rem((r - 128),64);
rows = floor((r-128)/64);

rows = rows + 1;


 wx = zeros(rows,128);

 wy = zeros(rows,128);
 wz = zeros(rows,128);
 wsvm = zeros(rows,128);

vectorx = currentdata(:,1);
vectory = currentdata(:,2);
vectorz = currentdata(:,3);
vectorsvm = currentdata(:,4);

for i = 1:rows
    x = (64 * (i-1)) + 1;
    if i ~= rows
        lastj = 128;
    else 
        break ;
    end;

        for j = 1:lastj

                wx(i,j) = vectorx(x);
                wy(i,j) = vectory(x);
                wz(i,j) = vectorz(x);
                wsvm(i,j) = vectorsvm(x);
                x = x + 1;

        end;


end;

and I want that result of medianfilter.m is to be fed into windowing.m .

Below is what i tried: merge.m

function ans = medianfilter(rawdataset_train_acc)
[m,n] = size(rawdataset_train_acc);
ans = zeros(m,n);
x = rawdataset_train_acc(:,1);
y = rawdataset_train_acc(:,2);
z = rawdataset_train_acc(:,3);
svm =rawdataset_train_acc(:,4);

ans(:,1) = medfilt1(x);
ans(:,2) = medfilt1(y);
ans(:,3) = medfilt1(z);
ans(:,4) = medfilt1(svm);

function [wx,wy,wz,wsvm] = windowing(ans)


% i = 1;
% obseen = 128;

 [r,c] = size(ans);

%  display(r);
% while obseen <= r
%     i = i +1;
%     obseen = obseen + 64;
%     
%   
% end;
% display(i);
residue  = rem((r - 128),64);
rows = floor((r-128)/64);

rows = rows + 1;


 wx = zeros(rows,128);

 wy = zeros(rows,128);
 wz = zeros(rows,128);
 wsvm = zeros(rows,128);

vectorx = ans(:,1);
vectory = ans(:,2);
vectorz = ans(:,3);
vectorsvm = ans(:,4);

for i = 1:rows
    x = (64 * (i-1)) + 1;
    if i ~= rows
        lastj = 128;
    else 
        break ;
    end;

        for j = 1:lastj

                wx(i,j) = vectorx(x);
                wy(i,j) = vectory(x);
                wz(i,j) = vectorz(x);
                wsvm(i,j) = vectorsvm(x);
                x = x + 1;

        end;


end;

I tried above but didn't achieve what i wanted. How can i do this?

If you have one function [output_a]=a(input) and a function [output_b] =b(input) you can simply stack them as @zizy archer did already recomend.It would look like this:

sample_input=3;    
sample_ouput_var=a(b(sample_input));

what this block does is taking the sampel input feeding into b() taking the output of b and directly feeding it into a. so in pseudo code:

sample_input=3;
sample_outputb=b(sample_input):
sample_outputa=a(sample_output_b):

In your special case I would do it like this

outa,outb, outc, outd =windowing(medianfilter(input))

If you want to merge two functins I would take the output of the second function and the input of the first one so in pseudocode this would be

function output1=examplefunc(input1):
      output1=input1*input1
end

and a second function

function output2=examplefunc2(input2):
     output2=input2+input2;
end

merge them by:

function outputmerged=merged_func(input1):
   output1=input1*input1;
   input2=output1;
   output_merged=input2+input2;
end

In your code example it looks like this:

function [wx,wy,wz,wsvm] = medianfilter_windowing_merge(rawdataset_train_acc)
%input from medianfilter output from windowing
[m,n] = size(angstanding);
ans = zeros(m,n);
x = angstanding(:,1);
y = angstanding(:,2);
z = angstanding(:,3);
svm = angstanding(:,4);

ans(:,1) = medfilt1(x);
ans(:,2) = medfilt1(y);
ans(:,3) = medfilt1(z);
ans(:,4) = medfilt1(svm);

%here happens the handover from one function to the other everything above is from the medianfilter.m everything below is from windowing 
currentdata=ans

[r,c] = size(currentdata);


residue  = rem((r - 128),64);
rows = floor((r-128)/64);

rows = rows + 1;


 wx = zeros(rows,128);

 wy = zeros(rows,128);
 wz = zeros(rows,128);
 wsvm = zeros(rows,128);

vectorx = currentdata(:,1);
vectory = currentdata(:,2);
vectorz = currentdata(:,3);
vectorsvm = currentdata(:,4);

for i = 1:rows
    x = (64 * (i-1)) + 1;
    if i ~= rows
        lastj = 128;
    else 
        break ;
    end;

        for j = 1:lastj

                wx(i,j) = vectorx(x);
                wy(i,j) = vectory(x);
                wz(i,j) = vectorz(x);
                wsvm(i,j) = vectorsvm(x);
                x = x + 1;

        end;


end;

I don't completely understand what are you attempting. It seems to me that what you want is indeed the composite function.

Once you have a function prototype/definition, you have to call it.

For example, if you have functions to compute the cube and the square of a number (cubeExample and squareExample respectively) and you want to compute the the 6th power of a number x :

result = squareExample(cubeExample(x))

function r = squareExample(a)
    r = a*a;
end

function r = cubeExample(a)
    r = a*a*a;
end

Also note that the names of the function definitions are mute , that means that don't really matter how you name them.

In this case would be something like

[wx,wy,wz,wsvm] = windowing(medianfilter(angstanding))

following your ideas, assuming the input format of windowing is the same as the output of medianfilter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM