How to read data from text file and store as it is with variables
Show older comments
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
Stephen23
on 9 Jun 2021
How exactly are you planning to process all of those variables?
Rishi Balasubramanian
on 9 Jun 2021
"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.
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.
Rishi Balasubramanian
on 9 Jun 2021
@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.
Rishi Balasubramanian
on 9 Jun 2021
Stephen23
on 9 Jun 2021
Debugging is also a matter of practice.
Answers (1)
Rafael Hernandez-Walls
on 9 Jun 2021
Edited: Rafael Hernandez-Walls
on 9 Jun 2021
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
Rishi Balasubramanian
on 9 Jun 2021
Rafael Hernandez-Walls
on 9 Jun 2021
You need put this idea in a while loop or something like..
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!