How can I print variables within a function?

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 日

0 投票

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 件のコメント

Andrew
Andrew 2014 年 7 月 25 日
Good day Michael,
Thank you very much for your response, you clearly have a great grasp of MatLab coding. I believe this is what you wanted me to try (I've removed a few variables so it so it takes less space)
% code
for i = 1:number_of_segments;
Seg(:,i)= mean(bandpower(data3{i,1},sampling_rate,[low_delta,high_delta;low_theta,high_theta],1,1))];
end
However I am getting a "Unbalanced or unexpected parenthesis or bracket" error. I have tried to input the code several different ways with no success.
Method #2
% code
for i = 1:number_of_segments;
Seg(:,i)= 'mean(bandpower(data3{i,1},sampling_rate,[low_delta,high_delta;low_theta,high_theta],1,1))]';
end
If I use Method #2, the script will run however the Seg1 to SegX files are not created. I've also tried Method #3
Method #3
% code
for i = 1:number_of_segments;
'Seg'(:,i)= 'mean(bandpower(data3{i,1},sampling_rate,[low_delta,high_delta;low_theta,high_theta],1,1))]';
end
This method also gives me a "Unbalanced or unexpected parenthesis or bracket" error.
Any ideas? Thank you once again for your kind response.
Sincerely,
Andrew
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 件)

カテゴリ

質問済み:

2014 年 7 月 25 日

コメント済み:

2014 年 7 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by