user1351343
user1351343

Reputation: 45

how to save an array of strings into a text file using MATLAB

I want to search for strings in a text file 'marine_forservers.txt' according to the indices saved in 'x.txt' and then save those strings in output file This is the code I tried but it can't save strings to files Can anyone help me??

search = importdata('marine_forservers.txt');
patterns=importdata('x.txt');
fid = fopen('outputI.txt','w');
for i=1:length(patterns)
    for j = 1:16709
        if(j==patterns(i))
         str= search(j);
         fprintf(fid, '%s\n', str);
   end
end
end
fclose(fid);

I got this Error in ==> suppI at 8 fprintf(fid, '%s\n', str);

Upvotes: 2

Views: 2266

Answers (1)

Pete
Pete

Reputation: 2344

This error:

??? Error using ==> fprintf Function is not defined for 'cell' inputs. 

Tells you everything you need to know; str is not a character array - it's a MATLAB cell:

http://www.mathworks.com/help/matlab/ref/cell.html

This should fix it:

fprintf(fid, '%s\n', str{1});

As a side note, that is why you should always include the error message text in the original question...

Upvotes: 1

Related Questions