New to Matlab, need help on first script

4 Ansichten (letzte 30 Tage)
Kevin
Kevin am 2 Sep. 2012
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 Kommentare
Dishant Arora
Dishant Arora am 2 Sep. 2012
you have inbuilt function 'bin2dec' for the conversion.
Image Analyst
Image Analyst am 2 Sep. 2012
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?

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Image Analyst
Image Analyst am 2 Sep. 2012
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 Kommentare
Kevin
Kevin am 2 Sep. 2012
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 am 2 Sep. 2012
Also, for the code you supplied, if i try that, i don't get an error message for entering something other than an integer

Melden Sie sich an, um zu kommentieren.


Dishant Arora
Dishant Arora am 2 Sep. 2012
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 Kommentare
Dishant Arora
Dishant Arora am 2 Sep. 2012
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 am 2 Sep. 2012
:) makes sense

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by