Below is the question and together with it is my code... but there is always error at the fscanf function.. Can anyone help me pls??

2 views (last 30 days)
Write a script, which first asks the user to input an integer number between 0 and 255, then
outputs the number to the screen between the characters '<' and '>', and calls a user function
mcheck’(as described below)to check the number entered. The input, display and check operation
should be repeated for 10 times. After the 10th iteration, the script should save all the input
numbers to a text file in a human readable format.
The user function ‘mcheck’ mentioned above will do the following check for each number entered:
if the number is in the range of 0 to 255, no change is needed; if the number entered is bigger
than 255, the script should print a message “error: The value too big.” to the screen, but make no
change to the value of the number; if a negative number is entered, no matter what is the exact
number, the script should convert it to -1.
Here's my code:
function mcheck(a)
file=[]
for n=1:10
a= input("Enter Value Between 0 To 255:")
if a<0
a=-1
elseif a>255
disp('error, the value is too big')
else
a=string(a)
file_id=fopen('file','w')
fprintf(file_id,'%s',a)
fclose(file_id)
end
end
file_id=fopen('file','r')
text=fscanf('file','%s',a)
disp(text)
fclose('file')
end

Accepted Answer

VBBV
VBBV on 13 Dec 2022
text=fscanf(file_id,'%s',a)

More Answers (1)

Jan
Jan on 13 Dec 2022
Edited: Jan on 13 Dec 2022
The problem is hidden here:
file_id=fopen('file','r')
text=fscanf('file','%s',a)
% ^^^^^^ ^ ???
fscanf expects the file identifier stored in file_id, not the CHAR vector 'file'.
What is the purpose of the trailing argument a? Omit it.

Tags

Community Treasure Hunt

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

Start Hunting!