vendredi 27 mars 2015

How to concatenate strings in a loop?

How to concatenate strings in a loop? For example I have this character array



fruits = char('apple','pear')


I would like to see this output



apple tart
pear tart


But when I use this loop:



for f = fruits'
strcat(f," tart")
end


The output is



ans =

a tart
p tart
p tart
l tart
e tart

ans =

p tart
e tart
a tart
r tart
tart


In fact I need this to read a bunch of csv files in a directory. Part of the file name is a variable over which I want to loop over. For example my directory contains the 2 following files from which I want to read data:



peartart.csv
appletart.csv


This other answer using a list of files is nice but then I have no control over the fruit variable name in the file. I wand control over this variable because I will perform a statistical test for each fruit data and store the test result with the fruit name in another file.


Aucun commentaire:

Enregistrer un commentaire