New to Matlab, need help on first script
4 views (last 30 days)
Show older comments
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 Comments
Image Analyst
on 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?
Answers (2)
Image Analyst
on 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
Dishant Arora
on 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 Comments
Dishant Arora
on 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.
See Also
Categories
Find more on Variables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!