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 ビュー (過去 30 日間)
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

採用された回答

VBBV
VBBV 2022 年 12 月 13 日
text=fscanf(file_id,'%s',a)

その他の回答 (1 件)

Jan
Jan 2022 年 12 月 13 日
編集済み: Jan 2022 年 12 月 13 日
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.

カテゴリ

Help Center および File ExchangeLow-Level File I/O についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by