https://www.mathworks.com/matlabcentral/answers/questions.atomMATLAB Answers — New Questions2018-02-20T21:53:13Ztag:www.mathworks.com,2005:Question/3838122018-02-20T21:53:13Z2018-02-20T21:53:13Zjson decode parsing similar files in two different ways<p>Why does matlab parse similar json files in two different ways. I have two different json files and while one is created as structures, other is read as cells of structures, adding one more unnecessary layer. The formats of the two are same down to each bracket.</p>Kate Murphyhttps://www.mathworks.com/matlabcentral/profile/authors/8420788-kate-murphytag:www.mathworks.com,2005:Question/3838002018-02-20T20:44:02Z2018-02-20T21:53:07ZReplacing the zero entry of a vector with its nonzero entries<p>Hello,</p><p>Today I have another problem slightly different from my previous question, but they are of the same nature.</p><p>Suppose I have a vector A in which there are some zero and nonzero entries. How can I replace the nonzero entries of the vector in such a way that the replacement is done with the closet (left/right index) nonzero entry?</p><p>If 0 appears between 2 numbers then we replace it with leftmost nonzero number. For example, if A=[2;0;-1;-2;0;0;4;1], then the replacement is done as follows: the 0 in A(2) is replaced with the number in A(1) (i.e. 2) not A(3); the 0 in A(5) is replaced with the number in A(4) and the zero in A(6) is replaced with the number in A(8).</p><p>If it occurs that only 1 entry is nonzero, then we replaced all the zero entries with that nonzero entry. If we have only 2 nonzero entries in the middle of the vector, say k,k+1, then we replaced the 1,2,..,k-1 entries with nonzero entry k,and we replaced the k+2,k+3,...,n with the nonzero entry k+1.</p><p>Thank you.</p>Hassanhttps://www.mathworks.com/matlabcentral/profile/authors/3971941-hassantag:www.mathworks.com,2005:Question/3837232018-02-20T14:15:44Z2018-02-20T21:52:14ZHow can I assign counter defined data in an array in MATLAB GUIDE - ex. data_array(counter)=some_data<p>Dear MATLAB Community Members,</p><p>This is my first time posting on MATLAB Answers, so if I had omitted something or done wrong, please, forgive me for that.</p><p>At the start I would like to Thank You - in advance, for trying and hopefully helping me to solve this problem. I strongly believe, that this problem will be interesting to many future MATLAB GUI beginners, as I am myself one of them. I am using MATLAB R2017b version.</p><p><b>This GUI should work like this:</b> You enter data into the edit box (one by one), press plus - push button, the GUI counts that button press, increments the counter by 1, and after that works like: some_array(counter)=data; , I would like to store my data in an array because latter I can do whatever I like with it. For the: counter=1, some_data_array(1)=10; for counter=2, some_data_array(2)=20; for counter=3, some_data_array(3)=30; etc... data info are only as example. <i>I need, a row vector, or column - never mind... just as it is a 1D vector.</i> Info: I do not know how many entries there can be - it's up to the user, so no finite upper number for loop (for) code structures. As well, the counting is displayed in Static text box just for testing display, there will be the sum of the array, or some other data related info into static box... Please, this is the main question here - and number 1) priority.</p><p><b>1) How to enter data in a row/column vector using counter as it's element position definer (in vector) using MATLAB GUI?</b> That data/vector will be used, later, between others callbacks/functions in the GUI.</p><p>It would be better if it is possible to solve this without loops... BUT, any suggestion and any solution are more than welcome!</p><p><b>Motive's and background:</b> I started learning MATLAB GUI (now with GUIDE, will do later with App developer...) circa 7 days ago. After I had successfully completed few basic examples which included some graphing and/or basic operations and engineering formula - after few hours, I have stuck to this problem for the last six or seven days... , due to not being able to assign an counter position defined value to an array in GUI. I am pretty much frustrated at the moment for not being able to finding the way to assign a value to AN ARRAY IN GUI USING COUNTER... which is an almost basic thing to do in MATLAB - ex. repetition structures (for loop, while, etc...). I have scrolled/read many answers in MATLAB Answers, Stack overflow, internet in general, etc... the answers were nowhere to find...</p><p>Here is the whole code presented. The .fig and .m files are in attachments, and in .txt file it is presented the output from MATLAB command line, after running - executing, this program (GUI). This is just for pure practicing and learning MATLAB GUIDE and MATLAB GUI capabilities - in general, by my side.</p><p>For me, and possibly for some other future MATLAB GUI users will be very useful the answer to this problem. I am interested - in general in the solution of this problem, of course and in this code presented here - hopefully, if someone has some time to spare... I meant to say: I am working on one broader problem (GUI). And, I have segmented the problem - related to coding, in segments. And, this is one of the segments. The most problematic one - to be honest.</p><p>As well, part 2) of this question, is: How to implement the upper mentioned in a 2D matrix ( m x n ), or whatever matrix/array dimension (with multiple pages)? How the main question labeled 1), can be done for higher order matrices, starting from 2D, 3D, etc... the general principle... and/or code hopefully.</p><p>The reason I need an array, is because when I have it, I can do whatever I need with it, and later will find the: sum(some_data_array), and use some other statistical properties on data. All the data is entered trough the same Edit Box - this is important to minimalize the GUI. As well, as a bonus question 3) : How You save/export that created array (some_data_array) to .dat file or .m file (from the 'basis' workspace...)? Asking this, because the data or analysis, can be used by other related applications...</p><p>Sincerely, Thank You.</p><p>Damir</p><p>EDIT: Have removed the code from the body of question - as suggested, and have attached the .fig file and .mat file for easier testing.</p><p><b>Short command window preview after</b> <i>counter=4</i> :</p><p>handles =</p><pre class="language-matlab">struct with fields:
</pre><pre> figure1: [1×1 Figure]
text2: [1×1 UIControl]
edit1: [1×1 UIControl]
pushbutton1: [1×1 UIControl]
output: [1×1 Figure]
input1: 40</pre><p>data_edit1 =</p><pre> 40</pre><p>C =</p><pre> 40</pre><p>br =</p><pre> 4</pre><p>intermediary_array =</p><pre> 0 0 0 40
>></pre>Teodohttps://www.mathworks.com/matlabcentral/profile/authors/7095160-teodotag:www.mathworks.com,2005:Question/3827252018-02-14T18:46:55Z2018-02-20T21:51:50ZHow to convert a cell to a string? <p>I have attempted to convert a cell to a string using cellstr (which doesn't work)... I need a column of cells (all containing numbers) to be strings so I can manipulate them. Thank you!</p>Lexington Stoyellhttps://www.mathworks.com/matlabcentral/profile/authors/8693441-lexington-stoyelltag:www.mathworks.com,2005:Question/3837922018-02-20T20:05:22Z2018-02-20T21:49:33ZHow to use the lsim function<p>I would like to check to see if the transfer function I developed through experimentation is correct by using two other transfer functions. This is my code:</p><pre class="language-matlab">%% Loading the data from excel to MATLAB for all trials
P5 = 'P5.csv'; % First trial: gain = 5
P5V2 = 'P5V2.csv'; % Second trial: gain = 5
P5V3 = 'P5V3.csv'; % Third trial: gain = 5
P4 = 'P4.csv'; % Lower gain
P6 = 'P5V3.csv'; % Higher gain
P10 = 'P10.csv';
P15 = 'P15.csv';
P2 = 'P2.csv';
</pre><pre class="language-matlab">% Reading the xls files into MATLAB
p5 = csvread(P5);
p5v2 = csvread(P5V2);
p5v3 = csvread(P5V3);
p4 = csvread(P4);
p6 = csvread(P6);
p10 = csvread(P10);
p15 = csvread(P15);
p2 = csvread(P2);
</pre><pre class="language-matlab">%% Analysis Question 1: Version 1 of Gain=5
time = p5(:,1);
output = p5(:,2);
error = p5(:,3);
</pre><pre class="language-matlab">% Creating a reference signal
% E = S - P, where S is the signal and P is position, then S = E + P
refsig = error + output;
</pre><pre class="language-matlab">% Plotting motor output vs. time of our best plot with a reference signal on
% the same graph using the hold on function
figure(1)
plot(time, refsig, 'b');
hold on
plot(time, output, 'r');
hold off
title('Motor Position Over a Period of Time at a Gain of 5');
xlabel('Time (s)');
ylabel('Motor Output');
ylim([-80 80]);
legend('Reference Signal','Motor Signal');
</pre><p>So want I would like to do is re-create my reference signal and simulate it in a closed loop using the 'lsim' function in MATLAB. The transfer function I checking is P5 and I am using P6 and P15 to compare the results with. So basically I am just unsure of how to use the lsim function. This is a second order system and I would like to simulate a step input.</p>Daiana Spataruhttps://www.mathworks.com/matlabcentral/profile/authors/12203760-daiana-spatarutag:www.mathworks.com,2005:Question/3837712018-02-20T18:11:42Z2018-02-20T21:48:42ZHow to iteratively split a matrix into multiple matrices according to a condition?<p>I have a 2 column matrix with many many rows.</p><p>It's been sorted according to the values in the first column.</p><p>I now want to split the matrix into ten matrices.</p><p>Those values in the first column which are 0-10% of the highest value will go in one matrix, those that are 10-20% of the highest value will go in the next... you get the idea.</p><p>How would I go about doing this? I presume a for loop is involved, but am struggling to make it work. Any advice would be very much appreciated - thank you!</p>ishbishhttps://www.mathworks.com/matlabcentral/profile/authors/11959845-ishbishtag:www.mathworks.com,2005:Question/3825492018-02-14T05:31:35Z2018-02-20T21:43:15Zneural nework validation accuracy calculation <p>I am currently training a CNN to do modulation classification using NN toolbox. The validation accuracy reported by matlab through the training progress window is approximated 50% at the end of training. But when I tested the accuracy myself using the trained network with the validation data set, it's only about 20% to 25%. This is calculated as (num of correct classification / overall number of tests).</p><p>How does matlab calculate the validation accuracy? Where can I find more information? Thanks.</p>Joyhttps://www.mathworks.com/matlabcentral/profile/authors/2242496-joytag:www.mathworks.com,2005:Question/3837722018-02-20T18:17:48Z2018-02-20T21:40:01ZConvert a Cell to String<p>Can you convert a cell to a string? How do you do so? Are there better types to use instead of a string? Or should you convert a cell to something first and then convert that thing to a string? I have a single column with 100+ rows of data that are currently cells being imported from Excel that I would like to manipulate as strings to separate the data.</p>Lexington Stoyellhttps://www.mathworks.com/matlabcentral/profile/authors/8693441-lexington-stoyelltag:www.mathworks.com,2005:Question/3204812017-01-15T15:18:26Z2018-02-20T21:36:57ZPI controller tuning using Controller designer in simulink<p>I'm using Matlab R2015a. I am developing a grid-tied PV inverter in simulink, I have developed the controller for the inverter but can't tune it with 'Controller designer'. I referred to video from mathworks by carlos osario, I tried his method and failed. How can I tune the PI controller in simulink</p>Muwaffaq Usmanhttps://www.mathworks.com/matlabcentral/profile/authors/8915734-muwaffaq-usmantag:www.mathworks.com,2005:Question/3102232016-11-02T04:18:46Z2018-02-20T21:35:24ZI am executing the following code to realize 2X2 MIMO using comm.MIMOchannel system object. In the last line the variables chan_out and chan_out1 are having different outputs even though the instructions are same. Please help me rectifying the error<pre class="language-matlab">Nt=2;Nr=2;
inpt=randi([0 1],Nt,3);
</pre><pre class="language-matlab">hchan = comm.MIMOChannel(...
'SpatialCorrelation',false, ...
'NumTransmitAntennas',Nt, ...
'NumReceiveAntennas',Nr);
</pre><pre class="language-matlab">chan_out=step(hchan,inpt');
chan_out1=step(hchan,inpt');
</pre>RAGHAVENDRAhttps://www.mathworks.com/matlabcentral/profile/authors/5193291-raghavendratag:www.mathworks.com,2005:Question/3838102018-02-20T21:33:38Z2018-02-20T21:34:00ZDetermine distance between point from a data table<p>My collaborator sent me a HUGE 200mb table of data as a TXT. I can import it fine, phew.</p><p>Column 6 and 7 are X and Y coordinates.</p><p>I want to figure out the delta/change in distance between row 1 and row 2, row 2 and row 3, etc.
We want to see how far a bird moves each second.</p><p>My attempts are laughably crappy.</p><p>Problem A: Transforming the table data to formats that functions (like "dist") can use, like a matrix or something.
Problem B: Setting up a loop to find the positive distance change for each row and the one below it, then write it to a new array or somesuch.</p>Constance Woodmanhttps://www.mathworks.com/matlabcentral/profile/authors/10602247-constance-woodmantag:www.mathworks.com,2005:Question/3809282018-02-05T21:07:04Z2018-02-20T21:27:40ZWhy does my MatLab documentation seem to be missing after a (very) recent license renewal?<p>I renewed my MatLab software license earlier today. And then I upgraded from R2016b to R2017b.</p><p>Before the renewal and upgrade, MatLab's help was working very well. Now, whenever I search Help for a word, MatLab's Help feature responds, "An error occurred searching the documentation.".</p><p>How can I fix MatLab's Help feature?</p>Michael McCormickhttps://www.mathworks.com/matlabcentral/profile/authors/7260272-michael-mccormicktag:www.mathworks.com,2005:Question/3837822018-02-20T19:10:25Z2018-02-20T21:27:26Zwe have a pop up window saying 'Deactivation Required'<p>we have a pop up window saying 'Deactivation Required'
Our license XXXXXX (10.66.10.19) is valid.
why did we get this pop up from time to time?
Thanks for your help.</p><p>Ping</p>Vadim Kouevdahttps://www.mathworks.com/matlabcentral/profile/authors/8248998-vadim-kouevdatag:www.mathworks.com,2005:Question/3838072018-02-20T21:10:10Z2018-02-20T21:18:21ZHow can I run an .exe from Matlab that requires user input?<p>I need to run an executable called program.exe that waits for the user to provide an input (write a file name and press enter). If I write</p><pre class="language-matlab">system('cd path_to_exe\program.exe')
</pre><p>the executable header text starts appearing in the Matlab console, but pauses midway indefinitely and I have to Crt+C to quit the program. Writing</p><pre class="language-matlab">system('cd path_to_exe\program.exe < NUL:')
</pre><p>displays the whole program.exe header, but then I am unable provide the necessary input ('file.txt'). Writing</p><pre class="language-matlab">!start path_to_exe\program.exe
</pre><p>opens the .exe in a cmd console, but I am unable to provide the input ('file.txt') from Matlab.
How can I run the .exe and provide the text input from Matlab?</p>Hasier Goitiahttps://www.mathworks.com/matlabcentral/profile/authors/9167999-hasier-goitiatag:www.mathworks.com,2005:Question/3817552018-02-09T14:30:35Z2018-02-20T21:16:56ZError using pdepe function in matlab.<p>The code runs well with M as a numeric but when I change it to a range of values, it gives me a error.
How can I rectify this?</p>Brianhttps://www.mathworks.com/matlabcentral/profile/authors/12102871-briantag:www.mathworks.com,2005:Question/3838082018-02-20T21:16:48Z2018-02-20T21:16:48ZCan bayesopt() be run without an objective function call?<p>I have a list of initial X values and initial objectives, as well as variable ranges and parameters, but would like to just extract the next suggested point to sample with bayesObject('NextPoint'), because the evaluation is not a function I can easily put into MATLAB. I attempted to make a dummy function with the correct number of inputs and set 'MaxObjectiveEvaluations' to 0 to avoid any actual function calls:</p><pre> bayesObject = bayesopt(dummyFunc,var1,'InitialX',initialXList,'InitialObjective',initialObjList,'MaxObjectiveEvaluations',0);</pre><p>but received the error</p><pre class="language-matlab">Error using bayesoptim.BayesoptOptions/checkAndFillStoppingCriteria (line X)
'MaxObjectiveEvaluations' must be a positive integer.
</pre><p>Can the 'NextPoint' property be called without ever calling the dummy function? Alternatively, is there a different function I should be using rather than bayesopt() for this?</p>David Freiberghttps://www.mathworks.com/matlabcentral/profile/authors/3746739-david-freibergtag:www.mathworks.com,2005:Question/2183672015-05-24T07:14:47Z2018-02-20T21:13:52ZEfficient implementation of linprog<p>I am using linprog to solve a linear optimization problem with thousands of constraints. I have noticed that for constraints more than a few thousand the matlab implementation of linprog performs very slow. Are there any GPU(CUDA) or parallel implementations of linprog available within Matlab or elsewhere ?</p>sidhttps://www.mathworks.com/matlabcentral/profile/authors/4250706-sidtag:www.mathworks.com,2005:Question/3834492018-02-19T07:27:23Z2018-02-20T21:13:01ZHow to make genetic algorithm output only one powerloss result<p>Hello everyone
i am new here. I am having a problem with my genetic algorithm. i am doing a project on optimal placement of distributed generation to minimise using both PSO and GA to compare. My genetic algorithm is giving several values of powerloss for one time i am running. i do not know how to get only one answer for the GA just like in PSO. can anyone help me please? i can send my code so that you can see.
thank you</p>Aisha Jahangeerhttps://www.mathworks.com/matlabcentral/profile/authors/12190099-aisha-jahangeertag:www.mathworks.com,2005:Question/3838062018-02-20T21:07:39Z2018-02-20T21:07:39Zchoose points lie near a line on a surface plot<p>I have a meshgrid of [x y] = meshgrid(1:100,1:100);
z could be any function of the x and y or some experimental data.</p><p>Then make a surface plot of z using surface(z).
I want to draw a line on the surface with GUI and get the points lying on the line, if the line doesn't pass any points, find the points close to it instead. The imline only works on the image object. It gives me an error for the surface plot. The ginput cannot get the points either. Is there an easy way to do this?</p><p>I also want to choose an ROI on the surface plot. Something like the roipoly which returns a mask. But it seems this function only works on an image object. Is there a similar function on the surface plot or how can I convert the surface object to an image object?</p>Huayan Wanghttps://www.mathworks.com/matlabcentral/profile/authors/4862849-huayan-wangtag:www.mathworks.com,2005:Question/3837832018-02-20T19:13:42Z2018-02-20T21:06:02Zdr/dt* ln(a*r*dr/dt)=b/r^7 how to solve this equation<p>kindly help me to solve this equation a = 0.5, b=2, r(0)=1.2</p>vishal vyashttps://www.mathworks.com/matlabcentral/profile/authors/8217805-vishal-vyastag:www.mathworks.com,2005:Question/3836812018-02-20T11:11:54Z2018-02-20T21:05:56ZHow to adjust the "isnan" to make it work for cell?<p>Hi I have the below for loop. I would like to replace the NaN data output with 0.07. I keep getting the below error message. Could anyone please advise me how to stop the error message? Thank you.</p><pre class="language-matlab">for i=1:size(A,2)%number of columns
x{i} = interp1(y3, x3, A(:,i), 'linear');
</pre><pre class="language-matlab">k=1:1;
temp=x(k,:);
temp(isnan(temp))=0.07;
x(k,:)=temp;
fid=fopen(['result_' num2str(1) '.txt'],'w');
fprintf(fid,'%f\n',x);
fclose(fid);
end
</pre><p>The error message:</p><pre class="language-matlab">Undefined function 'isnan' for input arguments of type 'cell'.
Error in InterpolaeMFJavadCompleted (line 24)
temp(isnan(temp))=0.07;
</pre>Ismail Qeshtahttps://www.mathworks.com/matlabcentral/profile/authors/10292327-ismail-qeshtatag:www.mathworks.com,2005:Question/3837762018-02-20T18:49:40Z2018-02-20T21:04:34ZLithium ion battery simulation for EV - Simscape language integration fucntion <p>I am trying to model the response of a Lithium-ion battery for use in an EV to a drive cycle and recieve the error in 'Capture_1'. The source code for the custom Em block is shown in Capture_2 through Capture_5.</p><p>I have run out of ideas on how to solve this problem as I have not encountered it before and the Mathworks support on this doesn't provide much insight for me.</p><p>Thanks in advance</p>Peadar Cruddenhttps://www.mathworks.com/matlabcentral/profile/authors/11170710-peadar-cruddentag:www.mathworks.com,2005:Question/3838052018-02-20T05:00:00Z2018-02-20T20:59:02ZWhy do I encounter the error stating I have exceeded the license limit of 1000 nonvirtual blocks?<p>Why do I encounter the error stating I have exceeded the license limit of 1000 nonvirtual blocks?</p>MathWorks Support Teamhttps://www.mathworks.com/matlabcentral/profile/authors/4622813-mathworks-support-teamtag:www.mathworks.com,2005:Question/3837952018-02-20T20:24:04Z2018-02-20T20:58:52ZNested For Loop Help Please<p>Hey All hope you can help</p><p>I am having trouble getting data stored from my initial loop which I want to use in a later loop.
The initial loop produces 40 sets of unique 2*2 matrix (A).</p><p>I am also struggling with being able to call the Matrix (A) in my nested loop i.e. I want to call the entire matrix and not just values within the matrix.
My internal loop within the nested matrix works I just need the outer loop to work.</p><p>for example- A1= [0 1; 1.5 4] and A2= [0 1; 1 5.6] etc and i want to be able to call
A1* .... etc
A2 *... etc</p><p>Thank you in advance!</p><pre class="language-matlab">zita =0.05; %inherrent damping
m= 773; %mass in MG
b=[0
1/m];
</pre><pre class="language-matlab">for T0=0.1:0.1:4; % Range of Time Periods defined in seconds
w = (2*pi)/T0; %rad/s
w2=w*w;
zita=0.05;
A=[0 1
-w2 -(2*zita*w)];
</pre><pre class="language-matlab">end
</pre><pre class="language-matlab">%nested loop to calculate the displacements with the corresponding A matrix
for % I need to call the A matrix here
for i=1:(NPTS_interp-1)% Runge-Kutta loop, i = incremental time step
</pre><pre> g1 = A*yt(:,i)+b*ft_interp(i);
g2 = A(1:end)*(yt(:,i)+ 0.5*g1*dt_interp)+0.5*b*(ft_interp(i)+ft_interp(i+1));
g3 = A(1:end)*(yt(:,i)+ 0.5*g2*dt_interp)+0.5*b*(ft_interp(i)+ft_interp(i+1));
g4 = A(1:end)*(yt(:,i)+g3*dt_interp)+b*ft_interp(i+1);</pre><pre> ynew = yt(:,i)+(g1+2*g2+2*g3+g4)*dt_interp/6;
yt(:,i+1)=ynew; %storage
ut=yt(1,:); %displacement values
vt=yt(2,:);%velocity values
[pks,locs]=findpeaks(ut,t_interp);
Max_displacement=max(pks);
end
end</pre>Serena Solankihttps://www.mathworks.com/matlabcentral/profile/authors/10046666-serena-solankitag:www.mathworks.com,2005:Question/3838042018-02-20T20:57:52Z2018-02-20T20:58:19ZAssigning class +1/-1 to a 100x2 matrix<p>I have a 100x2 matrix D. A scatter plot is attached. I have a 100x1 vector 'c'. If a point in D is to the left, I want to assign it as +1 and -1 otherwise. I know it sounds easy and I was trying this snippet:</p><p>if D(1:100,1)<mean(D(:,1))
c(:,1) = 1;
else c(:,1) = -1;
end</p><p>but it somehow doesn't do the job. I think we don't even need an if loop. Thanks for your comments.</p><img src = "/matlabcentral/answers/uploaded_files/105578/scatter.jpg">Deepayan Bhadrahttps://www.mathworks.com/matlabcentral/profile/authors/8596145-deepayan-bhadratag:www.mathworks.com,2005:Question/3738492017-12-20T09:22:58Z2018-02-20T20:48:15ZROC curve AlexNet CNN<p>How to calculate the ROC curve using AlexNet CNN from Matlab? I have two class.</p>Gledson Melottihttps://www.mathworks.com/matlabcentral/profile/authors/7172134-gledson-melottitag:www.mathworks.com,2005:Question/3837862018-02-20T19:45:56Z2018-02-20T20:47:53ZClabel can not change font color<p>I want to label the values on a contour figure with a customized font color. In old version matlab, clabel works well.</p><p>For example,</p><pre class="language-matlab">[x,y,z] = peaks;
[C,h] = contour(x,y,z);
set(h,'showtext','on')
clabel(C,h,'FontSize',15,'Color','red')
</pre><p>Now, my matlab is updated. Unfortunately, the 'color' property of clabel does not work any more.</p><p>I used clabel(C,'FontSize',15,'Color','red') in stead. But annoying "+" always go with the contour values.</p><p>Who knows how we can set the font color without the "+" following?</p><p>Many thanks.</p>Xuelei Fenghttps://www.mathworks.com/matlabcentral/profile/authors/11343038-xuelei-fengtag:www.mathworks.com,2005:Question/3837732018-02-20T18:19:13Z2018-02-20T20:45:26ZWhat is the appropriate technique for Image classifier for stationary tools pen, pencil, ruler, eraser?<p>I'm going to construct an image classifier for stationary tools pen, pencil, ruler, eraser.
Please tell me if the function trainImageCategoryClassifier() of the statistical toolbox is appropriate for this task or should I use neural network classifier, if so, which type sould I use?</p>Ezzathttps://www.mathworks.com/matlabcentral/profile/authors/2633659-ezzattag:www.mathworks.com,2005:Question/3837812018-02-20T18:59:51Z2018-02-20T20:45:04ZTo catch correct jump and store the number<p>Hi
I want to record the up and down count of the signal when it satisfies my threshold condition but the code is not working. I need to catch the jump shown by red in the image. I have attached the image as well as my code so can anyone solve it sooner. Thank you for your help!</p><img src = "/matlabcentral/answers/uploaded_files/105562/Inkedsumansuman_LI.jpg"><pre class="language-matlab">phi(1)=0.66702; phi(2)= 0.66703; phidc(1)=0;phidc(2)= e^-5; count0=0; count1=0;
</pre><pre class="language-matlab">for n=2:10^7;
</pre><pre class="language-matlab">p= 0.5*rand-0.25; q = rand -0.5 ;
</pre><pre class="language-matlab">phi(n+1)= 0.0031+ 1.97*phi(n) -0.97*phi(n-1)- 0.0039*cos( 0.5*phidc(n))* sin(phi(n)) + 0.039* q ;
</pre><pre class="language-matlab">phidc(n+1)= 1.89*phidc(n) -0.97*phidc(n-1)- 0.0079*sin(0.5*phidc(n))* cos(phi(n)) + 4*0.039* p ;
</pre><pre class="language-matlab">if phi(n+1)> 4.5 && phi(n)< 4.5 ;
count1= count1+1 ;
UP(count1)= n;
</pre><pre class="language-matlab">if phi(n+1)< 1.5 && phi(n)> 1.5 ;
count0= count0+1 ;
DOWN(count0)= n; end end end
</pre>suman Dhamalahttps://www.mathworks.com/matlabcentral/profile/authors/7418785-suman-dhamalatag:www.mathworks.com,2005:Question/3836292018-02-20T05:47:31Z2018-02-20T20:44:22ZPlot magnitude spectrum from fourier series?<img src = "/matlabcentral/answers/uploaded_files/105421/ddds.png"><p>Figure 1: Signal 4</p><p>Express the signal given in figure 1 as a Fourier series expansion.</p><p>The fundamental frequency is 100 Hz and the amplitude is 1.</p><p>Write a function called [time, signal_output]=plot_signal4 that is called like this plot_signal4(). This function plots and outputs signal 4 in the time domain using the Fourier series and the number of harmonics, n = 2000. Set the time resolution to 1/fs where fs (sampling frequency) = 44100 and the time from 0 to 0.03 seconds.</p><p>Write a function called [frequency,magnitude]=plot_signal4_mag_spec that is called like this plot_signal4_mag_spec(). This function plots the magnitude spectrum of signal 4 and outputs the frequency vector and the magnitude vector. Do not use the fft_wrapper function. You will have to convert the equation from its trigonometric form to its harmonic form. Set the number of harmonics (n) to 300 and the frequency range from 0 to (n×f0) Hz, where f0 is the fundamental frequency. Set the frequency resolution to the fundamental frequency.</p><p>I have answered the first part:</p><pre> function [time, signal_output] = plot_signal4()
%UNTITLED11 Summary of this function goes here
% Detailed explanation goes here
fs= 44100;
time = 0: 1/fs : 0.03;
f = 100;
T=1/f;
signal_output = 0;
a0=1/3;
n = 2000;
for k=1:1:n
ak=((-12/T^2)*(cos(2*pi*k*f*T/3)-1))/(2*pi*k*f)^2;
signal_output = signal_output + ak*cos(2*pi*f*k*time);
end
signal_output = signal_output +a0;
plot(time, signal_output);
xlabel('Seconds');
ylabel('Amplitude');
ylim([0 inf]);
end</pre><p>However I am unable to get a correct answer for the second part, finding the magnitude spectrum. I have tried the following approaches:</p><pre> if true
function [frequency, magnitude] = plot_signal4_mag_spec()
%UNTITLED12 Summary of this function goes here
% Detailed explanation goes here
[time, signal_output] = plot_signal4();
f0 = 100;
dft_sig = fft(signal_output);
f = 0:f0:300*f0; %(0:length(dft_sig)-1)*50/length(dft_sig);
y = abs(dft_sig);
plot(f,y(1:301));
n = length(signal_output);
frequency = (-n/2:n/2-1)*(50/n);
magnitude = fftshift(y);
stem(frequency,abs(magnitude))
end</pre><p>and this:</p><pre> f0 = 100;
n = 300;
T = 1/f0;
frequency = 0:f0:n*f0;
sz = size(frequency);
magnitude = zeros(sz);
for k = 0:1:300
magnitude(k+1) = abs(((-12/T.^2)*(cos(2*pi*k.*f0*T/3)-1))/(2*pi*k.*f0).^2);
end
plot (frequency, magnitude, '.');</pre><pre class="language-matlab">end
</pre>Aasif Edoohttps://www.mathworks.com/matlabcentral/profile/authors/12065409-aasif-edootag:www.mathworks.com,2005:Question/3837982018-02-20T20:39:25Z2018-02-20T20:44:20ZProblem in serial communication between Matlab and Arduino <p>Hello,</p><p>I got this error after running the program</p><pre class="language-matlab">Warning: Unsuccessful read: A timeout occurred before the Terminator was reached..
In an assignment A(I) = B, the number of elements in B and I must be the same.
</pre><pre class="language-matlab">Error in FLJZ7QBISCB49A5 (line 15)
CM(2)=fscanf(arduino,'%f');
</pre><p>And my code is</p><pre class="language-matlab">% Real time plotting.
clear all
close all
clc
</pre><pre class="language-matlab">arduino=serial('COM6','BaudRate',9600); % create serial communication object on port COM6
fopen(arduino); % initiate arduino communication
</pre><pre class="language-matlab">CM(1)=0;
time(1)=0;
i=1;
tic;
</pre><pre class="language-matlab">while (toc<=100)
CM(2)=fscanf(arduino,'%f');
time(2)=toc;
figure(1);
grid on;
axis([toc-10, toc+10, 0, 35])
h(i)=plot(time,CM,'b','LineWidth',5);
hold on
CM(1)=CM(2);
time(1)=time(2);
if(i >= 300)
delete(h(i-299));
end
i=i+1;
</pre><pre class="language-matlab">end
</pre><pre class="language-matlab">fclose(arduino); % end communication with arduino
</pre><p>My project is ball balancing I have a variable named (cm) in arduino i want to get this variable value and then save it in CM(2) but the command fscanf(arduino,'%f'); gives me a vector of values so how can i get just the cm variable to work on it.
I need your help.</p>Zack Elhttps://www.mathworks.com/matlabcentral/profile/authors/10041352-zack-eltag:www.mathworks.com,2005:Question/3838012018-02-20T05:00:00Z2018-02-20T20:44:03ZWhy is the license manager missing from the download only product selection screen?<p>Why is the license manager missing from the download only product selection screen?</p>MathWorks Support Teamhttps://www.mathworks.com/matlabcentral/profile/authors/4622813-mathworks-support-teamtag:www.mathworks.com,2005:Question/3837932018-02-20T20:12:38Z2018-02-20T20:41:44Zhow i can get loop?<p>If the condition on line 33 acceptable, then return to the beginning of the program else will continue, I want a loop that its the output only lines 40-45, how i can to do it ... can i get example</p><img src = "/matlabcentral/answers/uploaded_files/105573/loop.PNG">kamal farishttps://www.mathworks.com/matlabcentral/profile/authors/8166099-kamal-faristag:www.mathworks.com,2005:Question/3837992018-02-20T20:40:36Z2018-02-20T20:40:36ZHow to terminate simulation and output timesteps to termination<p>Hello everyone!</p><p>I am attempting to count the number of time steps it takes for a simulation to reach equilibrium, and once its there to terminate the simulation. Here, the simulation reaches equilibrium when n(t+1) = n(t) AND when S(t+1) = S(t). The following code runs the simulation until t = T, but I want it to stop when it reaches the above conditional and then I want it to output eq = t where t is the amount of time it took to reach equilibrium. Does anyone know how to make this happen, as this code is spitting out errors?</p><pre> function [n, S] = simulate_dynamics(n0, S0, dt, T, A, B, C, D, beta, thetaI, thetaC, lambda, alpha, K)</pre><pre> eq = []
S(1) = S0;
n(1) = n0;
for t = 1:T
pStart = A + B * (S(t).^2./(S(t).^2 + thetaI.^2));
pStop = C + D * (1 ./ (1 + exp(beta*(S(t)-thetaC))));
n(t+1) = n(t) + dt * (pStart * (1 - n(t)) - pStop*n(t));</pre><pre> S(t+1) = S(t) + dt * (lambda*S(t).^alpha - K*n(t));</pre><pre> % keep both variables inside [0 1]
if S(t+1) > 1
S(t+1) = 1;
end
if n(t+1) > 1
n(t+1) = 1;
end
if S(t+1) < 0
S(t+1) = 0;
end
if n(t+1) < 0
n(t+1) = 0;
end</pre><pre> if n(t+1) = n(t) & S(t+1) = S(t)
eq = t
return
end
end</pre>Colin Lynchhttps://www.mathworks.com/matlabcentral/profile/authors/7536193-colin-lynchtag:www.mathworks.com,2005:Question/3837962018-02-20T20:27:42Z2018-02-20T20:38:19ZFrequency response function In matlab<p>Can someone help me with this matlab frequency response problem.</p><p>This is what I have so far, but code doesnt work.</p><pre class="language-matlab">function [H] = freqresp(b, a, w)
</pre><pre class="language-matlab">a(1) = 1;
</pre><pre class="language-matlab">numer(0) = 0;
</pre><pre class="language-matlab">denom(1) = 0;
</pre><pre class="language-matlab">for k=1:length(b)
</pre><pre class="language-matlab">numer(k) = numer(k-1) + b(k) * exp(i*w*k);
end
</pre><pre class="language-matlab">for m=2:length(a)
</pre><pre class="language-matlab">denom(m) = denom(m-1) + a(m)*exp(i*w*k);
</pre><pre class="language-matlab">end
</pre><pre class="language-matlab">H = numer/denom;
</pre>shabaz leehttps://www.mathworks.com/matlabcentral/profile/authors/12203766-shabaz-leetag:www.mathworks.com,2005:Question/3837972018-02-20T20:33:19Z2018-02-20T20:37:06ZExporting data set in terms of first element in columns<p>If I had a matrix (1000x4), how would I separate them in terms of their first columns?
I want to store these matrices as something such as a = rows that start with 1s, b = rows that start with 2s, c = rows that start with 3s, etc... that also give all the elements in the row i.e c = [3 1 2 3; 3 4 5 6] Is there a way to make this into a for-loop function?
Thanks!</p><pre> mat = 1 2 3 4
1 5 6 7
1 8 9 0
1 1 3 5
2 2 3 4
2 4 5 6
2 7 8 9
3 1 2 3
3 4 5 6</pre>Monicahttps://www.mathworks.com/matlabcentral/profile/authors/11953903-monicatag:www.mathworks.com,2005:Question/3837852018-02-20T19:39:33Z2018-02-20T20:36:20Zhow to find mean absolute deviation image?<p>I want to display mean absolute deviation image from a given input jpg Image. MAD is an energy measure that represents regularity of texture.</p>Khushal Gumgaonkarhttps://www.mathworks.com/matlabcentral/profile/authors/3243335-khushal-gumgaonkartag:www.mathworks.com,2005:Question/3837492018-02-20T16:05:03Z2018-02-20T20:34:43ZDigitization of noisy signal<p>I have a signal as shown in the image here. I am trying to digitize the signal so that it catches the right jump only. My system is jumping between two states with noise. I want to calculate how much time it stays up and how much down. Could you please bring a super cool idea? Thanks for your help</p><img src = "/matlabcentral/answers/uploaded_files/105512/suman.png">suman Dhamalahttps://www.mathworks.com/matlabcentral/profile/authors/7418785-suman-dhamalatag:www.mathworks.com,2005:Question/3837872018-02-20T19:52:53Z2018-02-20T20:31:32ZSort coordinates based on closest connectivity?<p>Hello,</p><p>I have an nx3 array representing x,y,z coordinates in space. These coordinates are all connected and represent a tortuous line. I would like to calculate the length of this line, however the coordinates I have are not in order of connectivity. Is it possible to reorder them such that I can simply calculate the length by doing a for loop like:</p><pre class="language-matlab">length = 0;
for i = 1:length(array)-1
length = norm([coords(i,1)-coords(i+1,1),coords(i,2)-coords(i+1,2),coords(i,3)-coords(i+1,3)]);
end
</pre><p>I have tried just finding the neighbours of each coordinate and reordering that way, but this gets very convoluted quickly and requires that I "walk" through the line. For the total dataset I am trying to analyze I will literally have millions of these lines, so I am trying to avoid slow solutions like that.</p>Eric Chadwickhttps://www.mathworks.com/matlabcentral/profile/authors/7566687-eric-chadwicktag:www.mathworks.com,2005:Question/3836022018-02-19T23:57:35Z2018-02-20T20:25:00ZRandom generation of coordinate points that lie outside of defined surfaces. <p>I have a question regarding the generation on random xy coordinates in an already defined area. Now the area is comprised of many little sub-areas representing aggregates in the material's micro-structure. I want the randomaly generated coordinates (i.e. centroids of the air voids) to lie outside those sub-areas. The program I have so far can plot the xy coordinates of all of the nodes making up the lines which connect to establish the sub-areas. Any help would be much appreciated. Thanks.</p>Aimane Najmeddinehttps://www.mathworks.com/matlabcentral/profile/authors/6926934-aimane-najmeddinetag:www.mathworks.com,2005:Question/3833762018-02-18T18:24:04Z2018-02-20T20:24:07ZReplacing the zero entry of a vector with the corresponding entry of another vector of the same dimension.<p>Hello everyone,</p><p>Please I need some help about this:</p><p>Suppose I have 2 vectors A=[0;2;0] and B=[3;5;7]. How can I write a command in MATLAB that simply replace the zero entries of vector A with the corresponding non-zero entries of vector B?</p><p>In general, if I have 2 vectors A and B of the same dimension, assuming vector A have some zero entries, but the vector B have no zero entries. How can I replace the all the zero entries of vector A with that of the corresponding entries of vector B?</p><p>Thanks for any help.</p>Hassanhttps://www.mathworks.com/matlabcentral/profile/authors/3971941-hassantag:www.mathworks.com,2005:Question/3837742018-02-20T18:25:48Z2018-02-20T20:20:12ZNewbie question - Error in line 2<p>Sorry to ask a really newbie question. Does anyone know why the attached does not work?</p>Jonas Perssonhttps://www.mathworks.com/matlabcentral/profile/authors/12203047-jonas-perssontag:www.mathworks.com,2005:Question/3837892018-02-20T19:57:17Z2018-02-20T20:09:19ZHow to subset rows with reproducibility?<p>If I have a 10 by 5 matrix, how would I subset the data so that the same rows are selected each time?</p><p>I know how to select 10 random rows, but I'm unsure how to make it reproducible.</p>Chris Hhttps://www.mathworks.com/matlabcentral/profile/authors/6940214-chris-htag:www.mathworks.com,2005:Question/3835812018-02-19T21:28:18Z2018-02-20T20:06:49ZHow to interpolate a matrix<p>Essentially I didn't know how to ask this as it probably is simple but I don't quite know what to do.</p><p>Essentially I want to create a response function for an earthquake record, where ultimately I want to plot the peak displacement against a varying time period. I am using the Runge-Kutta Method to integrate to get displacement from the acceleration data.</p><p>I have got a range of values for the time period -T0 and have calculated the state-space matrix A from this.</p><p>essentially I am unsure if i need to interpolate my A matrix to be the same size as my Npts in order for the loop to work.</p><p>Hope it makes sense and hope you can help!</p><p>Thanks in advance!!</p><pre class="language-matlab">for i=0.1:0.1:4; % Range of Time Periods defined
T0=(i);% seconds
w = (2*pi)/T0(1:end); %rad/s
w2=w(1:end)*w(1:end);
zita=0.05;
m=773; % Mass in MG
A1=[0 1
-w2(1:end) -(2*zita*w(1:end))]; %state-space matrix of coefficients
b=[0
1/m]; %state-space load vector
</pre><pre> end </pre><pre class="language-matlab">ft = m*acc.'; %acceleration data- converting to force f=ma
</pre><pre> dt = 0.02; %seconds
NPTS = size(ft); %no.of points
tf = (NPTS-1)*dt;
t = 0:dt:tf;
A=A1(1:end);</pre><pre> dt_interp = 0.0001;
t_interp = dt_interp:dt_interp:tf;
ft_interp = F_interp(t, ft, t_interp);
NPTS_interp = size(t_interp,2);</pre><pre> yt = zeros(2,t_interp);</pre><pre> for i=1:(NPTS_interp-1)% Runge-Kutta loop, i = incremental time step</pre><pre> g1 = A(1:end)*yt(:,i)+b*ft_interp(i);
g2 = A(1:end)*(yt(:,i)+ 0.5*g1*dt_interp)+0.5*b*(ft_interp(i)+ft_interp(i+1));
g3 = A(1:end)*(yt(:,i)+ 0.5*g2*dt_interp)+0.5*b*(ft_interp(i)+ft_interp(i+1));
g4 = A(1:end)*(yt(:,i)+g3*dt_interp)+b*ft_interp(i+1);</pre><pre> ynew = yt(:,i)+(g1+2*g2+2*g3+g4)*dt_interp/6;
yt(:,i+1)=ynew; %storage
ut=yt(1,:); %displacement values
vt=yt(2,:);%velocity values
[pks,locs]=findpeaks(ut,t_interp);
Max_displacement=max(pks);
end</pre>Serena Solankihttps://www.mathworks.com/matlabcentral/profile/authors/10046666-serena-solankitag:www.mathworks.com,2005:Question/3837912018-02-20T20:04:42Z2018-02-20T20:04:42ZIs any sample model to simulate multicast and unicast IP network data communications?<p>Assumed I have one unit support multicast or broadcast IPv6 data and anther unit designed to receive the multicast data, is any sample to simulate this data flow process in different data rate?</p>Hong Chenhttps://www.mathworks.com/matlabcentral/profile/authors/11065482-hong-chentag:www.mathworks.com,2005:Question/2899302016-06-15T02:35:58Z2018-02-20T19:56:34ZHow to put toolbar using app designer?<p>How to put the toolbar using app designer as doing in guide?</p>Junghakhttps://www.mathworks.com/matlabcentral/profile/authors/1595092-junghaktag:www.mathworks.com,2005:Question/3835542018-02-19T17:48:14Z2018-02-20T19:51:06Zconvolution of two unit step functions.<p>alright folks, the issue i am having is that i am trying to use convolution on two step functions but for one i have an odd interval that i cannot figure out how to program in matlab. Here is the set up: x[n]= 1 ; 0<=n<=9 otherwise 0, h[n]= 1; 0<=n<=N where N is <= 9 otherwise 0. my problem in i do not know how to express this extra boundary in matlab. here is the code i have written thus far.</p><pre> code
end
nx=-1:9;
N<=9;
nh=0:N;
x=usD(n);
h=usD(n);
y=conv(x,h);
ny=(nx(1) + nh(1) + (0:length(nx) + length(nh) - 2));
plot (ny,y);</pre>justin stephenshttps://www.mathworks.com/matlabcentral/profile/authors/11894331-justin-stephenstag:www.mathworks.com,2005:Question/3835602018-02-19T18:30:35Z2018-02-20T19:50:44ZI want to read in an Excel table with heterogeneous data types<p>I can't find a format example, IO need to read in a data format, some string fields and double precision variables. What does the format option look like? e.g. %d. This is my code so far:</p><pre class="language-matlab">fname='Curves 02.13.18.xls';
T=readtable(fname,'ReadRowNames',true,format,{%d});
Bid = T(:,7);
Ask = T(:,8);
Mid = T(:,9);
Settle = T(1,1);
</pre>Julian Gammon IIIhttps://www.mathworks.com/matlabcentral/profile/authors/11215005-julian-gammon-iiitag:www.mathworks.com,2005:Question/3837772018-02-20T18:55:51Z2018-02-20T19:46:26ZSwitching text from upper to lower case and back in the editor<p>I recently jumped through many Matlab releases. One thing I cannot find in the current Editor is the action to switch text typed in UPPER CASE to lower case or vice versa. It was present in the older version, with shortcuts Ctrl+U and Ctrl+Shift+U.</p><p>Either I cannot find it in the GUI, and it was unjustly removed. In the latter case, please bring it back.</p><p>Thanks,
Naum Derzhi</p>Naum Derzhihttps://www.mathworks.com/matlabcentral/profile/authors/1376471-naum-derzhitag:www.mathworks.com,2005:Question/3837422018-02-20T15:32:15Z2018-02-20T19:46:08Zhow can I change the axes title in plot browser?<p>now it "axes(no title)".
I tried to put:</p><pre class="language-matlab">1)'name','my_name'
2)'title', 'my_title'
</pre><p>thanks</p>Gil Kolinhttps://www.mathworks.com/matlabcentral/profile/authors/11679468-gil-kolin