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 commentaires

Dishant Arora
Dishant Arora le 2 Sep 2012
you have inbuilt function 'bin2dec' for the conversion.
Image Analyst
Image Analyst le 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?

Connectez-vous pour commenter.

Réponses (2)

Image Analyst
Image Analyst le 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 commentaires

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 le 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

Connectez-vous pour commenter.

Dishant Arora
Dishant Arora le 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 commentaires

Dishant Arora
Dishant Arora le 2 Sep 2012
logical inputs must be class char, num2str converts s,e,f into strings. or you can directly input strings to s,e,f
Kevin
Kevin le 2 Sep 2012
So, where is my problem here... When i say "s = input('What is the s-value? ');" Does that mean I'm inputting a number? Or does that come in as a string value, which i then need to convert.? Thanks for your help
Dishant Arora
Dishant Arora le 2 Sep 2012
Modifié(e) : Dishant Arora le 2 Sep 2012
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.
For some reason I now get this error
??? Error using ==> input
The second argument to INPUT must be 's'.
Error in ==> Homework at 17 e = input('What is the e-value? ','e')
for my code
s = input('What is the s-value? ','s')
e = input('What is the e-value? ','e')
f = input('What is the f-value? ','f')
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 le 2 Sep 2012
:) makes sense

Connectez-vous pour commenter.

Catégories

En savoir plus sur Variables dans Centre d'aide et File Exchange

Tags

Question posée :

le 2 Sep 2012

Community Treasure Hunt

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

Start Hunting!

Translated by