2

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);

3
  • 1
    Your code for writing a string to a text file looks okay. I would guess that for some iteration in your double loop str is not a string. Can you provide an example (in the question) of what search and patterns look like? Perhaps we can eliminate that double loop and fix your problem in one hit... ps I don't know who down-voted this but it is not obvious to me why it received a downvote? Commented Jan 9, 2013 at 2:05
  • ok, when I display the value of str I gotstr = 'GGUCUGGGGUAUUGGCAAUAUGGGGUAGAGCAAUACUCUUCCGUGUUAAGAGAAAAUAAUAUUCCAAUCG' ??? Error using ==> fprintf Function is not defined for 'cell' inputs. Error in ==> suppI at 8 fprintf(fid, '%s\n', str); Commented Jan 9, 2013 at 11:08
  • I'm glad to see you have resolved the problem with the help of @Pete (+1). One other suggestion: I suspect that you could definitely eliminate that double loop from your code and make it run much faster - look into functions like ismember and intersect. Commented Jan 10, 2013 at 2:24

1 Answer 1

1

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...

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks all I tried fprintf(fid, '%s\n', cell2mat( str)); and it works well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.