Question

Given a filename and a sorted (ascending) vector of line numbers representing the lines you want...

Given a filename and a sorted (ascending) vector of line numbers representing the lines you want to keep, print those lines to a new file named '<filename>_summary.txt' . (MATLAB).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

MATLAB CODE:

inputFilename = 'input.txt'; % Name of input file
pages = [1 5 8 10 11]; % Vector having page numbers to keep
outputFilename = strcat(inputFilename, '_summary.txt'); % Name of output file

inputFileID = fopen(inputFilename, 'r'); % Open input file
outputFileID = fopen(outputFilename, 'w'); % Open output file

line = fgets(inputFileID); % Read first line from file
i = 1; % Line number
while ischar(line)
    % If current line number is in pages vector
    % then write the current line to the output file
    if(length(find(pages == i)) == 1)
        fprintf(outputFileID, '%s', line); % Write line to output file
    end
    line = fgets(inputFileID); % Read first line from file
    i = i + 1; % Increment line number
end

fclose(inputFileID); % Close file
fclose(outputFileID); % Close file

INPUT FILE:

FOR ANY HELP JUST DROP A COMMENT

OUTPUT FILE:

Add a comment
Know the answer?
Add Answer to:
Given a filename and a sorted (ascending) vector of line numbers representing the lines you want...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the following function: def get_largest(filename): input_file = open(filename, 'r') lines = input_file.readlines() input_file.close() largest =...

    Consider the following function: def get_largest(filename): input_file = open(filename, 'r') lines = input_file.readlines() input_file.close() largest = 0 for line in lines: items_list = line.split() for element in items_list: value = float(element) if value > largest: largest = value return largest The function takes a filename as a parameter and returns the largest number in the file as a float. Modify the above function and use a try-except-else block to handle exceptions that may occur and handle the FileNotFoundError in your...

  • C++ Write a function parseScores which takes a single input argument, a file name, as a string. Your function should read each line from the given filename, parse and process the data, and print the r...

    C++ Write a function parseScores which takes a single input argument, a file name, as a string. Your function should read each line from the given filename, parse and process the data, and print the required information. Your function should return the number of student entries read from the file. Empty lines do not count as entries, and should be ignored. If the input file cannot be opened, return -1 and do not print anything.     Your function should be named...

  • Modify the function to return a copy of the given array sorted in ascending order (1,...

    Modify the function to return a copy of the given array sorted in ascending order (1, 2, 3, etc). Do not modify the original array. function copyAndSortNumbers(numbers) { } /* Do not modify code below this line */ const original = [1, 7, 3, 5]; const sortedCopy = copyAndSortNumbers(original); console.log(original, '<-- should be [1, 7, 3, 5]'); console.log(sortedCopy, '<-- should be [1, 3, 5, 7]');

  • Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file...

    Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesn’t exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of...

  • class FileUtility: def __init__ (self, filename): self.__filename = filename def displayFile(self): # use loop structure to...

    class FileUtility: def __init__ (self, filename): self.__filename = filename def displayFile(self): # use loop structure to read and print each line of the file def main(): #1. create a new file and write the follwing lines to it. # #I never saw a Purple Cow, #I never hope to see one, #But I can tell you, anyhow, #I’d rather see than be one! # #2. create an object of FileUtility class #3. call the object's displayFile method main()

  • Implement a Java application for the following: We want to keep track of a Movie Collection....

    Implement a Java application for the following: We want to keep track of a Movie Collection. Each Movie in the collection will contain: Title, Genre, Year (4 digits) and Runtime (double - ex. 2.1 (hrs)). Your program will read movies from a local text file named movies.txt in the current project directory in Eclipse. Each line in the text file contains one Movie with the fields separated by commas. Read the text file and load the movies in to a...

  • In a linux command line Give AWK commands for accomplishing each of the following: Print the...

    In a linux command line Give AWK commands for accomplishing each of the following: Print the 2nd last field (the field directly before the last field) of each line from a file named 'last.txt' Assume you have a file called 'names' that contains a list of people, one person per line. Also, assume that on each line the 3rd field on that line contains the age of the person. Some of the people do not have an age listed, and...

  • I need help with this .java implementation: Implement an algorithm that given two sorted arrays of...

    I need help with this .java implementation: Implement an algorithm that given two sorted arrays of size m and n creates a sorted array of size (n + m) in O(n + m) time. The script must take as an input a command line argument specifying the name of a .txt file which will contain the arrays to be sorted. Each line in the .txt file will contain two arrays, with elements in arrays separated by commas and the arrays...

  • matlab Example: Function Name: battleFormation formation([3 5 11, formation1.txt) Inputs: (double) 1xN vector of the number...

    matlab Example: Function Name: battleFormation formation([3 5 11, formation1.txt) Inputs: (double) 1xN vector of the number of soldiers each line 2. (char)A file name, indluding extension, for the output file group number Formation1.txt: 1 Line 1 has the following line up: e 1 1 1 0 File Outputs: 21 Line 2 has the folloOwing line up: 1 1 1 1 1 1. A text file showing the soldiers formations 3 Line 3 has the following line up: e e 1...

  • Create a program SearchNewBooks.cc that given as command line input newbooks.dat and request.dat and a method...

    Create a program SearchNewBooks.cc that given as command line input newbooks.dat and request.dat and a method of search either binary or linear, creates a file of the number of books found from the request list. program should receive two filenames as command line arguments, the new books file and request file, which the program should read in. The list of new books and list of requested books should should be stored in only two ​std::vector​ containers. Because some code to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT