0

I am using matlab. I have a function which at the moment returns 5 arrays, but I want to join the array into a single matrix or just a cell array with heading of each being the output of the current function?

For instance, giving output like:

low_sec    lowmid_sec
1              7  
2              6    
35             5
5              43

Any ideas?

function [low_sec ,lowmid_sec , middle_sec , upmid_sec , upper_sec]=     sepfunc(intensdata)lengthofdata=length(intensdata); 
count1=0;
count_2=0;
count_3=0;
count_4=0;
count_5=0;

 for i=  1:lengthofdata %loop to seperate count number of data in 5 groups 
    if (intensdata(i,1)<0.05)
        count1=count1+1;     
    elseif (intensdata(i,1)>=0.05 && intensdata(i,1)<0.1)
        count_2=count_2+1;
    elseif (0.1<=intensdata(i,1) && intensdata(i,1)<0.15)
        count_3=count_3+1;
    elseif (0.15<=intensdata(i,1) && intensdata(i,1)<0.2)
        count_4=count_4+1;
    elseif (intensdata(i,1)>=0.2 )
        count_5=count_5+1;
    end
 end
  disp(count1);
  disp(count_2);
  disp(count_3);
  disp(count_4);
  disp(count_5);
   j=1;
   k=1;
   m=1;
   n=1;
   x=1; 
   low_sec=[count1];
   lowmid_sec=[count_2];
   middle_sec=[count_3];
   upmid_sec=[count_4];
   upper_sec=[count_5]; 


for i=  1:lengthofdata %to seperate original data into 5 different sub-groups. 
   if (intensdata(i,1)<0.05)
        low_sec(j,1)=intensdata(i,1);
        j=j+1 ;
    elseif(0.05<=intensdata(i,1) && intensdata(i,1)<0.1)       
        lowmid_sec(k,1)=intensdata(i,1);
        k=k+1;
    elseif(0.1<=intensdata(i,1) && intensdata(i,1)<0.15)       
        middle_sec(m,1)=intensdata(i,1);
        m=m+1;
    elseif(0.15<=intensdata(i,1) && intensdata(i,1)<0.2)       
        upmid_sec(n,1)=intensdata(i,1);
        n=n+1;
    elseif( intensdata(i,1)>=0.2)       
        upper_sec(x,1)=intensdata(i,1);
        x=x+1; 
   end

end
4
  • Why not output a struct? Commented Jul 21, 2014 at 14:59
  • i just began using matlab 2 weeks ago so i dont really know what that does. really new to programming in general. Commented Jul 21, 2014 at 15:01
  • @Dan i am not sure how to use that, is there no other way? Commented Jul 21, 2014 at 15:07
  • 1
    I think the easiest way is to concatenate all the arrays into a single matrix. Take a look at cat: mathworks.es/es/help/matlab/ref/cat.html Commented Jul 21, 2014 at 15:11

1 Answer 1

4

You have a few options, ues a cell array as you mentioned, use the new table structure or the easiest would be to just create a struct.

To do this, all you need is to add the following at the end of your function:

sec.low = low_sec;
sec.lowmid = lowmid_sec;
sec.middle = middle_sec;
sec.upmid = upmid_sec;
sec.upper = upper_sec;

and then to change your first line to be:

function sec = sepfunc(intensdata)
Sign up to request clarification or add additional context in comments.

1 Comment

Cell array approach - sec = {low_sec ,lowmid_sec , middle_sec , upmid_sec , upper_sec};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.