MATLAB: Fastawrite error with string array as input datatype

Bioinformatics Toolboxcharacter arraystrings

Dear MATLAB community,
I would like to create a fasta file from a cell array of character vectors. I have used the fastawrite function in Bioinformatics toolbox but I encounter the following error:
a=top50.dat.aa{2,1};
% 'a' is 50 X 1 cell array of character vectors
fastawrite('test.fasta',a)
Error using fastawrite (line 123)
Data is not a valid sequence or structure.
I have also tried to convert 'a' to a string but the error is still the same:
fastawrite('test.fasta',string(a))
Error using fastawrite (line 123)
Data is not a valid sequence or structure.
I have solved this issue by using a loop (since fasta write appends an existing file instead of overwriting it) but I would like to know if a more direct solution exists.
for i=1:50
fastawrite('test.fasta', string(a(i,1)))
end
Considering that since MATLAB 2018 all functions which take character array as an input work with string arrays, I would like to know if there is an issue with my code or with fastawrite.
Thank you,
Akshay
IIT D

Best Answer

You generally should be able to cell arrays of character vectors and string vectors interchangeably, and a character vector interchangeably with a single string. Both of these are two ways of encoding text. But if a function only excepts one piece of text, then you should expect to see the same error if you specify more than one piece of text. That is the case in the error you get when calling fastawrite with a cell of character vector or with a vector of strings.
In other words, (and as described in the documentation that madhan ravi points to), fastawrite only accepts a single piece of text as the data. This piece of text can be specified as a character vector or as a single string. If you want to write several pieces of text into a single FASTA file, then you first need to join them into a single piece of text. For example, you could try the following:
a=top50.dat.aa{2,1};
% 'a' is 50 X 1 cell array of character vectors
fastawrite('test.fasta',join(a, ''))