In matlab write a script (not a function), that uses a “while” loop to store a s
ID: 3674772 • Letter: I
Question
In matlab write a script (not a function), that uses a “while” loop to store a series of non-negative numbers entered
by the user. Each iteration of the loop will print one number to the file. Below is an example in which
the user is asked to enter ages. You may use any data and filename that you wish.
Command Window
>> problem1
Enter an age (negative to quit): 18
Enter an age (negative to quit): 29
Enter an age (negative to quit): 20
Enter an age (negative to quit): 21
Enter an age (negative to quit): -1
File contents will be:
18
29
20
21
Explanation / Answer
I have written this below matlab code to read the data using while loop condition for terminating with negtive value..
and stored the result in file..
prompt = 'Enter an age(negative to quit):';
fid=fopen('MyFile.txt','w');
while x<0
x = input(prompt)
fprintf(fid, [ x ' ']);
end
fclose(fid);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.