Question

Function / File Read (USING MATLAB) A) create a user-defined function : [maxsample , maxvalue , numsamples]=writetofile(sname,strgth) to do the following: The user-defined function shall: -Accept as t...

Function / File Read (USING MATLAB)

A) create a user-defined function :

[maxsample , maxvalue , numsamples]=writetofile(sname,strgth) to do the following:

The user-defined function shall:

-Accept as the input a provided string for the sample name (sname) and number for strength (strgth)

- open a text file named mytensiledata.txt, with permission to read or append the file.

- Make sure to capture any error in opening the file

- Write sname and strgth to the file

- Close the file

- Open the file again with read access and read the data into memory

- Of the values read, calculate the maximum the strength values and assign the variable : maxvalue

- Assign the sample name with the max strength to the variable: maxsample

- Assign the total number of samples read to the variables : numsamples

B) Calling Script

Create a calling script and call the user function you created (writetofile) four separate times to write the data for each sample to the file mytensildata.txt one at a time. Display the output of writetofile after each call to the function

NOTE: You don't need to use the input() command bring these data in memory

SAMPLE 1, 50.4

SAMPLE 2, 70.5

SAMPLE 3 , 65.3

SAMPLE 4, 100.8

You will likely need to delete mytensildata.txt and run the program one final time to obtain the correct data in the text file. If you get an error while trying to delete .Type close('all') in the command window.

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

% function writetofile.m
function [maxsample , maxvalue , numsamples] = writetofile(sname,strgth)
fileExists = false;
if(exist('mytensiledata.txt','file'))
fileExists = true;
end
[fid,msg] = fopen('mytensiledata.txt','a+');
if (~isempty(msg))
disp('Error in open file mytensiledata.txt');
else
if(fileExists)
fprintf(fid,'\n%s %f',sname,strgth);
else
fprintf(fid,'%s %f',sname,strgth);
end
fclose(fid);
fid = fopen('mytensiledata.txt','r');
data = textscan(fid,'%s%f');
fclose(fid);
maxsample = data{1}{1};
maxvalue = data{2}(1);
numsamples = 1;
  
for i=2:length(data{1})
numsamples = numsamples + 1;
if(data{2}(i) > maxvalue)
maxvalue = data{2}(i);
maxsample= data{1}{i};
end
end
end
  
end

%end of function

%main script


[maxsample , maxvalue , numsamples] = writetofile("SAMPLE1",50.4);
fprintf('MaxSample : %s MaxValue : %.1f Number of samples : %d\n',maxsample,maxvalue,numsamples);
[maxsample , maxvalue , numsamples] = writetofile("SAMPLE2",70.5);
fprintf('MaxSample : %s MaxValue : %.1f Number of samples : %d\n',maxsample,maxvalue,numsamples);
[maxsample , maxvalue , numsamples] = writetofile("SAMPLE3",65.3);
fprintf('MaxSample : %s MaxValue : %.1f Number of samples : %d\n',maxsample,maxvalue,numsamples);
[maxsample , maxvalue , numsamples] = writetofile("SAMPLE4",100.8);
fprintf('MaxSample : %s MaxValue : %.1f Number of samples : %d\n',maxsample,maxvalue,numsamples);

%end of main script

Output:

Command Line:

MaxSample SAMPLE1 MaxValue 50.4 Number of samples 1 MaxSampleSAMPLE2 MaxValue : 70.5 Number of samples 2 MaxSampleSAMPLE2 Max

File:

SAMPLE1 50.400000 SAMPLE2 70.500000 SAMPLE3 65.300000 SAMPLE4 100.800000

Add a comment
Know the answer?
Add Answer to:
Function / File Read (USING MATLAB) A) create a user-defined function : [maxsample , maxvalue , numsamples]=writetofile(sname,strgth) to do the following: The user-defined function shall: -Accept as t...
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
  • Using MATLAB. Create a script file that calls a user-defined function within a for/end loop to...

    Using MATLAB. Create a script file that calls a user-defined function within a for/end loop to symbolically compute the derivative of ln(x) on the interval from -5 to 5, containing 100 data points. Your script file should output a plot of the derivative of ln(x) (taken from your function) vs x. Use a line with circular markers for the plot and include a grid and appropriate labels. The user-defined function should 1. Receive a single x value as input. 2....

  • Write a user-defined function in C++ to read the content from a text file OUT.TXT, count...

    Write a user-defined function in C++ to read the content from a text file OUT.TXT, count and display the number of alphabets present in it

  • Question 1: Creating a user-defined function Write a user-defined MATLAB function named PBTask4pl_f.m for the following...

    Question 1: Creating a user-defined function Write a user-defined MATLAB function named PBTask4pl_f.m for the following math function with x the input argument and y the output y(x)=0.8x4-13x2-5x The function should work for x being a scalar or a vector. Write a script file named PBTask4pl.m to a) Use the function to calculate y(3) and y(5) and display the results in command window b) Use the function to make a plot of the function y(x) for -5:5: x 5:5. Label...

  • c++ program8. Array/File Functions Write a function named arrayToFile. The function should accept three arguments:...

    c++ program8. Array/File Functions Write a function named arrayToFile. The function should accept three arguments: the name of a file, a pointer to an int array, and the size of the array. The function should open the specified file in binary mode, write the contents of the array to the file, and then close the file. Write another function named fileToArray. This function should accept three argu- ments: the name of a file, a pointer to an int array, and the size...

  • Need help on C++ (User-Defined Function) Format all numerical decimal values with 4 digits after ...

    need help on C++ (User-Defined Function) Format all numerical decimal values with 4 digits after the decimal point. Process and sample run: a) Function 1: A void function that uses parameters passed by reference representing the name of a data file to read data from, amount of principal in an investment portfolio, interest rate (any number such as 5.5 for 5.5% interest rate, etc.) , number of times the interest is compounded, and the number of years the money is...

  • A. File I/O using C library functions File I/O in C is achieved using a file...

    A. File I/O using C library functions File I/O in C is achieved using a file pointer to access or modify files. Processing files in C is a four-step process: o Declare a file pointer. o Open the desired file using the pointer. o Read from or write to the file and finally, o Close the file. FILE is a structure defined in <stdio.h>. Files can be opened using the fopen() function. This function takes two arguments, the filename and...

  • Use two files for this lab: your C program file, and a separate text file containing...

    Use two files for this lab: your C program file, and a separate text file containing the integer data values to process. Use a while loop to read one data value each time until all values in the file have been read, and you should design your program so that your while loop can handle a file of any size. You may assume that there are no more than 50 data values in the file. Save each value read from...

  • Create a python script to manage a user list that can be modified and saved to...

    Create a python script to manage a user list that can be modified and saved to a text file. Input text file consisting of pairs of usernames and passwords, separated by a colon (:) without any spaces User choice: (‘n’-new user account, ‘e’-edit existing user account, ‘d’- delete existing user account, ‘l’- list user accounts, ‘q’-quit) List of user accounts, error messages when appropriate Output text file consisting of pairs of username and passwords, separated by a colon (:) without...

  • MATLAB Problem HW7P2 (20 points) (5 pts) Write a user-defined MATLAB function called HW7P2_fn for the...

    MATLAB Problem HW7P2 (20 points) (5 pts) Write a user-defined MATLAB function called HW7P2_fn for the following math function 3 o-0.47x The input to the function is x and the output is y. Write the function such that x can be an array (use element-by-element operations) (15 pts) Use the function in (a) the command window to calculate y(-2) and y(5) (b) a script file HW7P2.m to determine y(x) for 0.001 Sx S 10 with 1000 points. Hint: Use the...

  • Conditional statements, loops, reading from file, user defined functions.

    # in C Project objective: Conditional statements, loops, reading from file, user defined functions.**Submit source code (prog3.c) through CanvasOne source code file(unformatted text) will be submittedHere is INCOMPLETE code to get started: prog3.cHere is input.txt file: input.txtThe file name must match the assignmentThe code should be tested and run on a Microsoft compiler before it is uploaded onto CanvasThe code must be submitted on time in order to receive credit (11:59PM on the due date)Late submissions will not be accepted or gradedAll...

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
ADVERTISEMENT