https://it.mathworks.com/matlabcentral/answers/questions.atomMATLAB Answers — New Questions2017-05-26T16:46:28Ztag:it.mathworks.com,2005:Question/3420292017-05-26T07:09:28Z2017-05-26T16:46:28ZI want to store the output from my nested for loop in a matrix . How can it be done?<pre class="language-matlab">Bw=[150 155];
Rbwtobtf=[0.2,0.21];
Hf=[70 75];
Rdtoh=[0.85 0.86];
Tbf=[115 120];
Bbf= [250 260];
Mu=3400000000;
Fcu=50;
Rdtoh=[0.85 0.86];
Tbf=[115 120];
Bbf=[250 260];
for i=1:length(Bw)
for j=1:length(Rbwtobtf)
for k=1:length(Hf)
for l= 1:length(Rdtoh)
for m= 1:length(Tbf)
for n= 1: length(Bbf)
</pre><pre> Btf=Bw(i)/Rbwtobtf(j);
syms x;</pre><pre> d=solve(Mu==Fcu*Btf*x^2*(0.157*Rbwtobtf(j)+(1-Rbwtobtf(j))*(Hf(k)/x)*(0.45-0.225*(Hf(k)/x))),x);
D=vpa(d);
De=D(1);
h= De/Rdtoh(l);
A1 = Btf*Hf(k);% Area of top flange
Hw = h-Hf(k)-Tbf(m);
A2 = Bw(i)*Hw; %Area of web
A3 = Bbf(n)*Tbf(m); %Area of bottom flange
A = A1+A2+A3; %Total area
disp (A);
n=n+1;
end
m=m+1;
end
l=l+1;
end
k=k+1;
end
j=j+1;
end
i=i+1;
end</pre><p>I want to store values of A in a matrix. please help.</p>vinay mhatrehttps://it.mathworks.com/matlabcentral/profile/authors/10261144-vinay-mhatretag:it.mathworks.com,2005:Question/3420932017-05-26T16:45:18Z2017-05-26T16:45:18Zx2 and y2 not plotting as expected <p>Code is following</p><pre class="language-matlab">clear all; close all; clc;
% Arbitrary x's and y's
x1 = [10 20 30 40];
y1 = [1 2 3 4];
x2 = [100 200 300 400];
y2 = [105 95 85 75];
figure
plot(x1,y1,'o', 'MarkerEdgeColor', 'r', 'MarkerFaceColor', 'r')
ax2 = axes('Color','none'); % Create secondary axis
plot(ax2, x2,y2,'o', 'MarkerEdgeColor', 'b', 'MarkerFaceColor', 'b')
% Now set the secondary axis attributes
ax2.Color = 'none'; % Make the chart area transparent
ax2.XAxisLocation = 'top'; % Move the secondary x axis to the top
ax2.YAxisLocation = 'right'; % Move
</pre><p>i get the output</p><img src = "/matlabcentral/answers/uploaded_files/78873/Capture.PNG"><p>but i am looking for this output</p><img src = "/matlabcentral/answers/uploaded_files/78874/Capture1.PNG"><p>x2 and y2 are not plotting properly.where am i going wrong?</p>Kathyhttps://it.mathworks.com/matlabcentral/profile/authors/10376926-kathytag:it.mathworks.com,2005:Question/3420922017-05-26T16:41:07Z2017-05-26T16:42:03ZThe for loop does not give the result I want.<pre class="language-matlab">DNdog =
</pre><pre> 7
7
7
11
11
11</pre><pre class="language-matlab">blnmynokta =
</pre><pre> 11
11
11</pre><pre class="language-matlab">ksnkoor =
</pre><pre> 4
4
4
7
7
7</pre><pre class="language-matlab">for i = 1:length(DNdog)
for j = 1:(length(blnmynokta))
if (DNdog(i) == ksnkoor(j))
A(i,j) = [0]
elseif (DNdog(i) == blnmynokta(j))
A(j,j) = [-1]
end
end
end
</pre><pre class="language-matlab">Formed matrix:
A =
</pre><pre> -1 0 0
0 -1 0
0 0 -1</pre><pre> The matrix I want to form:
A=
0 0 0
0 0 0
0 0 0
-1 0 0
0 -1 0
0 0 -1</pre><p>I do not understand where I made mistakes. Can you help me?</p>Muhendisleksihttps://it.mathworks.com/matlabcentral/profile/authors/8930166-muhendisleksitag:it.mathworks.com,2005:Question/3420912017-05-26T16:29:46Z2017-05-26T16:37:19ZHow can I choose a set of columns from my table. I have to get the means of let's say columns 1 and 5, columns 2 and 6 etc from 20 columns. Thanks<p>Does this look like a correct line of code?
x = mean(w(:,[1:4:10 11:4:20]));</p>Abdullah Al Jajahttps://it.mathworks.com/matlabcentral/profile/authors/8861805-abdullah-al-jajatag:it.mathworks.com,2005:Question/3420902017-05-26T16:23:51Z2017-05-26T16:23:51ZWeighted discrete total variation<p>Hello,</p><p>I need to reconstruct a set of liver images [each acquired at a different phase of respiration] using a compressed sensing framework. I need to use the smoothness across these images as an L1-norm constraint. The code that I have uses a total generalized variation operator and is implemented as follows:</p><pre> if a.adjoint
y = x(:,:,[1 1:end-1])-x;
y(:,:,1) = -x(:,:,1);
y(:,:,end) = x(:,:,end-1);
else
y = x(:,:,[2:end end])-x;
end</pre><p>Here, x is a 3D matrix with the third dimension being the respiratory state.</p><p>I am not sure why it is implemented this way, but it works. What I would like to do now is putting different weights that correspond to the distances between these different phases on the respiratory curve. So, if two phases correspond to real close points on the respiratory level curve, I would like to put a high weight. If they are far apart, I would like to put a low weight so as not to oversmooth the images.</p><p>Putting the weights for the non-adjoint one [what is inside the else clause above] is perhaps trivial, but I looked at a number of resources and none of them answered how I should implement the adjoint calculation. So, I would appreciate if you could make suggestions regarding where and how I should put the weights for the adjoint. Do I need to use 1/w?</p><p>- Please do not suggest using another metric for the L1 norm. I do need to stick with total generalized variation at this point.</p><p>Thank you very much!</p>Sadikhttps://it.mathworks.com/matlabcentral/profile/authors/1666517-sadiktag:it.mathworks.com,2005:Question/3419712017-05-25T20:21:55Z2017-05-26T16:18:41ZHow can I import .csv files and then save each as a new matrix using for loop?<p>I want to assign variable names to matrices created after importing .csv files from the current folder.</p><p>My code:</p><pre class="language-matlab">csv_files = dir('*.csv');
num_csv = length(csv_files);
</pre><pre class="language-matlab">for k = 1:num_csv
T{k} = importdata(csv_files(k).name);
T1 = T{k};
end
</pre><p>T is a {1x5} cell array each containing the data matrices that I want separated and named. The output for T1 is only the last file that was run through. I can manually assign the variables to get the desired result like so:</p><pre class="language-matlab">T1 = T{1};
T2 = T{2};
T3 = T{3};
T4 = T{4};
T5 = T{5};
</pre><p>However, I will have more data files for my future projects and would like this to be done in the same for loop. Any help would be greatly appreciated!</p><p>Thank you</p>Jessica Shullhttps://it.mathworks.com/matlabcentral/profile/authors/10372089-jessica-shulltag:it.mathworks.com,2005:Question/3417032017-05-24T10:29:50Z2017-05-26T16:16:06Zconvert fit data to matrix<p>So I created a table of x, y and z values
and input them into fit using</p><pre class="language-matlab">f = fit([T.x, T.y],T.z,'cubicinterp')
</pre><p>they create the plot I would like but f is not a matrix for further processing.</p><img src = "/matlabcentral/answers/uploaded_files/78605/untitled.jpg"><p>Is there any way of getting z values for the fit object?
I would like the output to be a 512x512 matrix</p><p>Sorry pretty new to matlab</p>David Pitcherhttps://it.mathworks.com/matlabcentral/profile/authors/9961582-david-pitchertag:it.mathworks.com,2005:Question/3420272017-05-26T06:51:16Z2017-05-26T16:14:14ZWrite a function called integerize that takes as its input a matrix A of integers of type double, and returns the name of the “smallest” signed integer class to which A can be converted without loss of information. If no such class exists, the text '<pre class="language-matlab">function integerize(A)
int8(A<2^7-1);
int16(A<2^15-1);
int32(A< 2^31-1);
int64(A<2^63-1);
end
</pre>Wasi von Deutschlandhttps://it.mathworks.com/matlabcentral/profile/authors/10267023-wasi-von-deutschlandtag:it.mathworks.com,2005:Question/3420722017-05-26T14:05:27Z2017-05-26T16:10:06ZHow to create an RGB image?<p>What I have done thus far to create RGB images is to just load a white image and seperate the red green and blue components from this image and combine them in different ways to get different colors. No this feels kind of redundant because I keep thinking that MatLab must have a built-in RGB system in which you can just write a one line code from which the output will be any of the colors within RGB. Is there a simple function for this?</p>Zeff020https://it.mathworks.com/matlabcentral/profile/authors/10353572-zeff020tag:it.mathworks.com,2005:Question/3420872017-05-26T15:52:28Z2017-05-26T16:14:35ZHow can I change the any2csv output to have one header row containing the structure format?<p>Hey,</p><p>I am currently using any2csv (<http://www.mathworks.com/matlabcentral/fileexchange/19149-any2csv>) to output my mat file as a csv.</p><p>I am getting an output with the headers in tree format as seen in the attached image.</p><pre class="language-matlab">>> Structure1
>>Field,>> Field2,>> Field3,>> Field4,>> Field5, >> NestedStruct1
Data,Data,Data,Data,Data >>Field1,>>Field2,>>Field3,>> Field4,>> Field5
Data,Data,Data,Data,Data
</pre><p>I would like to output this data as one row of headers with the data pertaining to that header below it while containing the information about the structure format in the header. This can be seen in the other attached image.</p><pre class="language-matlab">Structure1_Field1,Structure1_Field2,Structure1_Field3,Structure1_Field4,Structure1_Field5,Structure1_NestedStruct1_Field1,Structure1_NestedStruct1_Field2,Structure1_NestedStruct1_Field3,Structure1_NestedStruct1_Field4,Structure1_NestedStruct1_Field5
Data,Data,Data,Data,Data,Data,Data,Data,Data,Data
</pre><p>I have changed the layout to this from the original version (lines 122-127)</p><pre> %left right top bottom
gap_c=struct('l',0,'r',0,'t',0,'b',1); % gaps around cell elements
gap_s=struct('l',0,'r',0,'t',0,'b',0); % gaps around struct-fields
gap_n=struct('l',0,'r',0,'t',0,'b',0); % gaps around arrays
gap_t=struct('l',0,'r',0,'t',0,'b',0); % gaps around text</pre><pre class="language-matlab">gap_content=struct('l',0,'t',1); % gap between titles and content. Both must be >=0, sum should be >0
</pre><p>As I rarely need to work with recursive functions I am having difficulty making this last change. Would you know how to change it or what I should change in the code?</p><p>Thanks in advance!</p>Carterhttps://it.mathworks.com/matlabcentral/profile/authors/10370604-cartertag:it.mathworks.com,2005:Question/3420662017-05-26T13:21:31Z2017-05-26T16:07:01ZHow to get directory from user and display in in MultiTab GUI?<p>Hi, I am using MultiTab GUI to write a GUI which has many text editors and pusbottons on it. The problem I face is that I need to get the directory from user and save it to avariable and also show it in a textbox editor. It seems pretty easy when I write the code for a single GUI but when I recall the function in the MultiTab GUI it gives me an error of:</p><pre> Error in tab_test>LoadData_Callback (line 327)
str = get(handles.LoadFilePath,'string');
Error while evaluating UIControl Callback</pre><p>my code inside the pushbotton recall function is this:</p><pre class="language-matlab">basedir = uigetdir('C:\');
handles.basedir = basedir;
guidata(hObject,handles);
str = get(handles.LoadFilePath,'string');
set(handles.LoadFilePath,'string',[str basedir]);
</pre><p>any help would be appreciated.</p><p>[EDITED, Jan, Code formatted, addition information moved from section for answers:]</p><p>This is the complete Error:</p><p>Reference to non-existent field 'LoadFilePath'.</p><p>Error in tab_test>LoadData_Callback (line 326)</p><p>str = get(handles.LoadFilePath,'string')</p><p>Error while evaluating UIControl Callback</p><p>and this is how I define these 2 controls:</p><pre> uicontrol('Parent', TabHandles{2,1}, ...
'Tag', 'LoadData', ...
'Units', 'pixels', ...
'Position', [round(PanelWidth/2)-100 4*ButtonHeight 200 ButtonHeight], ...
'String', 'Load Data armin', ...
'Callback', @LoadData_Callback , ...
'Style', 'pushbutton',...
'HorizontalAlignment', 'center',...
'FontName', 'arial',...
'FontWeight', 'bold',...
'FontSize', 12);</pre><pre> uicontrol('Parent', TabHandles{2,1}, ...
'Tag', 'LoadFilePath', ...
'Units', 'pixels', ...
'Position', [round(PanelWidth/2)-100 2*ButtonHeight 200 ButtonHeight], ...
'String', '', ...
'Style', 'edit',...
'HorizontalAlignment', 'center',...
'FontName', 'arial',...
'FontWeight', 'bold',...
'FontSize', 12);</pre><p>and this is the function to recall it:</p><pre> function LoadData_Callback(hObject, eventdata, handles)
% --- Executes on button press in LoadData.</pre><pre> % set(handles.LoadFilePath,'string','');
%
% drawnow;
basedir = uigetdir('C:\');
handles.basedir = basedir;
guidata(hObject,handles);
str = get(handles.LoadFilePath,'string')
set(handles.LoadFilePath,'string',[basedir]);
drawnow;
end</pre>Armin Zadihttps://it.mathworks.com/matlabcentral/profile/authors/10366781-armin-zaditag:it.mathworks.com,2005:Question/3416362017-05-24T00:58:31Z2017-05-26T16:04:55ZHow to compare pixel co-ordination using a for loop ?<p>Here is my MATLAB code.</p><pre class="language-matlab">for j = 1 : numberOfRegions
thisBoundary = boundaries{j};
x = thisBoundary(:, 1);
y = thisBoundary(:, 2);
</pre><pre> for j2 = 1 : numberOfRegions2
thisBoundary2 = boundaries2{j2};
x2 = thisBoundary2(:, 1);
y2 = thisBoundary2(:, 2); </pre><pre> if(x2==x)
if (y2 == y)
U = x2;
V = y2;
end
end
end</pre><pre> end</pre><p>In this code I tried to compare boundary pixel co-ordinations(x,y) of two masks which I already obtained. But when it comes to the if condition it gives following error. I want to obtain the matched pixel co-ordinations and store them separately. how can I fixed that?</p><p>_ <b>Matrix dimensions must agree.
Error in (line 130)
if(x2==x)</b>_</p>Piyum Ranganahttps://it.mathworks.com/matlabcentral/profile/authors/9634533-piyum-ranganatag:it.mathworks.com,2005:Question/3420782017-05-26T14:40:45Z2017-05-26T16:02:49Zhow can i convert decimal to hex or another basis without use dec2hex ?<p>I need to make a function about convert basis to another (decimal to oktal or another) but i dont know the prinsip , i hope some one give me tips for that</p>Mohammad Imam Malikhttps://it.mathworks.com/matlabcentral/profile/authors/9865796-mohammad-imam-maliktag:it.mathworks.com,2005:Question/3420892017-05-26T16:00:42Z2017-05-26T16:00:42ZHow to display a figure saved as an object<p>Hi,
I have a feeling this may be a very basic question, but I can't find the answer in the MatLab gcf, figure, or object handling help files...</p><p>I've saved a large number of figures as objects, by this method:</p><pre class="language-matlab">Fig1 = gcf;
</pre><p>I need to view the figures again, but can't work out how to view it from the object! Can anyone help?</p>CarrotCakeIsYumhttps://it.mathworks.com/matlabcentral/profile/authors/5399662-carrotcakeisyumtag:it.mathworks.com,2005:Question/3420742017-05-26T14:14:42Z2017-05-26T15:57:03ZMatlab comparison of two large matricies<p>I am trying to retrieve the index of exact matches (row-specific) between two large matricies. I have a n x 61 matrix A containing values from 0 to 9 and another n x 61 matrix B , whereas each row here contains values from 0 to 9 but mostly NaN (only 2 to 8 columns in each row of matrix B contain actual numbers). Matrix A can be expected to have between 1.5 million and 3 million rows, whereas matrix B has around 0.2 to 0.5 million rows. Here is an example of the setup:</p><pre class="language-matlab">% create matrix a with random data
</pre><pre class="language-matlab">dataSample = [0 9];
numRows = 1000000;
numCols = 61;
A = randi(dataSample,numRows,numCols);
</pre><pre class="language-matlab">% create matrix B with random data
numRows = 100000;
numCols = 61;
numColsUse = 2:8;
dataRange = 0:9;
B = NaN(numRows,numCols);
for i = 1:size(B,1)
</pre><pre> % randomly selet number of columns to fill
numColsFill = datasample(numColsUse,1);</pre><pre> % randomly select column index from available columns
colIdx = datasample([1:numCols],numColsFill);</pre><pre> % randomly select values from 0 to 9
numFill = datasample([0:9],numColsFill);</pre><pre> % insert numbers at respective column in matrix B
B(i,colIdx) = numFill;</pre><pre class="language-matlab">end
</pre><p>I want to compare every single row of matrix A with the entire matrix B and find exact matches, where the numbers of matrix B match the numbers of matrix A at their respective positions (columns) - hence the NaN in matrix B are to be ignored.</p><p>I can achieve the desired result using cellfun, where I slice matrix A in several subsets and then use a custom function to compare the rows of the subset with each row in matrix B, like so:</p><pre class="language-matlab">% put all rows of matrix B in single cell
cellB = {B};
</pre><pre class="language-matlab">% take subset of matrix A and convert to cell array
subA = A(1000:5000,:);
subA = num2cell(subA);
</pre><pre class="language-matlab">% prepare cellB to meet cellfun conditions
cellB = repmat(cellB, [size(subA,1) 1]);
</pre><pre class="language-matlab">% apply cellfun to retrieve index of each exact match
idxContainer = cellfun(@findMatch, cellB, subA, 'UniformOutput', false);
</pre><p>Function findMatch looks as follows:</p><pre class="language-matlab">function [ idx ] = LTableEval( cellB, subA )
</pre><pre> idxCheckLT = lt(cellB, repmat(subA, [size(cellB,1) 1]));
idxCheckGT = gt(cellB, repmat(subA, [size(cellB,1) 1]));
idxCheck = idxCheckLT + idxCheckGT;
idxSum = sum(idxCheck,2);
idx = find(idxSum == 0);</pre><pre class="language-matlab">end
</pre><p>This approach works, but it appears to be very inefficient, especially RAM-wise, as the cellfun requires all inputs to have the same size and hence a multiplication of the same data sets. Any ideas on how to tackle this problem in a more efficient way? Many thanks!</p>Benvaulterhttps://it.mathworks.com/matlabcentral/profile/authors/3960639-benvaultertag:it.mathworks.com,2005:Question/3417782017-05-24T17:13:29Z2017-05-26T15:56:29ZHow MATLAB divide the number of iterations of "parfor" on the workers of one computer?<p>Dear all,</p><p>Please, I have a question, how MATLAB divide the number of iterations of "parfor" on the workers of the computer?</p><p>In the following example of Mathworks in the picture, as I understood, they mentioned when the number of iteration is 10 and the number of workers is 4, the first three workers take the 2 iterations equally then the remaining four iterations divide into the four. That is mean the first three workers take 3 iterations and the last worker take just one iteration.</p><p>Please, could anyone correct me if I am wrong ! And please, explain to me how MATLAB divide the number of iterations, i.e. even or odd ?</p><p>Lease, if I have this case how MATLAB divide the iterations ?
1. If the number of iteration is 40 and we have 4 workers.
2. If the number of iteration is 40 and we have 5 workers.
3. If the number of iteration is 40 and we have 8 workers.
4. If the number of iteration is 40 and we have 12 workers.</p><p>Kind regards
Ammar</p><img src = "/matlabcentral/answers/uploaded_files/78660/ParFor.bmp">Ammar https://it.mathworks.com/matlabcentral/profile/authors/6713588-ammartag:it.mathworks.com,2005:Question/3420882017-05-26T15:55:25Z2017-05-26T15:57:07ZImplementing Neumann boundary condition for elasticity problem using the finite element method<p>I am solving the fallowing mixed boundary elasticity problem with the finite element method on the unit circle, using a triangle mesh with 3 nodes on each element:</p><img src = "/matlabcentral/answers/uploaded_files/78863/100.png"><p>I have already implemented it correctly for the pure Dirichlet problem. (so the global stiffness matrix is also good for this problem), but I do something wrong when implementing the Neumann boundary condition and I cannot figure what. (I still get the right solution for u1 where I have the Dirichlet condition). On the right side of the picture is the fem solution and on the left side is the analytical one:</p><img src = "/matlabcentral/answers/uploaded_files/78869/101.png"><p>and here is the code for the Neumann implemantation:</p><pre> %mesh made with initmesh and refinemesh</pre><pre> F=zeros(2*size(p,2),1);
K=stiffnessMatrixI2(p,t,mu,lambda);
fixed=unique([e(1,:) e(2,:)]);</pre><pre> %Neumann boundary condition
for el=1:size(t,2)
x1=p(1,t(1,el)); x2=p(1,t(2,el)); x3=p(1,t(3,el));
y1=p(2,t(1,el)); y2=p(2,t(2,el)); y3=p(2,t(3,el)); </pre><pre> %%%%P1P2 - edge of an element (triangle) taken in anticlockwise order
if( sum(ismember([t(1,el) t(2,el)],fixed))==2)
J=[x2-x1 y2-y1; 0 0];
ak=sqrt(J(1,1)^2+J(1,2)^2);
n=[y2-y1 x1-x2]./sqrt((y2-y1)^2 + (x1-x2)^2); %the ouward unit normal
F(2*(t(1,el)-1)+2,1)=F(2*(t(1,el)-1)+2,1)+ak*sigma0*n(2)*0.25;
F(2*(t(2,el)-1)+2,1)=F(2*(t(2,el)-1)+2,1)+ak*sigma0*n(2)*0.25;
end</pre><pre> %%%%P3P1
if(sum(ismember([t(1,el) t(3,el)],fixed))==2)
J=[ 0 0; x3-x1 y3-y1];
ak=sqrt(J(2,1)^2+J(2,2)^2);
n=[y1-y3 x3-x1]./sqrt((y1-y3)^2 + (x3-x1)^2);
F(2*(t(1,el)-1)+2,1)=F(2*(t(1,el)-1)+2,1)+ak*sigma0*n(2)*(-1)*0.25;
F(2*(t(3,el)-1)+2,1)=F(2*(t(3,el)-1)+2,1)+ak*sigma0*n(2)*(-1)*0.25;
end</pre><pre> %%%%P2P3
if(sum(ismember([t(2,el) t(3,el)],fixed))==2)
J=[0 0; x3-x2 y3-y2];
ak=sqrt(J(2,1)^2+J(2,2)^2);
n=[y3-y2 x2-x3]./sqrt((y3-y2)^2 + (x2-x3)^2);
F(2*(t(2,el)-1)+2,1)=F(2*(t(2,el)-1)+2,1)+ak*sigma0*n(2)*0.25;
F(2*(t(3,el)-1)+2,1)=F(2*(t(3,el)-1)+2,1)+ak*sigma0*n(2)*0.25;
end</pre><pre> end</pre><pre> np = size(p,2)*2; % total no. of nodes
g=analyticalSolutionDisc(p(1,fixed),Nu,G,sigma0); %Dirichlet BC at boundary nodes
fixed=(fixed-1)*2+1;
free=setdiff(1:np,fixed);</pre><pre> F=F(free,1)-K(free,fixed)*g'; %modify F
K=K(free,free); </pre><pre> Ufem=zeros(np,1); %allocate space for solution
Ufem(fixed)=g;
Ufem(free)=singularMatrix( K,F );
U2Fem=Ufem(2:2:np);
U1Fem=Ufem(1:2:(np-1));</pre>b10nd3https://it.mathworks.com/matlabcentral/profile/authors/8267145-b10nd3tag:it.mathworks.com,2005:Question/3413522017-05-22T13:05:11Z2017-05-26T15:55:03ZConversion of Polar Data to Cartesian Data<p>Good Morning,</p><p>I have an interpolation problem that I thought others would have more insight on. I have cartesian data Z(x,y) where x and y are vectors that describe the locations of the values of Z in terms of Cartesian coordinates. My goal is to take this data and to project it upon a polar grid so that I can perform an azimuthal average of the data.</p><p>To get the data Z(x,y) onto a polar grid, I essentially use the griddata function:</p><p>load('Zdata.mat')</p><p>[X,Y]=meshgrid(x,y);</p><p>[TH0,R0]=cart2pol(X,Y);</p><p>vor_polar=griddata(X,Y,vor,TH0,R0);</p><p>subplot(2,2,[1 3]); contourf(X/1000,Y/1000,vor); axis([-100 100 -100 100]);</p><p>subplot(2,2,[2 4]); contourf(R0/1000,TH0,vor_polar); axis([0 100 -3 3]);</p><p>This procedure works fine if Z(x,y) has ideal circular symmetry, such as shown in Figure 1. However, when I begin to add asymmetries to Z(x,y) (i.e. perturbations which drives the data away from circular symmetry) as in Figure 2, the interpolation scheme doesn't seem to pick up these asymmetries. I wanted to know if there are any underlying problems with my procedure or if this is related to the griddata function.</p>Gabriel Williamshttps://it.mathworks.com/matlabcentral/profile/authors/4476599-gabriel-williamstag:it.mathworks.com,2005:Question/3420862017-05-26T15:49:37Z2017-05-26T15:51:59ZHow to use GUI for the input of a function? Polynom calculator<p>Dear all,</p><p>I worte a function file in Matlab:</p><p>Where G is a function and H as well, i is the argument which deicides which mathematical operation should be used.</p><p>No im am struggeling a bit.
I want to create a gui, which is basically looking like a stanard random calculator, where i can input my G & H Function in the form '2*x^3 -2*x +3'.
Then there will be a push buttons for + - * :, I mean the different operations in the gui where I Can chose my i in the function.
At the at it should display the new polynom.
Cheers, Felix</p><p>function POLYNOM( G,H,i)</p><p>syms x</p><p>if i == 1</p><p>Sum = expand(G+H)</p><p>elseif i == 2
Difference = expand(G-H)
elseif i == 3</p><p>Product = expand(G*H)</p><p>elseif i == 4</p><p>Quotient = expand(G/H)
end</p><p>end</p>Felix_2991https://it.mathworks.com/matlabcentral/profile/authors/10344692-felix_2991tag:it.mathworks.com,2005:Question/3420802017-05-26T15:03:40Z2017-05-26T15:48:42Zhow to clear variable<p>hay
I have GUI code that extract 5 diffrent values from diffrent GUI in matlab using getappdata and setappdata. it works fine at the first time but when i use the gui again its show the variable from the first use.
how can i "restart" the values every time i use GUI?
i used clear and clearvars but it dont work.</p><p>thank you all!</p>Evelina Isakovhttps://it.mathworks.com/matlabcentral/profile/authors/8381034-evelina-isakovtag:it.mathworks.com,2005:Question/3420852017-05-26T15:44:47Z2017-05-26T15:45:37ZNuclear Segmentation on noisy DAPI fluorescent images<p>I have .tif image stacks for dapi fluorescent microscopy images. I am trying to segment out only the nuclei of the cells, but the images are very noisy and the target signal from the DAPI is diffused into speckled nuclear regions and cytosol. I'm trying to set up an automated thresholding way of doing this with multiple images such as these to segment out only the nuclei but I am extremely new to MATLAB and programming, and everytime I've found discussions about cell segmentation the tools they use do not achieve the same outcome with my images.</p>Steven Lewishttps://it.mathworks.com/matlabcentral/profile/authors/10169299-steven-lewistag:it.mathworks.com,2005:Question/3419862017-05-25T23:05:16Z2017-05-26T15:45:13Zwhy does the anonymous calculate incorrectly<p>in my code below, i am calculating a result two different ways. When I embed more into the anonymous function, it calculates an incorrect answer. Note y_A does not equal y_B. Does anyone have an idea why?</p><pre class="language-matlab">g=1.4;
M1=2;
theta=-15*pi/180;
M=M1;
vM1= sqrt((g+1)/(g-1))*atan(sqrt(((g-1)/(g+1))*(M^2-1)))-atan(sqrt((M^2)-1))
</pre><pre class="language-matlab">fx = @(M) vM1 - theta - sqrt((g+1)/(g-1))*atan(sqrt(((g-1)/(g+1))*(M^2-1)))-tan(sqrt((M^2)-1));
</pre><pre class="language-matlab">fx2= @(M)(sqrt((g+1)/(g-1))*atan(sqrt(((g-1)/(g+1))*(M^2-1)))-atan(sqrt((M^2)-1)));
</pre><pre class="language-matlab">y2=fx2(2.595)%second anonymous function works works for this one
</pre><pre class="language-matlab">y_A=fx(2.595)%trying to figure out why anonymous function is not working
</pre><pre class="language-matlab">y_B=vM1-theta-y2%checks out close to zero
</pre>Lou Jacksonhttps://it.mathworks.com/matlabcentral/profile/authors/50491-lou-jacksontag:it.mathworks.com,2005:Question/3416242017-05-23T22:02:54Z2017-05-26T15:44:44ZShape a transferfunction to measurement data in Control System Designer<p>Hello everyone,
For a self study assignment I have to create the transfer function of measurementdata to later shape a controller around. The data of the measurement is already analysed and I'm able to make bode plots out of it. The problem is that I can't find a way of fitting a magnitude plot with a known transfer function to the one of the measurements. I first tried using the MATLAB function 'tfest' but this didn't give coherent results (looks more like: as long as a lot of points are on the line, it must be correct).</p><p>I then tried to use the 'Control System Designer' app in MATLAB. This works great but comparing the output to the measurement plot is something I have to do by hand as of now. Does anyone know if it is possible to get the magnitude plot from the measurements into this app or is there a way to quickly get a visual feedback on whether or not both plots align?</p><p>Thanks in advance!</p>Jorishttps://it.mathworks.com/matlabcentral/profile/authors/7856034-joristag:it.mathworks.com,2005:Question/3420842017-05-26T15:44:13Z2017-05-26T15:45:22ZImplementing 2D Otsu Thresholding<p>I'm trying to binarize some very large (8548x10434) 2P images of whole mouse brains. I've been using Otsu (graythresh) with some degree of success but my images do not give bimodal histograms (image attached).</p><img src = "/matlabcentral/answers/uploaded_files/78867/Screen%20Shot%202017-05-26%20at%2015.05.18.png"><p>I think 2D Otsu will be an improvement but I'm unsure if it's the best method. I'm trying to use a 2D Otsu method I've come across here:</p><pre> %hists is a 256x256 2D-histogram of grayscale value and neighborhood average grayscale value pair.
%total is the number of pairs in the given image.
%threshold is the threshold obtained.
function threshold = Otsu2D(hists, total)
maximum = 0.0;
threshold = 0;
w_0=0;mu_ti=0;mu_tj=0;mu_i=0;mu_j=0;mu_is=0;mu_js=0;
for i =1:256
for j=1:256
P(i,j)=hists(i,j)/total;
end
end
for i=1:256
for j=1:256
mu_ti=mu_ti+i*P(i,j);
mu_tj=mu_tj+j*P(i,j);
end
end
for ii=1:256
for jj=1:256
w_0=w_0+P(ii,jj);
mu_is=mu_is+ii*P(ii,jj);
mu_i(ii,jj)=mu_is;
mu_js=mu_js+jj*P(ii,jj);
mu_j(ii,jj)=mu_js;
tr = ((mu_i(ii,jj)-(w_0*mu_ti))^2 + (mu_j(ii,jj)-(w_0*mu_tj))^2)/(w_0*(1-w_0));</pre><pre> if ( tr >= maximum )
threshold = ii;
maximum = tr;
end
end
end</pre><p>Im using the GLCM to compute the 2D histogram as per an example posted here previously by Image Analyst. The relevant code is:</p><pre class="language-matlab">%Now get the GLCM with graycomatrix().
kernel = [0, D; -D, D; -D, 0; -D, -D];
glcm = graycomatrix(grayImage, 'NumLevels', integerValue,...
'GrayLimits', [],'offset',kernel);
numberOfDirections = size(glcm, 3);
for k = 1 : numberOfDirections
glcm(:,:,k) % Display in the command window.
end
% Combine all directions into one.
% Sum along the third dimension.
glcm = sum(glcm, 3);
% Display the 2D glcm as a matrix.
subplot(1, 2, 2);
rgbImage = ind2rgb(glcm, hot(integerValue));
imshow(rgbImage);
% imshow(glcm, []);
axis on;
title('Image of the
</pre><pre> GLCM', 'FontSize', fontSize);</pre><p>The problem is my threshold output from Otsu2D is always 0.</p>Arian Jamasbhttps://it.mathworks.com/matlabcentral/profile/authors/9175273-arian-jamasbtag:it.mathworks.com,2005:Question/3415982017-05-23T18:56:10Z2017-05-26T15:37:45ZError when attempting to use xlswrite function to print 2d array to existing Excel file<p>Hello all,</p><p>I am attempting to ease my workload by having my code print the array containing my results to an existing Excel file that is in the same filepath as my script. Here is my issue:</p><p>The line including xlswrite:</p><pre class="language-matlab">xlswrite(TestData1.xlsx, dataComp, 1, O2)
</pre><p>The error that is generated at an attempt to run:</p><pre class="language-matlab">Struct contents reference from a non-struct
array object.
</pre><p>The array that I am printing to the Excel file was created initially using the "zeros" function, and was then populated index-by-index. Because of this, I assumed that its format would be that of a numeric matrix. However, the above error makes that seem incorrect. Any advice?</p><p>Best,
Jack</p>Jack Iversonhttps://it.mathworks.com/matlabcentral/profile/authors/6129956-jack-iversontag:it.mathworks.com,2005:Question/3420452017-05-26T09:31:08Z2017-05-26T15:34:50Zusing C code for serial port communication in matlab via mex file<p>Hi,</p><p>I have implemented a C code for the serial port Communication. I want to use this function in matlab. I read the documentation about mex file (mex function) and i made the changes which i need for mexFunction.
When i write mex serial_port.c to the command line everything is fine and matlab generates serial_port.mexa64. But when i want to use it and write serial_port() to the command line the serial port is not opening. what could be the problem?</p><p>here is my code:</p><pre class="language-matlab">#include "mex.h"
#include <stdio.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
</pre><pre> void mexFunction (int nlhs, mxArray *plhs[], int nrhs,
const mxArray *prhs[])</pre><pre> {
if(nrhs != 0) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nrhs",
"zero inputs required.");
}</pre><pre> if(nlhs != 0) {
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:nlhs",
"Two output required.");
}</pre><pre> char serial;
struct termios SerialPortSettings;</pre><pre> serial= open("/dev/ttyACM1", O_RDWR | O_NOCTTY);
printf("%i \n",serial);</pre><pre> tcgetattr(serial, &SerialPortSettings);</pre><pre> cfsetispeed(&SerialPortSettings,B9600);
cfsetospeed(&SerialPortSettings,B9600);</pre><pre> SerialPortSettings.c_cflag &= ~PARENB;
SerialPortSettings.c_cflag &= ~CSTOPB;
SerialPortSettings.c_cflag &= ~CSIZE;
SerialPortSettings.c_cflag &= ~CS8;</pre><pre> SerialPortSettings.c_cflag &= ~CRTSCTS;
SerialPortSettings.c_cflag |= CREAD | CLOCAL ;
SerialPortSettings.c_cc[VMIN] = 9;
SerialPortSettings.c_cc[VTIME] = 0;
SerialPortSettings.c_iflag &= ~(IXON | IXOFF | IXANY);
SerialPortSettings.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG);
tcsetattr(serial,TCSANOW,&SerialPortSettings);</pre><pre> char writeb[9];
writeb[0]=1;
writeb[1]=2;
writeb[2]=0;
writeb[3]=0;
writeb[4]=0;
writeb[5]=0;
writeb[6]=0;
writeb[7]=200;
writeb[8]=writeb[0]+writeb[1]+writeb[2]+writeb[3]+writeb[4]+writeb[5]+writeb[6]+writeb[7];
int bytes_written=0;</pre><pre> bytes_written=write(serial,writeb,sizeof(writeb));</pre><pre> char readb[9];
int bytes_read;
int a;
bytes_read=read(serial,&readb,9);</pre><pre> a=readb[2];
printf("%i \n",a);
close(serial);
}</pre><p>best regards</p>Ahmet Tunahttps://it.mathworks.com/matlabcentral/profile/authors/6005301-ahmet-tunatag:it.mathworks.com,2005:Question/3416462017-05-24T03:13:20Z2017-05-26T15:33:41ZSimulink real time logging buffer problems<p>Hi,</p><p>I am using Simulink Real Time for doing a rather lengthy test where I want to log several GBs of data before loading it from target to host.</p><p>I found the <i>signal logging data buffer size in doubles</i> and tried to increase it to a value that concurs with 5 GB. The target has 8 GB of RAM available. Whenever I try to build and deploy the model on the target I get a fault saying that the model memory exceeds available memory (I will post the exact error when I get back to work). It seems I can select a maximum of around 450 MB before I get into this problem.</p><p>The target is booted from a usb disk with the RAM option set to 8 GB. I also tried auto, but with no improvement.</p><p>Has anyone had this problem? or am I missing something?</p><p>Best Regards,
Jesper</p>Jesper Linigerhttps://it.mathworks.com/matlabcentral/profile/authors/6281219-jesper-linigertag:it.mathworks.com,2005:Question/3420762017-05-26T14:27:21Z2017-05-26T15:33:01ZBlack Scholes Functions and Negative Rates<p>Hi,</p><p>Are they any plans to modify Matlab's Black Scholes functions in Matlab to cater for negative risk free rates?</p><p>Thanks.</p>Seosamh Mac Réamoinnhttps://it.mathworks.com/matlabcentral/profile/authors/10338167-seosamh-mac-reamoinntag:it.mathworks.com,2005:Question/1051842013-11-07T00:03:46Z2017-05-26T15:32:36ZHow can I import a .txt file with several headers?<p>I have a data file in the form of:</p><p>header1</p><p>header1</p><p>data</p><p>.</p><p>.</p><p>.</p><p>data</p><p>header2</p><p>header2</p><p>data</p><p>.</p><p>.</p><p>.</p><p>data</p><p>header3</p><p>.</p><p>.</p><p>I need to import the data in a way that each header is the beginning of a new set of data that will go into separate matrices.
so all the data underneath header 1 will go into matrix1 and data under header 2 goes into matrix 2 and so on.
Any idea how to do that?</p>Zahra Vazirihttps://it.mathworks.com/matlabcentral/profile/authors/4782185-zahra-vaziritag:it.mathworks.com,2005:Question/3420832017-05-26T15:32:06Z2017-05-26T15:32:06ZI have a faul when run my code.<p>Hello guys. This image show my faul. When i open file.m and run. But it cant run corectly. It apear a error. I think it correlate with video card. I dont know to fix it. Who can tell me what is corect error and what i have to do to fix this ? Thank guys very much.</p><img src = "/matlabcentral/answers/uploaded_files/78864/Untitled.png">Hieu Ngochttps://it.mathworks.com/matlabcentral/profile/authors/5728323-hieu-ngoctag:it.mathworks.com,2005:Question/3406872017-05-17T14:07:38Z2017-05-26T15:31:05ZHow to run CellEditCallback when setting Data for a uitable<p>I've looked at many sources, but cannot find a workaround to this problem... I know CellEditCallback gets called after editing a uitable's Data... but is there any way to have it run when Set(uitable,'Data',x)? It seems if one wants to continuously update the Data, there is no way to trigger any callbacks to perform validation or other things...</p>Jeff Waldronhttps://it.mathworks.com/matlabcentral/profile/authors/6813961-jeff-waldrontag:it.mathworks.com,2005:Question/3420622017-05-26T12:43:54Z2017-05-26T15:29:29ZWrite a function called year2016 that returns a row-vector of struct-s whose elements correspond to the days of a month in 2016 as specified by the input argument. If the input is not an integer between 1 and 12, the function returns the empty array.<p>This is my code. Could you please tell me</p><pre class="language-matlab">function m = year2016
for i = 1:31
[MonthNumber, DateName] = weekday(datenum([2016 1:12 i]));
m.(i) = struct('month','MonthNumber','date',i,'day', DateName);
end
</pre>Wasi von Deutschlandhttps://it.mathworks.com/matlabcentral/profile/authors/10267023-wasi-von-deutschlandtag:it.mathworks.com,2005:Question/3420822017-05-26T15:29:27Z2017-05-26T15:29:27ZHow i do region based segmentation using active contour?<p>In Matlab</p>komal kellahttps://it.mathworks.com/matlabcentral/profile/authors/9248964-komal-kellatag:it.mathworks.com,2005:Question/3420812017-05-26T15:28:24Z2017-05-26T15:28:24ZLegacy code, c++ class<p>Hi,</p><p>I have c++ class. I want to use this c++ class in matlab/simulink. How I can do this ? i read something about legacy code but i didnt understand. Please help me.
Thanks a lot.</p><p>Best regards</p>Ahmet Tunahttps://it.mathworks.com/matlabcentral/profile/authors/6005301-ahmet-tunatag:it.mathworks.com,2005:Question/3420552017-05-26T11:08:12Z2017-05-26T15:27:17ZI have seven different graphs in one script and I want to see all seven graphs in one screen what matlab command I can Use.<p>f1=m1(:,1);
r1=m1(:,2);
i1=m1(:,3);
mag1=sqrt(r1.^2+i1.^2);
plot(f1,mag1);
figure(1);</p><p>f2=m2(:,1);
r2=m2(:,2);
i2=m2(:,3);
abs2=sqrt(r2.^2+i2.^2);
figure(2);
plot(f2,abs2);</p><p>f3=m3(:,1);
r3=m3(:,2);
i3=m3(:,3);
abs3=sqrt(r3.^2+i3.^2);
figure(3);
plot(f3,abs3);</p><p>f4=m4(:,1);
r4=m4(:,2);
i4=m4(:,3);
abs4=sqrt(r4.^2+i4.^2);
figure(4);
plot(f4,abs4);</p><p>f5=m5(:,1);
r5=m5(:,2);
i5=m5(:,3);
abs5=sqrt(r5.^2+i5.^2);
figure(5);
plot(f5,abs5);</p><p>f6=m6(:,1);
r6=m6(:,2);
i6=m6(:,3);
abs6=sqrt(r6.^2+i6.^2);
figure(6);
plot(f6,abs6);</p><p>f7=m7(:,1);
r7=m7(:,2);
i7=m7(:,3);
abs7=sqrt(r7.^2+i7.^2);
figure(7);
plot(f7,abs7);</p>Prateek Srivastavahttps://it.mathworks.com/matlabcentral/profile/authors/10070950-prateek-srivastavatag:it.mathworks.com,2005:Question/3419182017-05-25T14:22:35Z2017-05-26T15:25:38ZAllowing for code to work different with file names<p>I have a code that works fine but i have thus far only used it with a single test file. As a result of this i could run it while specifying the filename as well as the folder inside the code itself:</p><pre class="language-matlab">f1d = fopen(fullfile(sbd,'TEST_A.asc'),'rt');
</pre><p>Now that i need to apply it to multiple different files the code will not work. I have tried including a colon to read all files (as there is only one) but this doesn't work as the matlab error says there is no file name given. Can I not just specify the folder from which to select the file and the code read whatever files are in the folder?</p>Aaron Smithhttps://it.mathworks.com/matlabcentral/profile/authors/9670029-aaron-smithtag:it.mathworks.com,2005:Question/3416862017-05-24T09:09:52Z2017-05-26T15:23:41ZScalability Simulink 3D animation<p>I'm currently visualizing a light show with about 300 LEDs using the Simulink 3D animation package and it seems I have to handle every LED separately.</p><p>In the VR Sink, I have to enable the emissiveColor of each LED seperately and connect each LED to a separate signal in Simulink. If I now want to change the diffusive color of all my LEDs, does that mean I have to change it by hand for each of the 300 LEDs I have?!</p><p>Any help is much appreciated.</p>Luuk Poorthttps://it.mathworks.com/matlabcentral/profile/authors/5564181-luuk-poorttag:it.mathworks.com,2005:Question/3420322017-05-26T07:42:16Z2017-05-26T15:15:39ZDistinguish european voyages from coordinates<p>Hello everyone,</p><p>I have a large dataset with coordinates and name of arrival and departure ports. How can Matlab identify if the ports are in Europe or not? Is there a way from coordinates to distinguish voyages into Europe and outside of it?</p><p>Thanks,
Vanessa</p>Vanessahttps://it.mathworks.com/matlabcentral/profile/authors/10221241-vanessatag:it.mathworks.com,2005:Question/3420732017-05-26T14:11:33Z2017-05-26T15:13:31ZIs there a color database existent?<p>Is there a simple color database existent containing a bunch of RGB colors which I could use in my script to create colored images?</p><p>e.g. I'd like to be able to do this</p><pre class="language-matlab">image(LG) %% -> selects LG from database which gives a light green image
</pre>Zeff020https://it.mathworks.com/matlabcentral/profile/authors/10353572-zeff020tag:it.mathworks.com,2005:Question/3403802017-05-15T16:26:34Z2017-05-26T15:08:04Zremove discs from lat/lon coverage<p>This is my code:</p><pre class="language-matlab">[xIntrinsic, yIntrinsic] = geographicToIntrinsic(r, discLat, discLon);
[xc, yc] = polysplit(xIntrinsic, yIntrinsic);
for ii = 1:(numel(xc)-1)
xii = [xc{ii}; xc{ii+1}];
yii = [yc{ii}; yc{ii+1}];
k = convhull(xii,yii);
xc{ii} = xii(k);
yc{ii} = yii(k);
end
[xq, yq] = polyjoin(xc(1:end-1), yc(1:end-1));
[latQ, lonQ] = intrinsicToGeographic(r, xq, yq);
</pre><p>When I plot, I see the discLat/discLon points (as circles) along with the bounding lines:</p><img src = "/matlabcentral/answers/uploaded_files/77902/coverage.JPG"><p>Is there an easy way to remove the discs so that all I see is the bounding outside lines? I'm using the areaint function on the latQ/lonQ points to get an area, and it is coming back way high, I believe because it includes those discs.</p><p>Thanks.</p>Paul Huterhttps://it.mathworks.com/matlabcentral/profile/authors/8509391-paul-hutertag:it.mathworks.com,2005:Question/3419072017-05-25T13:12:19Z2017-05-26T15:02:48ZMatlab older version trial<p>Hello I am using Vivado 2015.4 System Generator and I need Matlab communication toolbox for some features I want to get the free trial of matlab that is supported by the Vivado 2015.4 but only I can get is version 2017a How can I get the 2015b version of Matlab as free trial I know that is possible for purchased products but I want to get free and legally</p>nisanur bakirhttps://it.mathworks.com/matlabcentral/profile/authors/7993985-nisanur-bakirtag:it.mathworks.com,2005:Question/629642013-02-10T23:10:21Z2017-05-26T14:59:43Zcreating 3D mesh for some points in space<p>So I have these points, A, B, C, ... in 3D. Their coordinates are denoted by x, y, z. For example point A has these coordinates: if x=2.5 and y=12, then z is 3, and B is x=4, y=3, and z=15; and so on.</p><p>So i created three arrays to show my points:</p><pre class="language-matlab">x=[2.5 4 6 18 9]; y=[12 3 7.5 1 10]; z=[3 15 16 8 11.5];
</pre><p>and i want to create a 3D mesh from my points (A, B, ...). There are total of 9 points.</p><p>I am able to create plot3 and/or scatter3 but not mesh :(</p><p>Ive spent already a full week on this and read many tutorials and such but i just get more confused and dont get it. Please help! Thanks!</p><p>lala-</p>lalahttps://it.mathworks.com/matlabcentral/profile/authors/1210661-lalatag:it.mathworks.com,2005:Question/3420792017-05-26T14:58:03Z2017-05-26T14:58:03ZHow to change the read-only property of 'Nodes' (from FEMesh) of a PDE model created with the PDE toolbox?<p>I am trying to refine a 2D mesh created with <i>geometryFromEdges</i> and <i>generateMesh</i> functions of the PDE Toolbox using functions that I created.
I am able to modify the coordinates of the nodes and the elements and visualize the result using <i>trisurf</i>, but I would like to make the changes directly to the pde model, so I can still use the <i>solvepde</i> function.</p><p>Here is the error that I am getting when I try to write new coordinates for the nodes:</p><pre class="language-matlab">You cannot set the read-only property 'Nodes' of FEMesh.
</pre>Jean-Francois Croteauhttps://it.mathworks.com/matlabcentral/profile/authors/10302831-jean-francois-croteautag:it.mathworks.com,2005:Question/2582872015-11-30T21:45:55Z2017-05-26T14:52:21Zfprintf for logical statement<p>im trying to write a single code for an fprintf.</p><p>Say I have x=1.9712 nh=0.3224 converged=true (or sometimes false)</p><p>I have written so far</p><pre class="language-matlab">fprintf('x is %d nh is %d converged is %d./n',x,nh,converged)
</pre><p>which results in, x is 1.971166e+00 nh is 3.223800e-01 converged is 1./nK>></p><p>If converged=true, I would like it to say something like x is 1.971.. nh is 3.223... convergence achieved converged=true</p><p>If converged is false I would like it to say something like x is 1.971.. nh is 3.223... convergence not achieved converged=false</p><p>The fprintf is being called in a loop I would like each time its called to have it written on a new line in the command window because now it produces on the same line.</p><p>Thanks for the help.</p>James Connorhttps://it.mathworks.com/matlabcentral/profile/authors/7128519-james-connortag:it.mathworks.com,2005:Question/3420712017-05-26T13:56:32Z2017-05-26T14:56:39ZSave object's boundary as coordinate arrays.<p>I have traced an image boundary and I need to save it as coordinate arrays. Actually I'm working on thermal image where I first extracted only the red component of the RGB image and then to black and white image then I have done morphological closing on it and obtained object's boundary.Actually I'm confused abt how to get boundary then I found it here and I have to save it in coordinate arrays.Thanks for your help in advance.
My code looks like this</p><pre class="language-matlab">a = imread('D:\New folder (5)\New folder (5)\VID-20170522-WA0021 (5-23-2017 3-45-10 PM)\yash.jpg');
a_red=a;
a_green=a;
a_blue=a;
a_red(:,:,2)=0;
a_red(:,:,3)=0;
BW = im2bw(a_red, 0.2);
se=strel('disk',10);
closeBW=imclose(BW,se);
[B,L] = bwboundaries(closeBW,'noholes');
c=label2rgb(L, @jet, [.5 .5 .5]);
imshow(c);
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)
end
</pre>yaswanth pushpakhttps://it.mathworks.com/matlabcentral/profile/authors/10381878-yaswanth-pushpaktag:it.mathworks.com,2005:Question/3416642017-05-24T07:22:17Z2017-05-26T14:49:03ZMCR addpath thirdparty toolbox<p>Hi,
I am using a custom made matlab program, but it uses additional scripts that I placed in sub-directory, in addition to it, I am using some toolboxes. The program uses the sub-routines in the toolboxes that are placed in many (sub)-directories. I would like to preserve the directory structure in my program. When I compile using addpath command and submit to a cluster, the program is terminated stating that sub-routines are not accessible. I have used</p><p>if ~isdeployed
addpath('some-directory')
end</p><p>but does not solve the problem. Any clue how to successfully run it, after compilation together with preserving the directory structure, rather than copying all the routines in the same folder?</p><p>I get this error:</p><p>Usage:
./run_image_reconstruction.sh <a href = "deployedMCRroot">deployedMCRroot</a> args</p>Golam Chowdhuryhttps://it.mathworks.com/matlabcentral/profile/authors/4835040-golam-chowdhurytag:it.mathworks.com,2005:Question/3315822017-03-23T18:33:15Z2017-05-26T14:48:54ZGet surface fitting equation with 'lowess'<p>Hello,
I used 'lowess' surface fitting method and got the surface. However, I need to get the equation for the surface so that I can use it to calculate different inputs. Could anyone help on this problem ? Thank you!</p>Li Yanghttps://it.mathworks.com/matlabcentral/profile/authors/5782038-li-yangtag:it.mathworks.com,2005:Question/3415122017-05-23T10:02:11Z2017-05-26T14:48:12ZUndefined function for input arguments of type 'char'.<p>I have a GUI window with only two push buttons. Upon clicking the first, the callback will generate a uidir to select a folder from which to work. Then the file will be reformatted and saved as a collection of smaller files. This works well. The other pushbutton is simply intended to close this current window and open another. The problem is that when I open the figure and press pushbutton one i get the following error:</p><pre class="language-matlab">Undefined function 'Window_one' for input arguments of type 'char'.
</pre><pre class="language-matlab">Error in @(hObject,eventdata)Window_one('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
</pre><pre class="language-matlab">Error while evaluating uicontrol Callback
</pre><p>I have not used the handles structure in this window as there are no variable shared between callbacks and there is no figure to update. In my experience a shortage of or mistake in the declaration of handles is the reason for this error so I am slightly confused as to what the error might be caused by. Does anyone have any idea what may cause this error to arise? There are no cases on Mathworks answers of this having arisen in the same scenario with the error occurring with a GUI window.</p><p>I have attached the code along with this question</p>Aaron Smithhttps://it.mathworks.com/matlabcentral/profile/authors/9670029-aaron-smithtag:it.mathworks.com,2005:Question/3420772017-05-26T14:27:59Z2017-05-26T14:40:09ZHow to calculate area under a "big" peak?<p>Hello. I have some dat that looks like this:</p><img src = "/matlabcentral/answers/uploaded_files/78856/Screen%20Shot%202017-05-26%20at%2015.16.10.png"><p>I want to find the area under the peak (red channel) but when I use the function <i>findpeaks</i>, what it returns is several little peaks:</p><img src = "/matlabcentral/answers/uploaded_files/78857/Screen%20Shot%202017-05-26%20at%2015.18.20.png"><p>Is there a way for Matlab to identify that the peak actually starts around pixel 40 and ends around pixel 180? And to then calculate the area under the peak between those values? Is there also a way to get an average of the "peak maximum values" (like in orange line in next graph)?</p><img src = "/matlabcentral/answers/uploaded_files/78858/Screen%20Shot%202017-05-26%20at%2015.25.44.png"><p>Thank you!</p>Catarina Figueirahttps://it.mathworks.com/matlabcentral/profile/authors/10351893-catarina-figueiratag:it.mathworks.com,2005:Question/3417322017-05-24T13:29:30Z2017-05-26T14:38:50ZThe cause of 'Error while evaluating uicontrol Callback' in GUI window<p>I have encountered this error in running a GUI window that I have created. From what I have been able to discern online, it is caused by a problem with the handle structure but the piece of code that continues to be identified by matlab as the problem is this:</p><pre class="language-matlab">Error in new_window_2>pushbutton1_Callback (line 190)
set(handles.verticalBin, 'enable', 'on');
</pre><p>I have changed a number of lines in the code but i am not sure how this (or other set(handles, 'enable', '') statements) are causing an error. How might the handles structure be failing to work properly to generate this error?</p>Aaron Smithhttps://it.mathworks.com/matlabcentral/profile/authors/9670029-aaron-smith