How to read data from text file and store as it is with variables

조회 수: 15 (최근 30일)
Rishi Balasubramanian
Rishi Balasubramanian 2021년 6월 9일
편집: Stephen23 2021년 6월 10일
I have this data file, it goes like
phi1_32_1 = 1 phi2_32_1 = 32
phi1_32_2 = 1 phi2_32_2 = 33
phi1_32_3 = 1 phi2_32_3 = 34
phi1_32_4 = 1 phi2_32_4 = 35
phi1_32_5 = 1 phi2_32_5 = 36
... and so on.
I want to read this file into matlab and store as it is. Where for example
the phi1_31_1 is the name of the variable and the value stored in it is 1
the phi2_31_1 is the name of the variable and the value stored in it is 32
... and so on.
Any help is much appreciated. I thank those who would be taking their time to help me, in advance...
  댓글 수: 7
Rishi Balasubramanian
Rishi Balasubramanian 2021년 6월 9일
Thought it might be helpful for iteration by iteration debugging...
Stephen23
Stephen23 2021년 6월 9일
Debugging is also a matter of practice.

댓글을 달려면 로그인하십시오.

답변 (1개)

Rafael Hernandez-Walls
Rafael Hernandez-Walls 2021년 6월 9일
편집: Rafael Hernandez-Walls 2021년 6월 9일
Assuming you have a file with only following line (name of file= 'algo.txt'):
phi1_32_1 = 1
You could then read the line with the following command:
fid=fopen('algo.txt','r');
a=fscanf(fid,'%s');
eval(a)
whos
I hope it is useful.
  댓글 수: 2
Rishi Balasubramanian
Rishi Balasubramanian 2021년 6월 9일
Got an error message
Error: Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To
construct matrices, use brackets instead of parentheses.
Rafael Hernandez-Walls
Rafael Hernandez-Walls 2021년 6월 9일
You need put this idea in a while loop or something like..

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by