Get Data From Input Box In Mathematica - wolfram-mathematica

I want to get a Number Using "Input" Command In Mathematica and Put it in a Variable.But When I Use This Command And Enter a Number,It Returns "Null".Here's the Code:
In[1]:= var = Input["Please Enter an Integer"];
Sqr = var^2
Out[2]:= Null^2

Hummm. I just tried this
In[1]:= var = Input["Please Enter an Integer"];
Sqr = var^2
Out[2]= 121
Maybe try starting Mathematica fresh, just type exactly your two lines of code, hit shift and enter, tap the 1 key twice to see 11 in the input box and click the OK button. Don't do anything else, no extra mouse clicks, no executing a bunch of stuff first and report what happens.

Related

how to not allow the textbox text (number only) to start with 0 (zero)

i have an exercise to make something like a calculator. 7 TextBox. It should look like
1/2 + 2/3 = 7/6 where every number is in his own box and the + has one too. must fill in the boxes before "=" numbers ( and + ; - ; * ; / in the one in the middle) and then check with button if there are not some letters or symbols. ok made it, but the teacher ask me to write 013 in the textbox, so.. how to make it like that so if the numbers start with 0 then after clicking the button it give me error. something like:
if ...... then
msgBox("ERROR")

Visual Basic: Increasing integer every 3 clicks

So my question is pretty much what the title says: how do I get a certain integer to increase by 1 for every three clicks of a button? For example, let's say the integer is 900. I click button1 once, then again, then again, and on the third click the integer changes to 901. How can this be accomplished?
Thanks in advance.
Use a counter that is actually three times larger behind the scene. Start at 2700 instead of 900, and increase the value by one for each click.
When you want to display the value that increases every third click, you divide the counter by three:
displayValue = counter \ 3
Try this maybe this could help or give you idea
Dim click = 0
Dim num = 900
if (click % 3 = 0)
num+=1
else
click+=1
end if

MATLAB: Modify getline() to remove 'end input on double-click' functionality

