Saving many strings in cell to .txt readable for Unity, error with fprintf

1 view (last 30 days)
Dear all,
I'm pasting all code to let you find all mistakes I made. The error is located in line with fprintf in the end. Mr MATLAB says:
*Error using fprintf Function is not defined for 'cell' inputs.
Error in setcolours (line 57) fprintf(fileID,formatSpec,colours{ch,:});*
I read many answers to similar questions but none of them helped me. I need to save these rows of strings for every channel (more or less 250 strings for 22/32/64 channels) in such way that Unity could read this then, recommended solution is array in .txt file. How can I rich my goal?
I can't attach .edf file, if there is a need, I doubt, you can find them there http://physionet.org/cgi-bin/atm/ATM in EEG Motor Movement Dataset.
Code:
function [colours] = setcolours()
%Function determines ranges of values that channels recorded at given time.
%edfread and data from physionet were used.
%Saved .txt file will be read by Unity to represent ranges of recorded values
%in brain model in Virtual Reality.
%Authors: M. Chen, V. Colaco, K. Wieciorek
%read .edf file (ask user for name of file (input)
rec = input('Type name of the record to be analysed, remember about .edf extension and '' ');
[hdr, record] = edfread(rec);
%Reads data from ALL RECORDS of file fname ('*.edf'). Header information is returned in structure hdr,
%and the signals (waveforms) are returned in structure record, with waveforms associated with the records
%returned as fields titled 'data' of structure record.
[nrows, nsam]= size(record);
%define number of time intervals
nit = nsam/40;
%alternative (10s = 10 000ms, 10 000ms/40ms)
%one frame takes 40ms when 25fps (1000ms/25)
%create array with zeros
meanar = zeros(nrows, nit);
colours = cell(nrows, nit);
%loop for channels 1-23 to determine colours of fragments
%first x of x+1 rows are channels
h=nrows-1;
for ch=1:h
for i=1:nit
j=nit*40;
k=j-40;
meanar(ch, i) = mean(record(ch,k:j));
if meanar(ch, i)< -300
colours{ch, i}={'black'};
elseif -300<meanar(ch, i)<-200
colours{ch, i}={'violet'};
elseif -200<meanar(ch,i)<-100
colours{ch, i}={'blue'};
elseif -100<meanar(ch,i)<0
colours{ch, i}={'cyan'};
elseif 0<meanar(ch,i)<100
colours{ch, i}={'green'};
elseif 100<meanar(ch,i)<200
colours{ch, i}={'yellow'};
elseif 200<meanar(ch,i)<300
colours{ch, i}={'magenta'};
elseif 300<meanar(ch,i)<400
colours{ch, i}={'red'};
else
colours{ch, i}={'white'};
end
end
%save row of cell to .txt
txtname = sprintf('colours_%s_%d.txt', rec, ch);
fileID = fopen(txtname,'w');
formatSpec = '%s\n';
fprintf(fileID,formatSpec,colours{ch,:});
fclose(fileID);
end
%Unity
%define placement of electrodes on model of the brain
%relate time
%change colours once a 40ms -> change colour of channel (row) x to written in column y
end
  1 Comment
per isakson
per isakson on 10 Jan 2015
Is it possible to run the code? Instructions on how to run the code is missing. What is the user supposed to answer to the question rec = input('Type name ...?

Sign in to comment.

Accepted Answer

per isakson
per isakson on 9 Jan 2015
Edited: per isakson on 9 Jan 2015
Replace
colours{ch, i}={'black'};
by
colours{ch, i}='black';
etc.
It is either
colours{ch, i}='black';
or
colours(ch, i)={'black'};
to create a cell array of strings, which is what you want. You did create a cell array of cell array of strings.
  1 Comment
Katarzyna Wieciorek
Katarzyna Wieciorek on 10 Jan 2015
Thank you very much for your help.
Pasting corrected loop for other users:
for ch=1:h
for i=1:nit
j=i*40+1;
if j>nit
j=nit;
end
k=j-40;
ar40(ch, i) = max(record(ch,k:j));
if (ar40(ch, i)<-100)
colours{ch, i}='black';
elseif (-100<ar40(ch, i))&&(ar40(ch, i)<-75)
colours{ch, i}='violet';
elseif (-75<ar40(ch,i))&&(ar40(ch, i)<-50)
colours{ch, i}='blue';
elseif (-50<ar40(ch,i))&&(ar40(ch, i)<-25)
colours{ch, i}='cyan';
elseif (-25<ar40(ch,i))&&(ar40(ch, i)<0)
colours{ch, i}='green';
elseif (0<ar40(ch,i))&&(ar40(ch, i)<25)
colours{ch, i}='yellow';
elseif (25<ar40(ch,i))&&(ar40(ch, i)<50)
colours{ch, i}='magenta';
elseif (75<ar40(ch,i))&&(ar40(ch, i)<100)
colours{ch, i}='red';
else
colours{ch, i}='white';
end
end

Sign in to comment.

More Answers (0)

Categories

Find more on EEG/MEG/ECoG in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!