MATLAB Answers

So i wanted to add a column to this table.

3 views (last 30 days)
CalebJones
CalebJones on 21 Aug 2019
Commented: CalebJones on 12 Nov 2019
Capture.JPGCode for=> at the 2nd column i wanted to add data in form of an iteration.Starting with string 'Rest' from 1 to 202 then 202 to 204 will have sting 'active' then 205 to 407 will have 'rest', 408 to 610 will have 'active'.
Rest and active string in the second column for every 202nd iternation.
basically for 202 seconds its rest and next 202 seconds it active.... this is basically a paradim for fnirs brain signal.

  0 Comments

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 23 Aug 2019
Edited: Guillaume on 23 Aug 2019
Note that 1 to 203 having the same element makes that a half-period of 203, not 202.
Simple way to create a column vector that switches between two values at regular interval be;pw. I'm creating a table since as David said, you can't mix types in a matrix. I'm also storing 'rest' and 'active' as categorical, it's the most logical data type for this:
halfperiod = 203; %calculated however you want
values = categorical({'rest'; 'active'});
numperiods = ceil(numel(zz) / halfperiod / 2); %last period may not be filled completely if numel(zz) is not a multiple of 2*halfperiod
filler = repmat(repelem(values, halfperiod), numperiods, 1); %create repeated values numperiod times
result = table(zz, filler(1:numel(zz)));
result.Properties.VariableNames = {'zz', 'state'} %optional give good names to the table variables
No loop needed.

  10 Comments

Show 7 older comments
CalebJones
CalebJones on 11 Nov 2019
The issue was with the variable which was of the type floating point number.
halfperiod = int16(interval);
Interval is 175.000 so when I converted it to int16 issue was solved.
Guillaume
Guillaume on 11 Nov 2019
I would strongly recommend that you use
halfperiod = round(interval); %or ceil(interval) or fix(interval) or floor(interval) depending on which rounding mode you want
instead of converting to int16.
To see what sort of trouble you can expect with your int16 conversion, see for example:
>> 800000 / int16(10)
ans =
int16
32767
did you expect that?
CalebJones
CalebJones on 12 Nov 2019
halfperiod = round(interval);
Does the job for me. Thank you.

Sign in to comment.

More Answers (1)

David K.
David K. on 21 Aug 2019
So one big problem here is that you do not have a table. You have a matrix. And matrices cannot have strings and numbers in them. You have plenty of choices so here are 2.
If you want the matrix format you could instead say that active is 1 and rest is 0 and create the matrix as such:
x(1:201,1) = 0;
x(202:204,1) = 1;
x(205:407,1) = 0;
x(408:610,1) = 1;
FullMat = [zz , x];
If you want the words themselves you can do nearly the same thing:
x(1:201,1) = {'Rest'};
x(202:204,1) = {'active'};
x(205:407,1) = {'Rest'};
x(408:610,1) = {'active'};
This creates a cell of values. If you want them as the data type table you can then do
myTable = table(zz,x);

  6 Comments

Show 3 older comments
CalebJones
CalebJones on 23 Aug 2019
so basically every 202 seconds it has to switch from rest to active and so on..
1 to 203 will have continues value of 'Rest' and 204 to 406 will have value 'Active'...so on... in column two.
The value 202 isn't hardcoded it is calculated in a else, but that part is done and sorted.
Right now i want a way to put rest and active in column two.
If loops seems ineffecient pls suggest another way.
Psuedocode would really help.
Thank you
David K.
David K. on 23 Aug 2019
This may help you on your way:
flag = 0; % Start at rest
startVal = 1;
switchAmount = 202; % or whatever you call your calculated value
x(startVal:startVal+switchAmount,1) = flag;
% Could also do an if statement to do the words as shown in the original answer
flag = flag==0; % Flip flag between 1 and 0
startVal = startVal+switchAmount+1; % Update start val
Then if switchAmount is constant through a run you can put that bit in a loop for however many times you want it to. Or if you calculate switch amount multiple times you can put this after switch amount.

Sign in to comment.

Sign in to answer this question.

Tags