New to Matlab, need help on first script

조회 수: 7 (최근 30일)
Kevin
Kevin 2012년 9월 2일
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
Dishant Arora 2012년 9월 2일
you have inbuilt function 'bin2dec' for the conversion.
Image Analyst
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
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
  댓글 수: 2
Kevin
Kevin 2012년 9월 2일
I'm actually experimenting both here...Just trying to learn as much as possible. I used the help command and got this, which i think is similar to yours...I really like the dialog window, it just seemed more complicated. I can't figure out how to store the input from the user. Any ideas?
prompt={'What is the s-value?','What is the e-value','What is the f-value'};
name='Please enter the values';
numlines=1;
defaultanswer={' ',' ',' '};
options.Resize='on';
options.WindowStyle='normal';
options.Interpreter='tex';
answer=inputdlg(prompt,name,numlines,defaultanswer,options);
Kevin
Kevin 2012년 9월 2일
Also, for the code you supplied, if i try that, i don't get an error message for entering something other than an integer

댓글을 달려면 로그인하십시오.


Dishant Arora
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
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
Kevin 2012년 9월 2일
:) makes sense

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Variables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by