Simulink adding blocks - matlab

Trying to use simulink but have some problems:
part.type = 'PB1';
part.name = ['SLibrary/PB_1_' int2str(rjoint1Count) '_Default/PB1_1' ];
part.handle = add_block(pjnt11, [sys '/' part.name], 'Position', pos, 'MakeNameUnique', 'on');
where
pjnt11 = 'SLibrary/PB_1_Default/PB1_1 ';
sys=Robot
which are defined before. Also the predefined SLibrary/PB_1_Default/PB1_1is
and
When I run the code I got the following error:
Error using startSimulation (line 134)
A new block named 'Robot/SLibrary/PB_1_1_Default/PB1_1' cannot be added
Tried to find it online but couldn't find any solution.
Thanks in advance.

You are trying to add a block, giving it the same name as an existing block. Block's must have unique names.

Related

Why histeq in Matlab is not working

I want equalize an image histogram. I tried the following code:
I = imread('1.png');
greyI = rgb2gray( I(:,:,1:3) );
J = histeq( greyI );
But the following warning occurred:
??? Attempt to execute SCRIPT histeq as a function:
D:\MatLab Prog\histeq.m
Error in ==> histeq at 3
J = histeq( greyI );
I also tried to get help from about histeq function, but that returns
No help found for histeq.m.
Help from you would be appreciated.
You have created your own file called histeq, see the error message. Delete or rename it to resolve the program.

Save values that are calculated in a function for each iteration of fminsearch

