Reputation: 291
I have a text file which consists of different combinations of characters. For example:
a+b*c
b+c*a
c+a*b
I want to read this file into matlab, and want to assign each line to an array like this:
c(1)=a+b*c
c(2)=b+c*a
c(3)=c+a*b
Further, I would like to assign other character arrays to the variables a,b,c etc. For example, I can assign as
a='A', b='B', c='C'
and print the final outputs as:
c(3)=C+A*B
But I am stuck at both the steps, as to how to read and assign the different lines to different character arrays in Matlab. Any suggestions are welcome.
Upvotes: 1
Views: 45
Reputation: 221504
Use importdata
to read the text into a cell array and perform the replacements one by one.
Code
%%// Name of your text file
file1 = 'eqns.txt'
c = importdata(file1)
c = strrep(c,'a','A');
c = strrep(c,'b','B');
c = strrep(c,'c','C')
Output
c =
'a+b*c'
'b+c*a'
'c+a*b'
c =
'A+B*C'
'B+C*A'
'C+A*B'
If you are interested in getting the alphabets into upper-case, you can directly do so after importing data -
c = importdata(file1)
c = upper(c)
Upvotes: 1