skip lines starting with a special character and assign data to variables
3 ビュー (過去 30 日間)
古いコメントを表示
Hello I am trying to write a code that reads a .txt file line by line and assigns the data to different variable names while omitting the lines that start with a special character such as % or # which are just comments.
for example I have the following text in the input data file:
--------------------------------------------------------------------------------------------
% model for activities
% 1 = D-H model
% 2 = modified DH model
% 3 = ideal solution
2
% program option
% 1 = fitting
% 2 = equilibrium
% 3 = equilibrium and fitting
2
% parameters of DH model
% A parameter
1.63E-03
---------------------------------------------------------
I would like to read the above text file where the lines starting with '%' are just comments which I would want to ignore. Then when I read the first number I would like to assign it to a variable say 'actmodel' and continue assigning the other numbers to variable names such as 'programoption','DHA' and so on in a loop.
I know that I should use fgetl to read the data line by line but I do not know how I can omit the lines with special characters and also assign the data to different variable name in a loop until I reach the end of the file
Thank you in advance
0 件のコメント
回答 (1 件)
dpb
2019 年 6 月 11 日
fid=fopen('yourfile.txt','r');
fmt='%f';
data=cell2mat(fid,fmt,'commentstyle','%'));
Do the assignment to variables either by a lookup of variable versus array position or use a struct with named fields from such a lookup table or similar. Do NOT create a whole list of independent variable names dynamically.
3 件のコメント
dpb
2019 年 6 月 11 日
Well, I'd still recommend against the plethora of variables unless the number is manageable and invariant and all subsequent code can easily be written in terms of those specific variables without repeating blocks of identical code excepting for changing the variable name, etc.
fgetl still doesn't solve your problem cleanly because even in your sample file the pattern between variables isn't the same--the first has comment lines of what the numbers mean as input guidance before the actual input and no clear variable name associated while the second only has the one line identifier but that, too, is not a valid MATLAB variable name and you only know it is a parameter after you've found the next line is numeric unless you're also parsing the comment line to determine what is in it...which is completely at odds with the idea of treating those lines as comment lines--they actually are also data that would be required to do what your question asks if were to explicitly follow that direction.
My suggestion instead is, presuming the file input is regular, to make a standalone lookup table that associates the input values with the associated variable but do that assignment programmatically, not by explicit code.
参考
カテゴリ
Help Center および File Exchange で String Parsing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!