Changing the value of column in a file if matches certain criteria of another value

Hi Folks,
I am new in programming. I have 2 text files. the file xx has 4 columns and the file yy has 2 columns.
What I want to do is reading file yy and if any row mathes the values of row (column 2 and column 3) of file xx, then changes the value of column 4 in the file xx to a number 99 instead of its original value.
Thanks in advance
Salim

Answers (1)

You can approach programming problems in the same basic way as word problems in math or science.
What do you need to do? You've stated that.
What are components in that?
  1. You need to read files. You need to read xx and yy.
  2. You need to store the data from xx and yy inside the MATLAB workspace.
  3. You need to compare data in yy to data in xx.
How far have you gotten? What specifically do you need help with? Do you know how to read files into a data matrix? Do you know how to access regions of a matrix by index? Do you know how to write a for loop?

7 Comments

Dear @Daniel,
I wrote the following script, but still did not changes the values of column 4 according to the criteria.
Any help is appreciated.
Thanks!
With the caveat that I've only read your script without executing it, take a look at your for loop and what conditions it's executing. Watch the values of i and j each time it executes. You can do this in two ways:
  1. Add an fprintf statement inside the for loop, printing the values of i and j.
  2. In the MATLAB Editor, click on the number "23" on line 23. This is the first line inside the for loop. That will set a breakpoint. Then click "Run" in the ribbon. It will pause on line 23, and you will be able to see the values of i and j in the workspace pane, which should be on the right. Do this repeatedly and you can watch how i and j change.
Check if any values of i and j are being skipped.
Also, lines like p(i)=p(i) are not useful. Variables automatically hold their value until they are changed explicitly.
Also, I should mention array indexing a little bit.
If I write, for instance,
x = [1 2 4 8 16 32 64 128 256 512];
then I can access multiple elements of x at once. For instance, the line below will grab all the odd indexes of x.
x([1 3 5 7 9])
ans = 1×5
1 4 16 64 256
I can then modify the odd indexed values to have a new value, e.g.
x([1 3 5 7 9]) = 10
x = 1×10
10 2 10 8 10 32 10 128 10 512
Now take a look at the modified value of x.
x
x = 1×10
10 2 10 8 10 32 10 128 10 512
I can access elements of the matrix more efficiently with the colon operator:
x(1:2:10)
ans = 1×5
10 10 10 10 10
will grab all the odd elements of the now-modified row vector.
Dear @Daniel,
Thanks for your kind respond, but still I could not make the code to work correctly. It does not changes the value of (a).
Please, could you have a trial and see whats the problem.
Regards
Salim
Hi @Salim,
If you share what you tried in response to my comment, I'll be happy to provide further tips. This looks like homework, and it's both unhelpful and against most academic policies to use a homework solution contributed directly by someone else. However, if I can help you learn how to solve the problem, I'm happy to assist in that way.
Please take a look at your code results, let me know what specifically is going wrong for you, and I can help you work through issues so you can learn. This is a huge part of the code development process: to try something, test it, watch it fail, and adjust it until it works.
Regards,
Daniel
Dear @Daniel,
below is attached my trial with the .m file and the input files.
Many thanks in advance.
Salim
edge_assign
Hi @Salim,
Please perform the steps in my reply https://www.mathworks.com/matlabcentral/answers/2018351-changing-the-value-of-column-in-a-file-if-matches-certain-criteria-of-another-value#comment_2879516. Once you have done so, let me know the results. The error in your code can be found by following the suggestion in my comment. Once the error is identified we can discuss how to fix it, if the solution isn't clear from my previous comments.
Regards,
Daniel

Sign in to comment.

Asked:

on 8 Sep 2023

Commented:

on 10 Sep 2023

Community Treasure Hunt

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

Start Hunting!