New to Matlab, need help on first script
이전 댓글 표시
First, I have never used Matlab previously, and the only software similar to this which I have used is Maple, so I may be hard to help at first but I usually pick things up quickly, so thanks for your patience...(and I have taken some java previously)
Here is the problem
"Write a MATLAB program that receives logical inputs s, e, and f (for the sign, exponent, and the fraction mantissa in the IEEE 754-2008 64 bit standard format) and outputs the corresponding decimal number. Test your program on various inputs including:
While I don't want the exact script to solve this, i would like to know which direction I should go...so here is my thinking
Start with a code that asks for logical input for s,e,f values. So this is what I got..
**UPDATE NOW I"M HERE****
s = input('What is the s-value? ');
e = input('What is the e-value? ');
f = input('What is the f-value? ');
s1 = bin2dec('s');
e1 = bin2dec('e');
f1 = bin2dec('f');
x = (-1)^s1 * 2^(e1-1023) * (1+f1)
disp('x')
************************
I'm getting this error,
******************
??? Error using ==> bin2dec at 54
Binary string may consist only of characters 0 and 1
Error in ==> Homework at 9
s1 = bin2dec('s');
***********
Does anyone now why...as a test I put in s=1, e=1, f=1
댓글 수: 2
Dishant Arora
2012년 9월 2일
you have inbuilt function 'bin2dec' for the conversion.
Image Analyst
2012년 9월 2일
Hmmm... not as robust and user friendly as the code I offered you. Any reason why you don't like dialog boxes and validating/checking your user's inputs?
답변 (2개)
Image Analyst
2012년 9월 2일
Here's a little snippet you may study and start with:
% Ask user for a number.
defaultValue = 45;
titleBar = 'Enter a value';
userPrompt = 'Enter the integer';
caUserInput = inputdlg(userPrompt, titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
integerValue = round(str2num(cell2mat(caUserInput)));
% Check for a valid integer.
if isnan(integerValue)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
integerValue = defaultValue;
message = sprintf('I said it had to be an integer.\nI will use %d and continue.', integerValue);
uiwait(warndlg(message));
end
Dishant Arora
2012년 9월 2일
s = input('What is the s-value? ');
e = input('What is the e-value? ');
f = input('What is the f-value? ');
s1=bin2dec(num2str(s));
e1 = bin2dec(num2str(e));
f1 = bin2dec(num2str(f));
x = (-1)^s1 * 2^(e1-1023) * (1+f1);
댓글 수: 6
Dishant Arora
2012년 9월 2일
logical inputs must be class char, num2str converts s,e,f into strings. or you can directly input strings to s,e,f
Kevin
2012년 9월 2일
Dishant Arora
2012년 9월 2일
편집: Dishant Arora
2012년 9월 2일
when you write s = input('What is the s-value? '), user can input any thing, a string, number, so that's need to be taken care of. if you want it to be read as string you must enclose input in quotes(''), other wise matlab considers it as a number.
where as s = input('What is the s-value? ','s'), reads input as string only.
Kevin
2012년 9월 2일
Dishant Arora
2012년 9월 2일
it should be
s = input('What is the s-value? ','s')
e = input('What is the e-value? ','s');
f = input('What is the f-value? ','s');
's' here means character string input.
Kevin
2012년 9월 2일
카테고리
도움말 센터 및 File Exchange에서 Variables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!