How can I print variables within a function?
26 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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
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 :)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!