Creating new text file
2 views (last 30 days)
Show older comments
I did this earlier but know it does not work. I'm trying to reformat a text file and print another one. When I run the code I turns back the following error:
Error using fprintf
Function is not defined for 'cell' inputs.
Error in P_format (line 36)
fprintf(fid, '%s %4s %4s %4s %4s %4s %4s\n', Pswmm{i,:});
Here is the code:
clear all
clc
%Format text data from NCDC website into SWMM5 compatible P .txt file
fid = fopen('C:\Users\sdehoyos\Desktop\Precip_448903Dulles_448906Reagan.txt');
A = textscan(fid, '%s %d %s %f', 'HeaderLines', '1');
fclose(fid);
COOPstationID=cellstr(cell2mat(A{1,1}));
Date=cellstr(num2str(A{1,2}));
Time=cellstr(cell2mat(A{1,3}));
Ptimes100=A{1,4};
for i=1:length(COOPstationID)
stationID{i,:}=cellstr({COOPstationID{i}(6:end)});
end
for i=1:length(Date)
Year{i,:}=cellstr({Date{i}(1:4)});
Month{i,:}=cellstr({Date{i}(5:6)});
Day{i,:}=cellstr({Date{i}(7:8)});
end
for i=1:length(Time)
Hour{i,:}=cellstr({Time{i}(1:2)});
Minute{i,:}={Time{i}(4:5)};
end
P=cellstr(num2str(Ptimes100/100));
Pswmm={stationID Year Month Day Hour Minute P};
fid = fopen('Pswmm.txt', 'w');
for i=1:length(Pswmm);
fprintf(fid, '%s %4s %4s %4s %4s %4s %4s\n', Pswmm{i,:});
end
fclose(fid);
0 Comments
Answers (1)
Azzi Abdelmalek
on 30 Aug 2013
Use square brackets []instead of curly bracket {}
Pswmm=[stationID Year Month Day Hour Minute P];
1 Comment
Image Analyst
on 31 Aug 2013
Edited: Image Analyst
on 31 Aug 2013
That won't work either because the things in there (stationID, Year, etc.) are also cells.
See Also
Categories
Find more on Characters and Strings 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!