Error using == Matrix dimensions must agree - matlab

my system will give me the a/m error when I input more than 3 characters
a = input('Please type f0 or f1: ' , 's');
if a == 'f0';
Run_f0
elseif a == 'f1';
Run_f1
else
disp('Please enter f0 or f1 only');
end
what should I do to resolve this error?
Thanks in advance

Matlab will compare each character of both strings. If one string is longer than the other one, there is nothing to compare and it will throw an error. You can bypass this by forcing the user to repeat the input until he gives a valid input:
valid = {'f0', 'f1'}
a = input('Please type f0 or f1: ' , 's');
while not(ismember(a, valid)) %// or: while not(any(strcmp(a, valid)))
a = input('Please really type f0 or f1: ' , 's');
end
The user will be asked to really input 'f0' or 'f1'.
As an alternative, you can consider to compare the strings with strcmp():
if strcmp(a, 'f0')
%// something
elseif strmpc(a, 'f1')
%// something else
else
disp('Please enter f0 or f1 only');
end

To compare strings you should use the function strcmp
a='12345'
strcmp('f01',a)
Returns: 0 (False)
a='f01'
strcmp('f01',a)
Returns: 1 (True)

Related

Data type Identification showing wrong output MATLAB

I would like to check if the value that has been entered by the users are Integer or not. I tried to using the below code, but it gives me the wrong output. When I entered a fractional value, it still shows me "it is an Integer".
prompt = {'Please Enter the start time:','Increment:','Stop time:'}; %Asking user to enter the data
dlgtitle = 'Input'; %Creating a title for the dialog box
dims = [1 40]; %Adjusting the dimensions
definput = {'0','0.01','10'}; %Using default values to warn users not to put text instead of number
answer = inputdlg(prompt,dlgtitle,dims,definput); %storing the three items of data the user enters
start = str2num(answer{1}); %Converting start value string to number
increment = str2num(answer{2}); %Converting increment value string to number
stop = str2num(answer{3}); %Converting stop value string to number
if start >= stop
f = msgbox('Start time cannot be greater than end time! Please re-enter!', 'Error!');
elseif increment >= stop
f = msgbox('The increment cannot be larger than the end time! Please re-enter!', 'Error!');
elseif start <= -1
f = msgbox('The start time can only be positive numbers! Please re-enter!', 'Error!' );
elseif stop <= -1
f = msgbox('The end time can only be positive numbers! Please re-enter!', 'Error!');
elseif increment <= -1
f = msgbox('The increment can only be positive numbers! Please re-enter!', 'Error!');
elseif isempty(str2num(answer{1}))
f = msgbox('Input must be a number! Please re-enter!', 'Error!');
elseif isempty(str2num(answer{2}))
f = msgbox('Input must be a number! Please re-enter!', 'Error!');
elseif isempty(str2num(answer{3}))
f = msgbox('Input must be a number! Please re-enter!', 'Error!');
end
start=int16(10); %define 'count' as an 8-bit integer
integer_check=isinteger(start)% is count an integer, 1 if yes, 0 if no
if integer_check==1 % if isinteger returns a ‘1’ display message
disp(' Yes it is an integer')
else % if isinteger returns a ‘0’ display message
disp(' No it is not an integer')
end
If is an integer, it doesn’t have a remainder if is divided over 1. So, instead of
integer_check = isinteger(start)
Use
integer_check = rem(start,1) == 0;

How to redirect user back to start if they enter an incorrect input

I am really new to this. This falls under my else statement but i would like to redirect it back to the start. I was thinking along the lines of
'''
false = input('You have entered an invalid number .Please try again and enter correct the unit (1-3) you wish to convert: ');
while ~x ==1 || x== 2|| x||3
x = input('Enter the unit (1-3) you wish to convert: ');
end
'''
Your conditional statement requires && and not || if you're checking for wrong inputs. This should work.
isInputWrong = true;
while (isInputWrong)
x = input('Enter 1, 2, or 3: ');
isInputWrong = (x ~= 1 && x~=2 && x~=3);
if (isInputWrong)
disp('Your input is wrong');
end
end
I figured it out:
while x<1 || x>3
x = input('You have entered an invalid number. Please Enter the unit (1-3) you wish to convert: ');
end
I like any for this:
while ~any(x, [1 2 3]) % Read: "while x doesn't match any in [1 2 3]"
x = input('You have entered an invalid number. Please enter the unit (1-3) you wish to convert: ');
end

