how made log-file in *.txt format?

22 views (last 30 days)
Mahdi
Mahdi on 20 Apr 2015
Edited: Stephen23 on 21 Apr 2015
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!

Answers (1)

Stephen23
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
Mahdi
Mahdi on 21 Apr 2015
Edited: Mahdi on 21 Apr 2015
Thank you for your answer. Another Question: the number of aircrafts (objects) isn't known bevor starting of Simulation. I have found only the page, which explaine fprintf. But I don't know, how to show statements for each aircraft seperately in text file. Should I add them as following in a 'for-loop' and after that order them for each aircraft? for example:
for k=1:length(time)
texts(k)=[... text ...]
end
for i=1:length(myObjects.Aircraft)
if cmpstr(myObjects.Aircraft(k).myCALLSIGN, texts(k,:)
????? (write texts, which belong to aircraft k)
end
end
Stephen23
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.

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!