How to create Input dialog box in matlab? - matlab

I want to create input dialog box in matlab. I am performing simple addition operation in MATLAB.
Two variables name, a and b are needed to be given by user and then performing addition c=a+b; and display it in output. Both a and b should be positive integer only.
I tried following:
a = inputdlg({'Enter positive integer (a)'});
b = inputdlg({'Enter positive integer (b)'});
c=a+b;
But it is giving following error:
Undefined function or method 'plus' for input arguments of type
'cell'.
Please suggest how can i code the above program in described way.

That's because the output of inputdlg is a cell array containing a string; here a 1-cell array.
Hence you need to access the content of the cell array to perform the operation; for example using {curly brackets} : {a} and {b}.
In your case, since you are asking the use for a number, you need to convert the output, which is a string, to an actual number Matlab can use using for instance str2double, which operates on cell arrays
c = str2double(a) + str2double(b)

Related

How to convert EditField value to cell array?

I want to take a cell array from the user containing the number of zeros and poles of each transfer function in a system identification app that I am designing in MATLAB's app designer.
User enters something like this:
{[2,1], [1,0]; [1,0], [2,1]}
EditField or TextArea treats this input as a char array or string, But I want to re-convert it to a cell array of numbers, not strings. How is that possible?
You can use eval to evaluate the string to get the resulting numbers. This works if it has numbers, variables and functions accessible from the workspace where you are running eval. See documentation for eval at https://www.mathworks.com/help/matlab/ref/eval.html. If there is a variable in the expression for example as, {[2,1], [1,0]; [1,0], a} with a defined in the base workspace then you need to use evalin. evalin lets you specify the workspace where the expression needs to be evaluated.
Finally if it is not a cell array and contains only an array of numbers then str2num also can do the job of converting the string to numbers.

MATLAB cell array of function handles - How does it work?

