2

I have two txt files that are 1.txt and 2.txt. It stores data such as

1.txt:

P_e = [-0.1 0.71 0.88;-0.09 0.59 0.839;-0.08 0.55 0.816;-0.07 0.546 0.811;-0.06 0.46 0.769]

and data in 2.txt is

  P_e = [-0.1 0.5 0.6;-0.09 0.1 0.2;-0.08 0.3 0.4;-0.07 0 01;-0.06 0 0]

I want to cacluate sum of P_e variables column by column,except column 1 in 1.txt and 2.txt and store it into P_e_sum variable.

    -0.1    0.71+0.5    0.88+0.6
    -0.09   0.59+0.1    0.839+0.2
    -0.08   0.55+0.3    0.816+0.4
    -0.07   0.546+0     0.811+0
    -0.06   0.46+0      0.769+0

So the result is

Pe_sum=[ -0.1   1.21    1.48;
        -0.09   0.69    1.039;
        -0.08   0.85    1.216
        -0.07   0.546    0.811;
        -0.06   0.46    0.769]

Could you help me to implement it by matlab? Thank you so much

1 Answer 1

1

Its going to be hard to read those in as text. But the contents of those text files are basically formatted as matlab scripts anyway. Rename them to .m then you can just do this:

run ('1.m')
p1 = P_e;  
run ('2.m')
p2 = P_e;

pSum = [p1(:, 1), (p1(:, 2:end) + p2(:, 2:end))];

Will do what you want.

As a side note, consider not naming these files just 1.txt Matlab doesn't agree with numeric first file names.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much. But I think you maybe change run('1.m') to run('1').
@user8264 no, matlab requires .m files to work with run. You will need to rename the .txt files to .m files.
Yes. I rename that files. But i check your script does not work if uses run('1.m'). It need to use run('1')

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.