Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 854623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:57:10+00:00 2026-05-15T07:57:10+00:00

In the sample text file below, if column 3 contains a 1 then the

  • 0

In the sample text file below, if column 3 contains a 1 then the corresponding data of column 2 should be merged with the data of the previous row in column 2. For example, the 40 in row 2 should be added to the 10 in row 1, then row 2 should be set to 0 (as shown in the modified sample text file). The problem with my code below is that it only records changes in the current data time(i,1) but not the changes made for the previous data.

original.txt    
    a  time c
    1  10   0
    2  40   1
    3  20   0
    4  11   0
    5  40   1

modified.txt    
    a  time c
    1  50   0
    2  0    0
    3  20   0
    4  51   0
    5  0    0




fid=fopen('data.txt');
A=textscan(fid,'%f%f%f');

a   =A{1};
time=A{2};
c   =A{3};

fclose(fid);

fid=fopen('newData.txt','wt');

for i=1:size(a)
  if c(i,1)==1
    time(i-1,1)=time(i,1)+time(i-1,1); % merge the time of the current and the previous
    time(i,1)  =0; %set the time to 0

    array = []; %empty the array
    array = [a(i,1) time c(i,1)]; add new data
    format short g;
    fprintf(fid,'%g\t %g\t %g\n',array);
end
fclose(fid)
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T07:57:11+00:00Added an answer on May 15, 2026 at 7:57 am

    The reason the current time value is written properly but the previous one isn’t is because you have already written the previous one to the file on the previous loop iteration, so there is no way for you to change it. You need to remove the printing from within the loop and add it after you adjust all of the time values.

    You can also take advantage of vectorization by using the FIND function instead of a for loop. You also only need one call to FPRINTF to output all the data. Try this:

    a = [1; 2; 3; 4; 5];          %# Sample data
    time = [10; 40; 20; 11; 40];  %# Sample data
    c = [0; 1; 0; 0; 1];          %# Sample data
    
    index = find(c == 1);                %# Find indices where c equals 1
    temp = time(index);                  %# Temporarily store the time values
    time(index) = 0;                     %# Zero-out the time points
    time(index-1) = time(index-1)+temp;  %# Add to the previous time points
    c(index) = 0;                        %# Zero-out the entries of c
    
    fid = fopen('newData.txt','wt');              %# Open the file
    fprintf(fid,'%g\t %g\t %g\n',[a time c].');  %'# Write the data to the file
    fclose(fid);                                  %# Close the file
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below will print all of the text from the sample text file
Below is my sample text file { Here is my schema file [Sample File.txt]
Below my sample html file: some text here <img src=http://site.com/7b399e20/77165/5fa/2a31ffb8.jpg/> sometext here some text
Hi i created one sample application that saves data as text file using simple
I have a simple text dictionary file, which contains words, separated by ';'.My problem
I have a very simple python script that should scan a text file, which
I have a simple java code that reads text csv file that contains sentences
I have tried using a example file using PHPExcel script to extract data from
I am reading a text file and processing some records, a relevant sample of
I have a text file, I am told the delimiter is as quoted below.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.