how made log-file in *.txt format?

11 次查看(过去 30 天)
Mahdi
Mahdi 2015-4-20
编辑: Stephen23 2015-4-21
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!

回答(1 个)

Stephen23
Stephen23 2015-4-20
编辑:Stephen23 2015-4-20
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 个评论
Mahdi
Mahdi 2015-4-21
编辑:Mahdi 2015-4-21
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 2015-4-21
编辑:Stephen23 2015-4-21
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by