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

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 Comments

How exactly are you planning to process all of those variables?
I thought I would pose that on another question, but here we go.
I am trying to automate a very tedious process.
Where my desired output should be like
32 - phi1_32_1, phi2_32_1 32-phi1_32_2, phi2_32_2.......... 32 - phi1(32, m), phi2(32, m)
(32++) - phi1_33_1, phi2_33_1 (32++) - phi1_33_2, phi2_33_2......... (32++) - phi1(33, m), phi2(33, m)
.... and so on for a particular level.
Hope this helps
"I am trying to automate a very tedious process."
Yes, I understand.
That data design certainly looks very tediuous.
By forcing meta-data into the variable names (something that some beginners apparently like to do) you have forced yourself into writing slow, inefficient, complex, buggy code that is difficult to debug. Poor data design is the cause of these difficulties.
"I thought I would pose that on another question,"
Yes, I thought that might be the case:
Any methods you find that magically process all of those variable names will be slow and inefficient.
You have painted yourself into a corner: https://en.wiktionary.org/wiki/paint_oneself_into_a_corner
As long as you continue to access variable names dynamically your code will be complex, slow, and difficult to work with. Instead of using MATLAB as it was designed (for data in arrays) you are fighting it.
Improving your data design would make your code simpler and much more efficient. Most likely you should be using indexing, just like all experienced MATLAB users would do.
Ah, the indexing. I was specifically trying to bypass that and use the way I intended for easier readability, as I will be using this to automate multiple cases of the similar kind.
I have had so many of my doubts answered crisply by you, so if you're saying, must mean something.
I will try as you said, and if I f up again, I will post another comment maybe. Thank you.
@Rishi Balasubramanian: indexing is only one possibility. That data looks perfect for a table:
Each piece of data should be stored as its own variable in the table, so you table would look something like this:
phi A B C
--- - - -
1 32 1 1
1 32 2 1
1 32 3 1
1 32 4 1
1 32 5 1
2 32 1 32
2 32 2 33
2 32 3 34
2 32 4 35
2 32 5 36
This will make processing your data much much easier than what you are currently trying to do.
"I was specifically trying to bypass that and use the way I intended for easier readability"
So you want to "bypass" simple and efficient indexing by writing slower, more complex, obfuscated code that removes all of the benefits of using MATLAB (i.e. working with arrays). An very interesting design decision.
Readabiity is just a matter of practice.
Thought it might be helpful for iteration by iteration debugging...
Debugging is also a matter of practice.

Sign in to comment.

Answers (1)

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 Comments

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.
You need put this idea in a while loop or something like..

Sign in to comment.

Products

Release

R2021a

Edited:

on 10 Jun 2021

Community Treasure Hunt

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

Start Hunting!