I am trying to understand the following commands of a MATLAB script :
global operatorObj
calcEVR_handles = operatorObj.calcEVR_handles;
m = operatorObj.nInputs
E = zeros(m,1);
V = zeros(m,1);
R = zeros(m,m);
for i=1:m
[E(i), V(i), R(i,i)] = calcEVR_handles{i}(t,x);
end
What can calcEVR_handles be, if t is a float and x is a vector?
calcEVR_handles (to me) looks like a cell array where each element is a handle to a function. Each element in calcEVR_handles is an anonymous function that takes in a single value t and a single vector x. As such, by doing calcEVR_handles{i}, you would access the corresponding function stored at the ith element in the cell array. Once you have access, you then pass your parameters to this function and it gives you those three outputs.
To show you an example of this working, consider the following cell array that works similarly to calcEVR_handles.
calcCellFunc = {#sin, #cos, #tan};
This is a three element cell array, where each element is a handle to a function. The # is a special character in MATLAB that denotes that you are creating a handle to a function. It's also used to create anonymous functions, but let's shelve that for this answer. You can read more about it here if you want to delve into more detail regarding this.
Back to our cell array of handles, we will make handles for sin, cos and tan. You can then iterate over your cell array by accessing the function you want by calcCellFunc{idx} where idx is the element you want in the cell array. This will ultimately give you the function stored at index idx. Once you do that, you can then call the function and specify whatever inputs you want (or none if it doesn't take any inputs). Here's a quick example for you. Let's create a random 5 x 5 matrix, and run through each function with this matrix serving as the input. We then take each of these outputs and store them into a corresponding slot in an output cell array. As such:
rng(123); %// Set seed for reproducibility
M = rand(5);
calcCellFunc = {#sin, #cos, #tan};
out = cell(1, numel(calcCellFunc)); %// To store the results for each function
for idx = 1 : numel(calcCellFunc)
out{idx} = calcCellFunc{idx}(M); %// Get the function, then pass
%// the matrix M to it
end
If you want to make things clear, you could split up the out statement to this instead:
func = calcCellFunc{idx}; %// Get access to the function
out{idx} = func(M); %// Pass M to this function
If you're new to handles / anonymous functions, you should probably use the above code first to make it explicitly clear on what MATLAB is doing. You are first getting access to the function you want that is stored in the cell array, and then you pass your arguments to this function.
If we display the output, we get:
>> celldisp(out)
out{1} =
0.6415 0.4106 0.3365 0.6728 0.5927
0.2823 0.8309 0.6662 0.1815 0.7509
0.2249 0.6325 0.4246 0.1746 0.6627
0.5238 0.4626 0.0596 0.5069 0.5737
0.6590 0.3821 0.3876 0.5071 0.6612
out{2} =
0.7671 0.9118 0.9417 0.7398 0.8054
0.9593 0.5564 0.7458 0.9834 0.6604
0.9744 0.7745 0.9054 0.9846 0.7489
0.8518 0.8866 0.9982 0.8620 0.8191
0.7522 0.9241 0.9218 0.8619 0.7502
out{3} =
0.8363 0.4503 0.3573 0.9094 0.7359
0.2942 1.4934 0.8932 0.1845 1.1370
0.2308 0.8167 0.4690 0.1773 0.8850
0.6149 0.5218 0.0597 0.5880 0.7004
0.8761 0.4135 0.4205 0.5884 0.8814
The first element of the output cell array has the output when you pass M to sin, the second when you pass M to cos, and the third when you pass M to tan.
So the next question you're asking... why is this useful?
Point #1 - Nix the copying and pasting
This kind of code writing is very useful because if you want to use the same inputs and supply them to many different functions, we would naturally be inclined to do some copying and pasting. Take each of your function names, and create a single line for each. Each line would call the corresponding function you want, followed by the input arguments. This can become quite tedious, and so one smart way to do it would be to place your function name as a handle into a cell array, and to write one for loop that goes over all of the functions dynamically. You could even explore cellfun and escape using the for loop to iterate over all of the function handles too, but I'll leave that for you to read up on.
In this way, you have very maintainable code and if you want to remove functions that don't need to be run, just remove the handles from the cell array rather than scrolling down to where the line that invokes this function is located and removing that.
This is actually a very common technique in computer science / software engineering in general. In fact, this is actually quite close to what are known as function pointers. This is MATLAB's cheap way of doing it, but the logic behind this is essentially the same.
Point #2 - Higher Order Functions
Another way this is useful is if you have a function where one (or more than one!) of the inputs is a function, and you also specify inputs into this function as additional parameters to this function. This is what is known as a higher order function. The outputs would be based on using this input function, and the additional inputs you specify to it and the outputs are based on using this input function and the inputs you specify for this function.
One very good example is the fzero function in MATLAB. The goal is to find the root of a non-linear function, and the first parameter is a handle to a function that you specify. The base behaviour behind how fzero works is the same no matter what the function is. All you have to do is specify the function you want to solve and the initial guess of where you think this root is.
All in all, anonymous functions are very useful.

error in inserting cell type numbers for fuzzy input

I want to make a system content Fuzzy, so first I make the graphical system shape in GUI. The main sector of my system is a table that some columns of this table must be filled by user, other rows must be filled after Fuzzy processes. Then, I make the Fuzzy system separately, and when I want to insert cell type numbers (instead of Fuzzy input variables) to my Fuzzy system in MATLAB using command window, this error appeared:
"??? Undefined function or method 'min' for input arguments of type 'cell'."
Please help me to fix the problem.
Your problem is probably unrelated to your "Fuzzy" application.
From the error, you are either explicitly trying to min()over a cell array entries or passing cellinput arguments to a function that expects double.
% example cell array of doubles (one per entry)
N = 4; cellArray = mat2cell(randi(10, N, N), ones(N,1), ones(N,1));
% min of all
minCellArray = min([cellArray{:}]);
% min of two entries
minSubArray = min(cellArray{1}, cellArray{2});
Now compare the above with trying to do minSubArray = min(cellArray(1), cellArray(2)), which will generate the same error as the one you get.
Overall, beware of the cellArray{i} vs cellArray(i) assignment or passing (as input) to functions.

How to change returned functions parameters automatically in MATLAB?

I am doing a MATLAB assignment and it includes the evaluation of a returned lambertw() function. However, since I am doing Monte Carlo here, I need to use a branch of the function which is not the default branch. For example, change the parameter from the returned default zero
lambertw(0, -7661844165657387/9007199254740992*exp(-1))
to minus one
lambertw(-1, -7661844165657387/9007199254740992*exp(-1)).
Is there a way to do this automatically?
p.s. this is a follow up to my previous question
If you have the result shown above stored in a symbolic variable s, one option you have is to use CHAR to convert the symbolic variable to a character string, use STRREP to replace the first argument to LAMBERTW, then use SYM to convert the character string back to a symbolic equation:
s = sym(strrep(char(s),'lambertw(0','lambertw(-1'));

neural network,matlab programming

what can i do to solve this error in my program(learning the net)? it appear after sim. the net.
Error in ==> network.sim>simargs at 236
switch class(P)
??? Output argument "Pi" (and maybe others) not assigned during call to "C:\Program
Files\MATLAB\R2008b\toolbox\nnet\nnet\#network\sim.m>simargs".
Error in ==> network.sim at 173
case 2, [err,X,Xi,Ai,T,Q,TS,matrixForm] = simargs(net,X);
Error in ==> mlptrptest at 62
y = sim(net,A{1,1})
Note: Don't fall into the same trap I did. There is also a function called SIM in Simulink that will likely show up in searches for the function SIM in the Neural Network Toolbox...
The first thing I would check is that the second input argument A{1,1} is of the correct form. Specifically, A{1,1} would have to be a cell array or a matrix of doubles. If it is of any other form, like a structure or a matrix of any other class, you will get the error you are seeing. Admittedly, this particular error isn't handled very well by the subfunction simargs, in that it gives you some cryptic messages that don't really tell you the basic problem, which is that your input is not in the right format.
Here are a couple things to check:
Do you really mean to pass the first element of cell array A as an input argument, or do you mean to simply pass the cell array itself? If it's the second option, just do this:
y = sim(net,A);
If you do mean to pass the first element of A as an input argument, double-check it's class using the function CLASS:
class(A{1,1})
If you don't see double displayed, it means the first element of A is not the right type. If it is a matrix, you can convert it to double before you pass it to SIM like so:
y = sim(net,double(A{1,1}));