I understand that this is a recurrent topic, I have tried understanding the answers that people provided but none of them seemed easy to transfer to my particular problem as the solutions are usually for an implementation that is too far from what I'm trying to do
Could anyone help with the following.
When I run the code below I get:
Error using dlmwrite (line 118)
The input cell array cannot be converted to a matrix.
Error in GAVNeuroScan (line 25)
dlmwrite(outputfile, CondRowDone);
I give an example of what I want to achieve in the comments at the end of the code.
If someone can help me getting the contents of CondRowDone
to a text file as exemplified in the comments that would be great!
studyname='TestGav';
subjects={'504','505'};
conditions={'HighLabel','LowLabel','HighSound','LowSound'};
nCond=4;
GFPorBMR='GFP';
for curCond=1:length(conditions)
for curSubject=1:length(subjects)
gavRow{curSubject}=[subjects(curSubject) '-' conditions{curCond} '-' GFPorBMR '.avg'];
end
CondRowDone{curCond,:}=['GROUPAVG' '{' gavRow '}' 'G Y 1 N N' conditions{curCond} 'avg.'];
end
outputfile = [studyname '_GAV_' curSubject '.txt'];
dlmwrite(outputfile, CondRowDone);
% What I want is a text file that would look exactly like that. I think I'm
% not far but it fails to write...
%
% GROUPAVG {{HighLabel-504-GFP.avg} {HighLabel-505-GFP.avg}} G Y 1 N N {HighLabel.avg}
% GROUPAVG {{LowLabel-504-GFP.avg} {LowLabel-505-GFP.avg}} G Y 1 N N {LowLabel.avg}
% GROUPAVG {{HighSound-504-GFP.avg} {HighSound-505-GFP.avg}} G Y 1 N N {HighSound.avg}
% GROUPAVG {{LowSound-504-GFP.avg} {LowSound-505-GFP.avg}} G Y 1 N N {LowSound.avg}
From what I have seen using the debugger, you have a little bit of confusion between the curly braces as text and the curly braces to handle MATLAB cell arrays.
Here is a re-write of your for
-loop to produce the cell array of strings you have given in your code example. Also, to produce the exact output you specified, subject and condition have to be given in a different order:
for curCond=1:length(conditions)
gavRow = [];
for curSubject=1:length(subjects)
if (curSubject ~= 1)
gavRow = [gavRow ' '];
end
gavRow = [gavRow '{' [conditions{curCond} '-' subjects{curSubject} '-' GFPorBMR '.avg'] '}'];
end
CondRowDone{curCond}=['GROUPAVG ' '{' gavRow '} ' 'G Y 1 N N {' conditions{curCond} '.avg}'];
end
As for the task of writing the strings to disk, MATLAB is telling you that it cannot handle your cell array as a matrix. When it comes to write cell arrays to disk, I think you have to write it yourself using low-level functions, like this:
outputfile = [studyname '_GAV_' curSubject '.txt'];
fid = fopen(outputfile, 'w');
for i=1:length(CondRowDone)
fprintf(fid, '%s\n', CondRowDone{i});
end
fclose(fid);