khoshnaw
khoshnaw

Reputation: 53

creating matrix/array with number and string

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

Upvotes: 0

Views: 53

Answers (1)

Dan
Dan

Reputation: 45752

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)

Upvotes: 4

Related Questions