Textscan on tab delimited file in MATLAB
Show older comments
Please I require help to make this code output the content of a file after inserting a tab and a & in-between the elements:
%Open file for reading
[FileName,PathName,FilterIndex] = uigetfile('*', 'Select the input file');
if FilterIndex
FileName = strcat(PathName,FileName);
fid = fopen(FileName);
temp = textscan(fid, '%d\t %s\t %10.6f\t %10.6f\t %10.6f\t %10.6f\n');
tmpstr1 = temp(1);
tmpstr2 = temp(2);
tmpstr3 = temp(3);
tmpstr4 = temp(4);
tmpstr5 = temp(5);
fclose(fid);
fid = fopen('MetabsArranged.txt', 'w');
A = [tmpstr1 tmpstr2 tmpstr3 tmpstr4 tmpstr5];
for k = 1:size(A)
fprintf(fid, '%d\t &\t %s\t &\t %10.6f\t &\t %10.6f\t &\t %10.6f\t &\t %10.6f\n',A{k});
end
fclose(fid)
end
Input example- a txt file:
1 Sugar 3.13E-01 4.38E-01 4.38E-01 2.19E-01
2 Histidine 6.25E-02 1.88E-01 6.25E-02 6.25E-02
Output required:
1 & Sugar & 3.13E-01 & 4.38E-01 & 4.38E-01 & 2.19E-01
2 & Histidine & 6.25E-02 & 1.88E-01 & 6.25E-02 & 6.25E-02
1 Comment
Jan
on 12 Sep 2011
Please format your code, such that it is readable. See: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup
Did you read the "Markup help" link? If not, why?
It seems to be very hard for beginners to format the code. It would be helpful, if you describe your problems in: http://www.mathworks.com/matlabcentral/answers/12556-how-can-code-formatting-be-made-more-intuitive-for-newcomers
What does "unable to get the code to work" mean exactly? Do you get an error - which message? Does the output differ from the expectations?
Accepted Answer
More Answers (0)
Categories
Find more on Data Import and Export in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!