I want to find the Minimum of a function using
[x,fval] = fminsearch(#(param) esm6(param,identi),result(k,1:end-1),options)
now for each Iteration step i want some values that the function 'esm6' calculates to be saved in an Array. I tried the following:
In the first line of the function i wrote
identi.sim.i_optiIter = identi.sim.i_optiIter + 1;
to have an iteration-variable counting the iteration steps of fminsearch. And later to catch the values that I need I used
identi.sim.guete_werte.gew(identi.sim.i_optiIter,:,:) = y_sim;
identi.sim.guete_werte.ungew(identi.sim.i_optiIter,:,:) = y_sim_ungew;
and to make sure that I use the new values of the identi-struct for the next function call, I wrote this at the end of the function:
assignin('base','identi',identi);
Now unfortunatly it doesn't do what I wanted it to do. Can anyone help me with this?
EDIT:
I made another attempt on it, using an Output function. I extendend my Options like this:
options = optimset('Display','iter','MaxIter',3,'OutputFcn',#outfun);
But now the Problem is that i cannot figure out where to put this outfun. The outfun Looks like this:
function stop = outfun(x,optimvalues,state,iteration,y_sim,y_sim_ungew)
stop = false;
if state == 'iter'
guete_werte.gew(iteration,:,:) = y_sim;
guete_werte.ungew(iteration,:,:) = y_sim_ungew;
end
end
Now the Problem with it is, that i can not put it in the file, where i call the fminsearch, because that is a script. If i put the outputfunction into a separate .m-function file, it is not able to Access the variables of the esm6 function. And if I add it to the esm6-function file, matlab can't find the function and says
??? Error using ==> feval Undefined function or method 'outfun' for
input arguments of type 'struct'.

Non-modal questdlg.m prompt

My code makes a plot and then makes a prompt asking the user if he wants to make another plot with different parameters. The problem is, while questdlg.m is open, the user cannot look at details on the plot.
Here's the code :
while strcmp(Cont,'Yes') == 1
%Some code modifying 'data'
plot(1:X,data);
Cont = questdlg('Would you like to plot another pixel?','','Yes','No','Yes');
close all;
end
I've tried a few things. I tried to create another function called normalquestdlg.m, and I copy pasted the questdlg.m code into it, modifying line 401.
set(QuestFig,'WindowStyle','modal','Visible','on');
to
set(QuestFig,'Visible','on');
I tried different location for the normalquestdlg.m function. Putting it in my default Matlab folder for homemade functions gave me the following error :
Undefined function 'dialogCellstrHelper' for input arguments of type 'char'.
Error in **normalquestdlg** (line 74)
Question = dialogCellstrHelper(Question);
Error in **Plot** (line 40)
Cont = normalquestdlg('Would you like to plot another pixel?','','Yes','No','Yes');
And putting it in the same folder as questdlg.m (C:\Program Files\MATLAB\R2014a\toolbox\matlab\uitools) gave me the following error :
Undefined function 'normalquestdlg' for input arguments of type 'char'.
Error in **Plot** (line 40)
Cont = normalquestdlg('Would you like to plot another pixel?','','Yes','No','Yes');
I even tried to put it as the first path to look for :
p = path
path('C:\Program Files\MATLAB\R2014a\toolbox\matlab\uitools', p)
Cont = normalquestdlg('Would you like to plot another pixel?','','Yes','No','Yes');
path(p)
Needless to say, this didn't change a thing.
Anyone has any tips for me?
No easy solutions were to be found. The easiest thing I could find was to download MFquestdlg.m (http://www.mathworks.com/matlabcentral/fileexchange/31044-specifying-questdlg-position/content/MFquestdlg.m) and modify line 384 of it this way :
set(QuestFig, 'WindowStyle', 'modal', 'Visible', 'on');
to
set(QuestFig, 'Visible', 'on');
since 'normal' is the default WindowStyle.
I prefered to do this than to modify basic Matlab functions. This could be enhanced even more (if someone had the need to do so) by adding an input in the function specifying the WindowStyle (either 'normal', 'modal' or 'docked'), and it would be quite easy, adding too a little fail-proof like line in this style :
if nargin < 8
WinStyle = '%enter default mode'
end
if nargin == 8
if strcmp(WinStyle,'normal') == 1 | strcmp(WinStyle, 'modal') == 1 | strcmp(WinStyle, 'docked') == 1
else
error('MATLAB:questdlg:IncorrectInput', 'The WindowStyle input parameter was incorrectly written')
end
end

MATLAB Error using importdata

I get an error at the importdata line saying
"Error using importdata (line 137)
Unable to open file."
when I write in 'specimenAl.dat' manually into the function, the program runs fine. How can I make use of the importarray function while defining the argument as an element of an array?
material = {('specimenAl.dat'), ('specimenSt.dat')};
A = importdata(material(1));
Data = A.data;
Force = Data (:,2);
Displacement = Data (:,1);
Strain = Data (:,3);
I had the same problem. fixed it by using {} in
A = importdata(material{1});
Hope this helps!

'Undefined function or variable' in Matlab

Here is my code:
function [im,sindx,end1]=alln(im,i,j,secret,sindx,end1)
slen=length(secret);
p=im(i,j);
neigh= [im(i-1,j) im(i+1,j) im(i,j-1) im(i,j+1) im(i-1,j-1) im(i+1,j-1) im(i-1,j+1) im(i+1,j+1)];
minpix = min (neigh)
maxpix = max (neigh)
if minpix < p < maxpix
lowlim = minpix+1;
highlim = maxpix-1;
range = highlim-lowlim+1;
nbits=floor(log2(abs(range)));
if sindx+nbits-1>slen
end1=1;
return
end
for k=1:nbits
bin(k)=secret(sindx+k-1);
end
b = bin2dec(bin);
newvalue1 = abs (minpix + b);
newvalue2 = abs (maxpix - b);
if abs(p-newvalue1)<= abs(p-newvalue2)
im(i,j) = newvalue1;
else
im(i,j) = newvalue2;
end
sindx=sindx+nbits;
end
end
My main program calls this function. When I run the program, I get the following error message:
??? Undefined function or variable "bin".
Error in ==> alln at 34
b = bin2dec(bin);
I know there are many experts for whom this is not a problem at all. I am new to MATLAB. Please guys, show me the way, which type of modification in the code can overcome this problem?
First of all, are there some lines missing from the file? Perhaps you've stripped some comments from the top? Because the error message says that
b = bin2dec(bin);
is line 34, but it's line 22 in the code you present.
OK, that aside...
The error message says that 'bin' isn't defined, but I see that it's being set on the line...
bin(k)=secret(sindx+k-1);
That suggests to me that THAT line isn't being run.
I see that that bin = ... line is inside of a 'for' loop, so I suspect that the for loop is run zero times, meaning that 'bin' never gets defined. What is nbits? Is it 1, or perhaps less than 1? THAT would prevent the loop from running at all.
Try removing the semicolon from the end of the
nbits=floor(log2(abs(range)));
line and run your code again.
Leaving off the semicolon will force the value of nbits to be printed in the Command Window. I bet you'll find that it's 1 or less. If that's the case, then start looking at HOW nbits is calculated, and I bet you'll find the problem.
At what input arguments to the function alln, are you getting the error?
Lets suppose that nbits is 0, then the following loop will not run:
for k=1:nbits
bin(k)=secret(sindx+k-1);
end
So, bin will be undefined. So, the error happens. This is one of the cases where the error can happen. There are many such possible cases.