1

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.

2
  • c(3)=C+A*B or as a string like this ? -c(3)='C+A*B' Commented Apr 22, 2014 at 9:14
  • yes, like a string! Thanks a lot. Commented Apr 22, 2014 at 9:26

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.