Matlab function return value

I have one program that has function and the problem, return value, it has too many output.
Like exempley: y = text the answer comes up
Error in text (line 2)
if nargin == 0
Output argument "array" (and maybe others) not assigned during call to "
C:\Users\name\Documents\MATLAB\text.m>text".
The program text.m reads a txt file that contains a couple of names and numbers like
exemple:
John doughlas 15986
Filip duch 357852
and so on. The program convert them to 15986 Doughlas John and so on.
function array = text(~)
if nargin == 0
dirr = '.';
end
answer = dir(dirr);
k=1;
while k <= length(answer)
if answer(k).isdir
answer(k)=[];
else
filename{k}=answer(k).name;
k=k+1;
end
end
chose=menu( 'choose file',filename);
namn = char(filename(chose));
fid = fopen(namn, 'r');
R = textscan(fid,'%s %s %s');
x=-1;
k=0;
while x <= 24
x = k + 1;
All = [R{3}{x},' ',R{1}{x},' ',R{2}{x}];
disp(All)
k = k + 1;
end
fclose(fid);
Is there anyway to fix the problem without starting over from scratch?
Grateful for all the answers!
You specify the function output argument in the definition, but you don't assign anything to it in the function body.
For example, in
function y = student(j)
your output is y. So you have to assign something to y.
Read more about functions in MATLAB.
Here is a working example.
The first part is to create a function called 'functionA' in a filename 'functionA.m'. Then put the following code inside:
function result = functionA(N,alpha)
result = 5;
return
end
The second part is to create another Matlab file(i.e. upto you to name it) or you can use the Matlab command window even. Then run the following code:
getresult = functionA(100,10);
getresult
After running you get the following answer:
ans =
5

Matlab, Basics about Loops

I need to ask the user for a temperature, k or K stands for kelvin, c or C stands for celsius and f or F stands for Fahrenheit. How can I put all of those in a loop? I need to keep asking the user until they enter in one of the letters above. This is what I have so far.
tempType = input('What type of temperature would you like to use: ', 's');
value = ['k','K','c','C','f','F'];
while strcmp(tempType, value) == 1
tempType = input('What type of temperature would you like to use: ', 's');
end
I'd rather do like this to avoid two exactly same lines (tested in R2011b):
AllowedTemperature = {'k','K','c','C','f','F'};
SelectedTemperature = '';
while ~any(strcmp(SelectedTemperature,AllowedTemperature))
SelectedTemperature = input('What type of temperature would you like to use: ', 's');
end
disp( [ 'SelectedTemperature: ' SelectedTemperature ] )
You want (length(tempType)~=1 || length(findstr(tempType,value))==0) to be the condition of your while

How to repeat an input if the user didn't type in anything and edit the entered input in matlab?

For example I have
x = input('value = ?')
how can I repeat this command if the user just press enter without any input and I would like to know is there anyway I could edit my input like just pressing the 'backspace' after I have keyed in.
Like I got two input variable now'
x = input('??');
y = input('???');
can I edit my first input if I have insert the first input data for x when the input function y is prompted out.
sincerely thanks for anyone that willing to give some helps.
For the 1st case:
I would like to have a code something like this
x = input('value = ?');
while x == %%no input%%
x = input('value = ?'); %prompt the input command again
end
and
while x==error %% I want x in numeric input only
x = input('value = ?'); %prompt the input command again
end
For the first case:
x = input('??'); % if the user just hits 'enter' x is an empty variable
while isempty( x )
x = input('??');
end
For a more robust method
x = str2double( input('Your input here:', 's') );
while ~isnan( x )
x = str2double( input('Your input here:', 's') );
end
the command input('??', 's') returns the input "as-is" and does not try to convert it to a number. The conversion is done via the command str2double. Now, if the input is not a number (double), then str2double returns NaN. This can be captured by isnan.
Hopes this works for you.
to repeat for blanks,
x=''
while isempty(x)
x=input('value=');
end
for non-numeric, you could use something like
x=''
while isempty(x)
try
x=input('value=')
catch me
fprintf('enter a number\n')
end
end