The matlab function getline (image processing toolbox) returns the position of a polyline (which has previously been defined with the cursor) either on a double-click or on pressing the return key or spacebar.
Due to my butter-fingers and accidentally double-clicking I want to remove the ability to end on a double-click.
What part do I need to change, or what functions should I be looking out for, I can't find out how a double click is even defined in matlab.
Cheers!
MATLAB associates "callback" functions with graphics objects, which define what to do when the mouse is clicked, keys are pressed, etc.. In getline(), the section to look at is the NextButtonDown() subfunction. This is the callback that is associated with subsequent mouse presses after the first mouse press to initiate the line. The key is that is checks the SelectionType figure property, which will be open for a double click. When that is the case, it closes the figure. So, to disable that functionality, just remove the extra case and checking logic. Here is the diff for my r2009b version:
306,310d305
< selectionType = get(GETLINE_FIG, 'SelectionType');
< if (~strcmp(selectionType, 'open'))
< % We don't want to add a point on the second click
< % of a double-click
<
322,328d316
<
< end
<
< if (~strcmp(get(GETLINE_FIG, 'SelectionType'), 'normal'))
< % We're done!
< set(GETLINE_H1, 'UserData', 'Completed');
< end
The answer provided by #JohnColby solves your problem by editing the GETLINE.m function file. Basically you comment out every line that check if a double-click was performed. This information is obtained by querying the 'SelectionType' figure property.
Alternatively, if you are like me and you hate making changes to built-in functions, then consider the following solution that doesn't involve changing any existing functions. Here is an example of how we use it:
h = addlistener(handle(gcf), 'WindowButtonDownFcn', 'PostSet', #changedWBDFcn);
[x,y] = getline();
delete(h)
plot(x,y, 'Color','r')
The idea is to create an event listener that gets triggered when the 'WindowButtonDownFcn' figure property changes. We use it to insert a function that gets called just before the previously set callback function (actually we replace the callback with our own function that calls the old one at the end).
This allows us to insert a section that checks if the call was triggered by a double-click, and simply skip such event.
This had to be done twice, because GETLINE first calls FirstButtonDown on first click, which sets NextButtonDown to be called on subsequent clicks, thus the use of the flag variable to differentiate between the two cases.
The code for the above event listener function:
function changedWBDFcn(src,ev,flag)
hFig = ev.AffectedObject; %# figure handle
currFcn = ev.NewValue; %# current callback function
delete(src); %# delete event listener
if nargin < 3, flag = false; end %# determine flag
%# hijack WindowButtonDownFcn function
set(hFig, 'WindowButtonDownFcn',{#wbdFcn,currFcn,flag})
%# callback function
function wbdFcn(o,e,currFcn,flag)
%# skip anything but single-clicks
if ~strcmpi(get(hFig,'SelectionType'),'normal')
return
end
%# evaluate previous callback function
hgfeval(currFcn) %# getline('FirstButtonDown'),getline('NextButtonDown')
%# repeat process after first click
if flag
addlistener(handle(hFig), 'WindowButtonDownFcn', ...
'PostSet', {#changedWBDFcn,true});
end
end
end

MATLAB exiting loop in function when using GUI

I have a function which runs when a button is clicked and that function will call another function to perform plotting, which is an animation composed by a series of loops. Problem is, after the program is run, it cannot be stopped unless pressing Ctrl+C which then cause the whole .exe to fail and require reopening it.
I want to make a Reset button, so to stop the loop from running, but how to pass a new variable from the interrupt to the existing function which is two level above the stack?
Thank You
edit # 22:13 27/3/2011 (UKT)
a more clearer of what I am trying to do:
function push_calculate_Callback(hObject, eventdata, handles)
(List of parameters read in the GUI)
fmain(list of paramters required to pass to the function, fmain)
that's first part of the code which linked directly to the GUI, the fmain looks like:
function fmain(List of parameters)
(List of calculations...)
fplotting(list of paramters for plotting)
in which the fplotting is where the animation comes out from, inside the fplotting:
function fplotting(list of parameters)
for i = 1:(end)
(do the animation plot)
end
here is the loop where I want to stop when I press the reset button. coz if anyone press Ctrl+C when the animation is undergoing (looping), it will give an error and the exe file will crash and requires reopening it. So what I want it be able to stop it when someone press the reset button.
If I use persistent, the new value, after I press the reset button, it will not pass into the existing loop and break the loop... looks like I am looking for a way to update the parameter after I have change it outside the function
Thanks
Use exit or quit to exit the program
To stop the loop from running, you can use break or return from a function..
Your best bet might be to use a PERSISTENT variable whose state you check at each iteration of the loop in function 2, and whose value you modify in the "reset"-callback.
reset callback
function reset_callback(hObj,eventdata,handles)
%# "activate" persistent variable
persistent breakThis
%# set it to 1
breakThis = true;
function with the loop
function functionWithTheLoop(someInput)
...
%# "activate persistent variable
persistent breakThis
...
%# start loop
for iter = 1:numOfIterations
%# check whether you need to break out of the loop
if breakThis
%# reset the function value
breakThis = false;
break;
end
...
end
You can use application data information, i.e. setappdata and getappdata:
% animation loop function
function loop(hFig)
setappdata(hFig, 'run', true); % here or somewhere else...
while(getappdata(hFig, 'run'))
plot(....);
end
end
% GUI stop-loop btn callback
function stop(hFig)
setappdata(hFig, 'run', false);
end
I guess that would work.

Problems with GUI in Matlab

I have such code:
a=5;
b=a;
c=10;
u = (0:0.05*pi:2*pi)'; %'
v = [0:0.05*pi:2*pi];
X = a*sin(u)*cos(v);
Y = a*sin(u)*sin(v);
Z = c*cos(u)*ones(size(v));
Z(Z>0)=0; % cut upper
V1=4/3*pi*a*b*c;
d=1/2;
e=2^d;
a2=a/e;
b2=a/e;
c2=c;
V2=4/3*pi*a2*b2*c2;
X2 = a2*sin(u)*cos(v);%-2.5;
Y2 = b2*sin(u)*sin(v);
Z2 = c2*cos(u)*ones(size(v));%+0.25;
Z2(Z2>0)=0; % cut
h=1/3;
for j = 1:20
k1=(sin(pi*j/20)+0.5)^h;
a=a*k1;
c=c*k1;
X = a*sin(u)*cos(v);
Y = a*sin(u)*sin(v);
Z = c*cos(u)*ones(size(v));
Z(Z>0)=0;
a2=a2*k1;
b2=a2*k1;
c2=c2*k1;
X2 = a2*sin(u)*cos(v)+5;%-2.5;
Y2 = b2*sin(u)*sin(v);
Z2 = c2*cos(u)*ones(size(v));%+0.25;
Z2(Z2>0)=0;
hS1=surf(X,Y,Z);
alpha(.11)
hold on
hS2=surf(X2,Y2,Z2);
hold off
axis([-20 20 -20 20 -20 20]);
F(j) = getframe;
end
movie(F,4)
I have to input parameters a,b,c from the keyboard. I've made GUI & tried to do it by using "Edit text" with a function below, but it's not working((.
I can't understand what's the problem with it.
function a_edit_Callback(hObject, eventdata, handles)
user_entry = str2double(get(hObject,'string'));...
a=user_entry;
The problem is that your callback function executing your code is not 'seeing' the parameters you defined in your edit text callbacks. You need to establish your variables in the subfunction, since they aren't global.
Using guide, set up a uicontrol button to click when you've entered your parameters into your uicontrol edit text boxes. Under the callback of your button, place your above code, with the following at the top:
a=str2double(get(handles.a_edit,'String'));
b=str2double(get(handles.b_edit,'String'));
c=str2double(get(handles.c_edit,'String'));
This will pull in the current strings of your edit text uicontrols. (Assuming you've assigned the tag format x_edit for each of the edit text boxes in guide.)
EDIT:
Open the figure you already created with the edit text boxes. Next, check to make sure each of your text boxes have the tag a_edit, b_edit, c_edit by using the property inspector. Then create a button using guide, and open the property inspector by double clicking on it. Find the 'tag' field, and name it run. Save your figure, and open the corresponding M-file.
Next, find the line with run_Callback(hObject, eventdata, handles). Place the following under it:
a=str2double(get(handles.a_edit,'String'));
b=str2double(get(handles.b_edit,'String'));
c=str2double(get(handles.c_edit,'String'));
%# Add the rest of your code from above verbatim, minus the first three lines
This should be the ONLY code you add to the auto-generated M-file - don't mess with anything else until you get this much working. If you don't want the animation popping up randomly in your figure window, you can add a set of axes using guide as well.
From the looks of the code, it appears to be a 'script' and not a 'function'.
Did you just want a 'dialog (built-in GUI dialog)'? If so, you can add the following at the beginning of your script:
prompt = {'Enter the parameter value "a":','Enter the parameter value
"b":','Enter the parameter value "c":'};
dlg_title = 'Input the Parameter Values';
num_lines = 1;
def = {'5','5','10'};
answer = inputdlg(prompt,dlg_title,num_lines,def);
a=answer{1};a=str2double(a);
b=answer{2};b=str2double(b);
c=answer{3};c=str2double(c);
% Y.T.

Resources