MATLAB Answers

0

skip lines starting with a special character and assign data to variables

Zaheer Shariff さんによって質問されました 2019 年 6 月 11 日
最新アクティビティ Zaheer Shariff さんによって コメントされました 2019 年 6 月 12 日
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 件のコメント

サインイン to comment.

1 件の回答

dpb
Answer by dpb
on 11 Jun 2019

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 件のコメント

Thank you for the help. However I am not sure if this would be the best solution for my case as the format of the data to be read from my input file may also contain strings/text (sorry that I did not mention this earlier in my question). And therefore I would like to use something like fgetI to read the data line by line and assign it immediately to a variable name. For example the input file may contain text which is the name of file containing experimental data say 'experiment1.txt' I will assign it to a variable 'name'; and then I would be able to acess this file using:
filename = [name,'']
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.
Thank you again. I agree with your sugesstions and will to proceed based on it. However, the code you suggested previously considers that all the data in the input file to be of the same format. What if I have mixed data; say numbers and text to read. How would you suggest me to do this. for example see below:
--------------------------------------------------------------------------------------------
% 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
% name of the file containing expt data
experiment1.txt
---------------------------------------------------------

サインイン to comment.



Translated by