0

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} 
Bastien
  • 362
  • 2
  • 7
  • 21

2 Answers2

1

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);
blackbird
  • 957
  • 12
  • 18
  • Cool that is almost perfect! Just a quick question. It looks like although there is as many columns in ``CondRowDone`` as there are conditions, this does not translate into the text file. In other words I only get the first column written as a line when I run the code. Any idea as to what to do? Finally, just so I understand and learn from this: the ``if`` loop ads columns to gavRow? – Bastien Feb 02 '14 at 16:29
  • Oh and why is not possible to have condition and subject in the direction shown in the example. Sorry I'm not all that good at MATLAB really. – Bastien Feb 02 '14 at 16:35
  • @Bastien: to your first comment: that was the fault of the use of `size(CondRowDone, 1)` in the second `for`-loop. I corrected my code to use `length(CondRowDone)`. And of course it is possible to get condition and subject in the direction shown, see updated answer. – blackbird Feb 02 '14 at 16:59
  • Thanks so much. Yes I realised after that it was just a matter of swapping in gavRow. And yes! I should really have spotted this "mistake". Can you get back to me re the implementation of the ``if`` loop. Is it instructing the addition of a column? Am I correct? – Bastien Feb 02 '14 at 18:15
  • What the `if` in the loop does is just to add a single space character so that `{HighLabel-504-GFP.avg}` will be separated from the next `{HighLabel-505-GFP.avg}` by a space. It does this only if `curSubject` is not equal to 1, in other words for every subject but the first one. – blackbird Feb 02 '14 at 18:25
0

dlmwrite only handles numeric data. One way around this, if you have Excel, would be to use xlswrite - it can take in (some kinds of) cell arrays directly.

xlswrite(outputfile, CondRowDone);

Then, do some batch xls to csv conversion (for example, see this question and answers). To get a text file directly you'll need to use lower level commands (as in blackbird's answer).

Community
  • 1
  • 1
nkjt
  • 7,825
  • 9
  • 22
  • 28
  • Ok thanks for this pointer too. I did try xlswrite but it failed to load Excel for some reason… Blackbird's answer has put me on the right direction though. Thanks for your input :) – Bastien Feb 02 '14 at 16:31