How can I print variables within a function?

조회 수: 10 (최근 30일)
Andrew
Andrew 2014년 7월 25일
댓글: Andrew 2014년 7월 25일
Good day to all,
1st time posting here so I apologize for any errors. I am currently working on a script which uses the mat2tiles script. Essentially when the data is analyzed a variable called number_of_segments which can vary based on the size of data is created. This value is created by my script and can range from 1 to 1000. From this point I use the following code (which works)
% code
for i = 1:number_of_segments;
s=['Seg' int2str(i) '= mean(bandpower(data3{i,1},sampling_rate,[low_delta,high_delta;low_theta,high_theta;low_alpha1,high_alpha1;low_alpha2,high_alpha2;low_beta1,high_beta1;low_beta2,high_beta2,low_beta3,high_beta3;low_gamma,high_gamma],1,1))'];
eval(s);
end
This creates several variables which are named Seg1 to SegX where X is defined by the variable number_of_segments. Each Seg1 to SegX file is a 1x152 double array. My goal is to automatically concatenate these Seg1 to SegX arrays into one larger array which I have called TotalSegments.
I can do this by hand in the Command Window. For example if I know that number_of_segments = 5 then in order to get my desired result the code would be (this code works)
% code
TotalSegments=vertcat(Seg1,Seg2,Seg3,Seg4,Seg5);
end
The problem is that the variable number_of_segments changes for each file. I would like the script to output my result. My issue is that I do not know how to print this in MatLab. Here is what I have attempted (This code does NOT work)
% code
for i = 1:number_of_segments;
s=[int2str(i) 'TotalSegments= vertcat(Seg(i));'];
eval(s);
end
Does anyone know how this might be achieved? I appreciate any input. Thank You
Currently using MatLab R2014a

채택된 답변

Michael Haderlein
Michael Haderlein 2014년 7월 25일
Dear Andrew,
you shouldn't name your variables this way. Better use the index as what it is: an index.
for i = 1:number_of_segments;
Seg(:,i) = mean(bandpower(...))';
end
That results in an 152-x-number_of_segments double array.
Concenating is then a simple job:
C_Seg=Seg(:)'
Best regards,
Michael
  댓글 수: 3
Patrik Ek
Patrik Ek 2014년 7월 25일
Hi, you must fix the paranthesis and brackets at the end. Right now it looks something like (method #1) mean(...]; Obviously a typo, but these can be hard enough to find. Especially for the person that wrote the code :)
Andrew
Andrew 2014년 7월 25일
Thank you very much to both of you for your help. The final code for those who may revisit this post is as follows
% code
for i = 1:number_of_segments;
Segment(:,i)= mean(bandpower(data3{i,1},sampling_rate,[low_delta,high_delta;low_theta,high_theta;low_alpha1,high_alpha1;low_alpha2,high_alpha2;low_beta1,high_beta1;low_beta2,high_beta2;low_beta3,high_beta3;low_gamma,high_gamma],1,1));
end
Thank you very much once again!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by