how made log-file in *.txt format?
22 views (last 30 days)
Show older comments
Hi everyone,
I have written a program about air traffic controll and now want to save all texts, which appear in MATLAB comand window. Each text is 'String'. for example:
Display_Gate=['In front of ',myObjects.Aircraft(k).myCALLSIGN,' there is another aircraft which his seperated distance is not completed. ','Therfore it should be stay at last Postion for this time= ',num2str(myObjects.Aircraft(k).myTIME)];
I have to arrange these texts according to Callsign of airplanes in *.txt file. for example:
DLH1230:
text1
text2
text3
...
FRA920:
text1
text2
text3
...
usw. last Question ==> how can I add fixed Header (such Date/Time, name of Program/Department) to the Log-File?
Thank you in advance!
0 Comments
Answers (1)
Stephen23
on 20 Apr 2015
Edited: Stephen23
on 20 Apr 2015
You can use fprintf to write data to a text-file. It can also convert numeric values to string, so you can replace all of the num2str calls as well, simplifying your code.
Note that you will need to study the documentation carefully as there are many options for the formatting!
2 Comments
Stephen23
on 21 Apr 2015
Edited: Stephen23
on 21 Apr 2015
A loop might be a good way to do this, but you don't really explain enough for us to know how these text and data are related to each other.
Why not just loop over the aircraft like this:
fid = fopen(filename,'wt');
%fid = 1;
for k = 1:numel(myObjects.Aircraft)
fprintf(fid,...) % write the text
end
fclose(fid)
Note that you can use an fid value of 1 to write to the command window: this might be useful for getting this code working, otherwise use fopen and fclose to write to a file.
You do not explain how time and the airplanes are related to each other, nor why you think you need two loops. If you upload your code then we would have more idea of what you are actually doing: you can upload using the paperclip button and press both the Choose file and Attach file buttons.
Note that fprintf is more efficient than string concatenation or using num2str, and arguably neater too:
str = 'In front of %s there is another aircraft whose separated distance is not completed. Therefore it should be stay at last position for time= %f';
fprintf(fid, str, myObjects.Aircraft(k).myCALLSIGN, myObjects.Aircraft(k).myTIME);
Also note that cmpstr is not a MATLAB function (but strcmp is), and that you should avoid naming variables i or j as these are both names of the inbuilt imaginary unit.
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!