https://ww2.mathworks.cn/matlabcentral/answers/questionsMATLAB Answers — New Questions2019-12-13T13:18:39Ztag:ww2.mathworks.cn,2005:Question/4957602019-12-09T18:04:03Z2019-12-13T13:18:39Zreplacing zeros with one and storing it in matlab hi i want to display a zero matrix (10x10)
then i want to ask the user to select a specific row and column and then whatever the user selects will turn into 1.
example:
1 2 3 4 5 6 %where 1-6 are columns and A is the name of row
A 0 0 0 0 0 0
user will select: A,4
then the above will change into the following:
1 2 3 4 5 6
A 0 0 0 1 0 0
so that when the user enters again, this will be stored
for example second time user enters will look like this:
user will select: A,2
then the above will change into the following:
1 2 3 4 5 6
A 0 1 0 1 0 0 Elaf Trabulsihttps://ww2.mathworks.cn/matlabcentral/profile/authors/16636513-elaf-trabulsitag:ww2.mathworks.cn,2005:Question/4963732019-12-12T21:24:06Z2019-12-13T13:18:21ZError using fprintf when copying cell content into a text fileI'm trying to copy the content of the following cell into a text file.
I'm using the following code:
[maxrow, maxcolumn]=size(MyCell);
id = fopen('filename.txt','w+t');
if id<0
error('could not open file');
end
for ro =1:maxrow
for co =1:maxcolumn
fprintf(id,'%10s',MyCell{ro,co});
end
fprintf('\n')
end
fclose(id)
But I'm getting this error:
"Error using fprintf
Function is not defined for 'cell' inputs."
Any help?
Thanks!Ahmad Fakihhttps://ww2.mathworks.cn/matlabcentral/profile/authors/12687262-ahmad-fakihtag:ww2.mathworks.cn,2005:Question/1604552014-10-28T18:05:15Z2019-12-13T13:17:50ZHow to export complex-valued matrix to HDF fileHello,
I need to export complex (real,imaginary) data from Matlab using the HDF5 format. I am using the Armadillo C++ template library and I need to duplicate the file structure used by Armadillo.
The HDF5 documentation is not too helpful. It is way too general for me to understand how to simply export complex data. Someone else must surely have done this before.
Below is the output from h5disp so you can see the structure of the files written by Armadillo.
>> h5disp('Example-complex.h5')
HDF5 Example-complex.h5
Group '/'
Dataset 'dataset'
Size: 4x5
MaxSize: 4x5
Datatype: H5T_COMPOUND
Member 'real': H5T_IEEE_F64LE (double)
Member 'imag': H5T_IEEE_F64LE (double)
ChunkSize: []
Filters: none
FillValue: H5T_COMPOUND
Thanks a lot for any pointers.Charleshttps://ww2.mathworks.cn/matlabcentral/profile/authors/387393-charlestag:ww2.mathworks.cn,2005:Question/4964562019-12-13T11:23:58Z2019-12-13T13:17:12ZRename field arrays of a structure that hold identical nameHi there,
I have some previously processed data that is identically named by variable but not by .mat name. I believe it would have been better to assign from the beginning some sort of dinamically generated naming to the output but it didn't happen and now is too late.
After I load the data and organize it into a structure with the following code:
alldat=[];
for i=1:18
alldat=[alldat;load(['CCD_Dyad',num2str(i),'.mat'])];
end
The code at first does what I want, it takes the CCD_Dyad archives with the different numeration (from 1 to 18) and it places them inside a structure as you can see in the following pic.
Now the problem is with the name, as all the fields, filled with 54x54 matrices, have identical names. Now, as they are identically named I cannot use them and perform operations (such as a simple mean between all the matrices) as it reports an error of "Too many input arguments" which makes sense if all the fields are named the same way.
I've been diving into the forums but I do not find any answers fit to my problem. Then I'd like any way to dinamically rename (by number) the fields, or a way to print the name of the file (which contains the number) into the variable, so I can have them differentiated and capable to perform operations with.
Any help would be much appreciated.
Thanks in advanceUnai Vicentehttps://ww2.mathworks.cn/matlabcentral/profile/authors/15885269-unai-vicentetag:ww2.mathworks.cn,2005:Question/4938202019-11-29T04:41:10Z2019-12-13T13:16:01ZHow to use the flags to enable\disable L1 Cache of GPU on Windows?
I have read through GPU books that one can disable L1 cache at compile time adding the flag -Xptxas -dlcm=cg to nvcc for all memory operations using nvcc. Also,the L1 cache can also be explicitly enabled with the flag -Xptxas -dlcm = ca.
I do not know how to write these commands in command prompt on windows.
seham moawedhttps://ww2.mathworks.cn/matlabcentral/profile/authors/16978817-seham-moawedtag:ww2.mathworks.cn,2005:Question/4964312019-12-13T08:31:29Z2019-12-13T13:15:19ZFractional order transfer function
I tried to write a matlab script for the computation of this transfer function but I keep receving an error message saying that the exponent must a scalar integer.
How can I overcome this issue?
Thank you in advance.Abdelkarim Jabranehttps://ww2.mathworks.cn/matlabcentral/profile/authors/6606003-abdelkarim-jabranetag:ww2.mathworks.cn,2005:Question/4964722019-12-13T13:15:09Z2019-12-13T13:15:09ZIs there a difference between fir1 and designfilt with bandpassfir?Dear all,
I want use a fir1 bandpass filter and found both methods, the option with fir1 and the one with designfilt combined with bandpassfir. Is there a marked difference between both options? Which one should I use?
I'm grateful for any clarification.
Thanks,
PeterPeter Bäuerlehttps://ww2.mathworks.cn/matlabcentral/profile/authors/4802817-peter-bauerletag:ww2.mathworks.cn,2005:Question/4964712019-12-13T13:09:00Z2019-12-13T13:09:00ZLQR synthesis with delaysFor example I have a fifth order system with 2 control and with pure time delay on the second actuator. I want to create LQR controller for this system. For time delay I use pade approximation and therefore I got 7th order system and gain matrix from LQR of size [2,7].
The question is how to implement this controller to my system, if I have only 5 states? Will this pipeline work or I need to rely on another approach?
A = [ -0.01193 0.07582 0.08387 0 0;
-0.02857 -23.18 -6.841 0 0;
0.08397 2.998 -38.68 0 0;
0 0 1 0 0;
0 1 0 10 0;]
B = [ 0.0008361 -0.6344;
0 87.97;
0 129.4;
0 0;
0 0];
linsys_with_delay = ss(A,B,eye(5),[],'InputDelay',[0 0.35])
linsys_pade = pade(linsys_with_delay,2)
K_lqr = lqr(linsys_pade.a,linsys_pade.b,eye(7),eye(2))Alexey Mazurhttps://ww2.mathworks.cn/matlabcentral/profile/authors/16010215-alexey-mazurtag:ww2.mathworks.cn,2005:Question/4964692019-12-13T12:41:21Z2019-12-13T13:08:19ZGUIDE - using the data of a Textbox to use it in other functions in the mainI want to use the variable minVelocity in my main. the variable will me entered in the textbox of my gui after I press execute it will be saved.
But I can not use it in my main it does not work.
I get this error message: Undefined function or variable 'minVelocity'.
function varargout = Filtering(varargin)
% Begin initialization code
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Filtering_OpeningFcn, ...
'gui_OutputFcn', @Filtering_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code
% --- Executes just before Filtering is made visible.
function Filtering_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Filtering (see VARARGIN)
% Choose default command line output for Filtering
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Filtering wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Filtering_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% Find the gui using your unique tag
guiHandle = findobj('Type','Figure','Tag','Filtering');
%data = guiHandle.UserData;
guidata(hObject,handles);
function minVelocity_text_Callback(hObject, eventdata, handles)
% hObject handle to minVelocity_text (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function minVelocity_text_CreateFcn(hObject, eventdata, handles)
% hObject handle to minVelocity_text (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in execute_button.
function execute_button_Callback(hObject, eventdata, handles)
minVelocity = str2double(get(handles.minVelocity_text, 'string'));
% Store the data in the "UserData" property of the GUI Figure
% This assumes that the hObject is a child of the GUI figure
data.minVelocity = minVelocity;
hObject.Parent.UserData = data;
disp(minVelocity)
Analysis_Main
--------------------------------------------------------------------
This is a part of my main ( 5 line of 1500)
%minVelocity = 2;
if straightManeuver == true
straight = data(vx > minVelocity, :); %
%extract filtered Data to plot
straightVelocity = straight(:,2);
end
Meganhttps://ww2.mathworks.cn/matlabcentral/profile/authors/16175538-megantag:ww2.mathworks.cn,2005:Question/4773132019-08-23T11:10:34Z2019-12-13T13:07:11Zviewing vectors from a tableHello, My recent results have allowed me to generate a feather graph (found at: https://uk.mathworks.com/matlabcentral/answers/476683-incomplete-graph-from-array )
on reading the manual page I think both U and V are plotted on this graph and I think this has doubled the abcissa range in order to accomidate the vector in both directions. I have attached this array.
Are my assumptions correct and is it possible to view this data in both directions simontaneously since I am plotting a circular event?
I appologise for revisiting this work.
gsGavin Seddonhttps://ww2.mathworks.cn/matlabcentral/profile/authors/6295967-gavin-seddontag:ww2.mathworks.cn,2005:Question/4964642019-12-13T12:28:55Z2019-12-13T13:06:15ZPlotting graph using roots I have calculated the roots of 𝑦 = 𝑥 3 + 5.5𝑥 2 + 3.5𝑥 − 10 (p=[1 5.5 3.5 -10]; >> x=roots(p) x = -4.0000 -2.5000 1.0000) How do I plot this on a single graph (The curve y for −5 ≤ 𝑥 ≤ 3.). Any help will be appreciated:)Mark Davieshttps://ww2.mathworks.cn/matlabcentral/profile/authors/16955237-mark-daviestag:ww2.mathworks.cn,2005:Question/4963672019-12-12T20:39:40Z2019-12-13T13:05:42ZHow to fix this problem?>> sitem
sitem =
1 2 3
>> U
U =
1×2 cell array
[1×2 double] [1×2 double]
U{1}
ans =
1 2
>> U{2}
ans =
2 3
I want to check each value of sitem if it contain in U an put them in F ={}
the output is F = { {1 2}, {{1 2 },{ 2 3 }}, {2 3} }
please help meMira lehttps://ww2.mathworks.cn/matlabcentral/profile/authors/15555924-mira-letag:ww2.mathworks.cn,2005:Question/4963142019-12-12T14:54:21Z2019-12-13T13:01:25ZNon linear fitting with constraintsI have a function that uses 9 inputs (cts(1) to cts (9)) and provides 1 value as result. The function needs 4 parameters (c(1) to c(4)). The code of the function is:
fun = @(c,cts) ((c(1) .* (4-c(4))) ./ (c(2)+c(3) .* 4)) *cts(1,:) +...
((c(1) .* (5-c(4))) ./ (c(2)+c(3) .* 5)) *cts(2,:)+ ...
((c(1) .* (6-c(4))) ./ (c(2)+c(3) .* 6)) *cts(3,:) + ...
((c(1) .* (7-c(4))) ./ (c(2)+c(3) .* 7)) *cts(4,:) + ...
((c(1) .* (8-c(4))) ./ (c(2)+c(3) .* 8)) *cts(5,:) + ...
((c(1) .* (9-c(4))) ./ (c(2)+c(3) .* 9)) *cts(6,:) + ...
((c(1) .* (10-c(4))) ./ (c(2)+c(3) .* 10)) *cts(7,:) + ...
((c(1) .* (11-c(4))) ./ (c(2)+c(3) .* 11)) *cts(8,:) + ...
((c(1) .* (12-c(4))) ./ (c(2)+c(3) .* 12)) *cts(9,:)
I have 11 measured values with each 9 inputs. From this data set I want to fit the parameters c. A condition that applies for only the first row of the input is that
((c(1) .* (4-c(4))) ./ (c(2)+c(3) .* 4)) *cts(1,1) must be greater than ((c(1) .* (5-c(4))) ./ (c(2)+c(3) .* 5)) *cts(2,1). Is there an algorithm which can do that? I already tried lsqcurvefit without this constraint, but the fitted parameters do not follow the condition.
Thank you for your time.Marco Knoblochhttps://ww2.mathworks.cn/matlabcentral/profile/authors/15743339-marco-knoblochtag:ww2.mathworks.cn,2005:Question/4099412018-07-12T02:03:45Z2019-12-13T12:58:09ZHow to add to an image white Gaussian noise of zero mean and standard deviation of certain gray levels? Hello everyone, How can we add white Gaussian noise to an image with zero mean and standard deviation of 64 gray levels? I do know how to add noise of zero mean and variance using imnoise but I do not know about standard deviation of 64 gray levels. Mohsin Shahhttps://ww2.mathworks.cn/matlabcentral/profile/authors/2266137-mohsin-shahtag:ww2.mathworks.cn,2005:Question/4963062019-12-12T14:14:49Z2019-12-13T12:54:17ZHow does ode23 works?Hello,
I want to solve a system of linear equations in matlab. From internet I created a function for my system of equations and in a test script I am applying the ode23 matlab code on that function. In mine opinion it works perfect.
But I really would like to understand how the ode23 code in matlab works together with the function containing the system of equations without giving the arguments to the function.
This is my function:
function [Dv_Div]=ODE_Examples(I, D)
x=D(1);
y=D(2);
z=D(3);
%The system of equations equations
Dv_Div=[x-z;2*x+3*y+z;y+z];
end
This is the script:
domain=[0 10];
InitConditions=[1 2 0];
[IVsol2,DVsol2]=ode23('ODE_Examples', domain, InitConditions);Clarisha Nijmanhttps://ww2.mathworks.cn/matlabcentral/profile/authors/16896806-clarisha-nijmantag:ww2.mathworks.cn,2005:Question/4964702019-12-13T12:46:30Z2019-12-13T12:46:30ZHelp with extract averager, longitude and latitudeHello. Ineed extract from text file below latitude longitude. Example 5657.119,N,02404.880,E. Then i need get average from latitude and logitude and show it in one point in plot and its need to be done in editor. Please help
ACICYhttps://ww2.mathworks.cn/matlabcentral/profile/authors/17088872-acicytag:ww2.mathworks.cn,2005:Question/4957382019-12-09T16:02:39Z2019-12-13T12:44:55ZIn the matlab page of Associated Legendre Polynomials, is the example for the spherical harmonics correct?The '+ve' nodes will always be adjacent to the '-ve' modes in any vibration problem. In the example shown, upper 4 nodes are in one phase while the lower ones are in the opposite phase. chaitanya acharyahttps://ww2.mathworks.cn/matlabcentral/profile/authors/16924792-chaitanya-acharyatag:ww2.mathworks.cn,2005:Question/4962282019-12-12T03:14:49Z2019-12-13T12:43:20ZHow can I calculated centroid contour distance in image at every 10 degree?I I have done many experiments, but I have not been able to determine the angle in the image, I want to get the vertices only at every 10 degrees, but in my experiments, I get the vertices on each pixel contained in the image? please help me solve this problem :). Here is my code
clc;
close all;
clear all;
fontSize = 10;
image_folder = 'D:\Mangrove Classification\Data Training';
filenames = dir(fullfile(image_folder, '*.jpg'));
total_images = numel(filenames);
for n = 1:total_images
full_name= fullfile(image_folder, filenames(n).name);
I = imread(full_name);
I = imresize(I,0.2);
M = I(:,:,1) < 168;
% Fill holes
M = imfill(M, 'holes');
% Take largest blob only.
M = bwareafilt(M, 1);
subplot (1,2,1);
imshow(M);
[labeledImage numberOfBlobs] = bwlabel(M, 8);
blobMeasurements = regionprops(labeledImage, 'Centroid', 'Orientation');
xCenter = blobMeasurements(1).Centroid(1);
yCenter = blobMeasurements(1).Centroid(2);
subplot(1,2,2);
imshow(I);
axis on;
hold on;
% Plot the centroid.
plot(xCenter, yCenter, 'b+', 'MarkerSize', 10, 'LineWidth', 3);
boundaries = bwboundaries(M);
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
for k = 1 : length(boundaries)
thisBoundary = boundaries{k};
plot(thisBoundary(:,2), thisBoundary(:,1), 'b', 'LineWidth', 2);
numberOfBoundaryPoints = length(thisBoundary);
angle = 0: 10 : 360;
for a = 1 : length(angle)
xb = thisBoundary(a,2);
yb = thisBoundary(a,1);
angles(a) = atand((yb-yCenter) / (xb-xCenter));
distances(a) = sqrt((xb-xCenter).^2+(yb-yCenter).^2);
end
end
save('Latih.mat','angles','distances');
endMuhammad Andi Yusranhttps://ww2.mathworks.cn/matlabcentral/profile/authors/15298951-muhammad-andi-yusrantag:ww2.mathworks.cn,2005:Question/4964322019-12-13T08:38:48Z2019-12-13T12:43:15ZHow can i did the coding ?Use Matlab software to draw a graph of the Fourier series approximations over square signal function, x(t), for k=1, k=5, k=25,k=50 and k=100. Use the equations in Fourier series and programming it using matlab script to plot these graphs. (Provide code and plot the graph in microsoft Word) . T= 2pi. Refer to the picture for the equation and graph.
Iffah Farhahhttps://ww2.mathworks.cn/matlabcentral/profile/authors/16769778-iffah-farhahtag:ww2.mathworks.cn,2005:Question/4963352019-12-12T16:11:30Z2019-12-13T12:36:59ZWhy are eigenvector matrices computed by matlab not idempotentAs far as I know, eigenvector matricies should be idempotent. The eigendecompsotion in matlab however, fails to satisfy this.
Here is an example with my comments:
numel=3;
a = rand(numel);a2=a*a';%gen symmetric matrix
[G,L] = eig(a); %eigendecomposiaion
%i know this is what matlab is solving for
norm(a*G-G*L)%and it does, this quantity is close to zero
%eigenvector matricies should be idempotent
norm(G'*G-eye(numel))
norm(G*G'-eye(numel))
%neither of these are close to zero... that is troubling
%Also, if G was idempotent, these would be zero
norm(a-(G*L*G'))
norm(a-(G'*L*G)) %i checked this too b/c i thought i made a mistake with the transpose
%why should this be true?
%AG=GL -- what matlab solves for
%A=GL*inv(G)
%inv(G)=G' for idempotent matrices, so A=GL*G'
Can anyone explain what is going on here? I have an application where I am trying to orthoganalize a set of equations using an eigen decomposition, and the resulting matrix is not exactly diagonal.
ex. in my example, G'*a*G should be diagonal (it should equal L), but it is not
Cheating, and using the inverse, we get that (G\a)*G is 'almost' diagonal
Thanks,
MarcoMarco Sammonhttps://ww2.mathworks.cn/matlabcentral/profile/authors/7286536-marco-sammontag:ww2.mathworks.cn,2005:Question/4963182019-12-12T15:05:33Z2019-12-13T12:35:38ZODE solver - division by zero at time boundariesHi
I have a problem when solving my system of ODEs. I simplified it to what I find essential for the problem.
As I see it, the problem is at t = 0, y(1,2) = 0 , whereas dy(2:end,2) = NaN AND at t = p.t_empty, y(1,1) = 0, whereas dy(2:end,1) = NaN
This is however the time interval I am interested in the solution for. It is essentially a system of two tanks. A reaction occurs in tank 1 and the matter is transferred to tank 2 meanwhile.
Do you have any suggestions to how I can solve this?
p.Q = 40;
p.Q_R = 150;
p.V = 20;
p.n = 101;
p.t_empty = p.V/p.Q;
y0 = zeros(p.n*2+2,1);
y0(1:2) = [p.V, 1];
options = odeset('RelTol',1e-12,'AbsTol',1e-12);
[t,y] = ode45('ODE_tank',[0,p.t_empty],y0,options,p);
figure(1)
plot((0:p.n-1),y(end,2:p.n+1))
figure(2)
plot((0:p.n-1),y(end,p.n+3:end))
function dy = ODE_tank(t,y,options,p)
y = reshape(y,[],2);
dy = zeros(size(y));
dy(1,1) = -p.Q;
dy(2,1) = -p.Q_R/y(1,1)*y(2,1);
dy(3:p.n,1) = p.Q_R/y(1,1)*(y(2:p.n-1,1) - y(3:p.n,1));
dy(p.n+1,1) = p.Q_R/y(1,1)*y(p.n,1);
dy(1,2) = p.Q;
dy(2:end,2) = p.Q/y(1,2)*(y(2:end,1)-y(2:end,2));
dy = reshape(dy,[],1);
endBastian Andersenhttps://ww2.mathworks.cn/matlabcentral/profile/authors/13687642-bastian-andersentag:ww2.mathworks.cn,2005:Question/4964682019-12-13T12:30:33Z2019-12-13T12:30:33ZWhat text encoding is usedHi!
What text encoding is used in Matlab, and has defaults changed recently?
- Editor
- Command window
- Default for fopen(...,'w') ?
Can I change it without creating issues?
Would like to be able to write 'degree C' as '°C' using character #176 , both to Command window and to output files (text files)
Ran into problems when upgrading to Win10 and R2019b.Olofhttps://ww2.mathworks.cn/matlabcentral/profile/authors/3655859-oloftag:ww2.mathworks.cn,2005:Question/4962272019-12-12T02:31:48Z2019-12-13T12:30:10ZHow to plot a system of ODE's using the particular solution Hi I have a system of ODE's which i have obtained the particular solution for is it then possible to plot the individual lines of the ODE's on one plot using this particular solution?Michael Finchhttps://ww2.mathworks.cn/matlabcentral/profile/authors/13985115-michael-finchtag:ww2.mathworks.cn,2005:Question/4964672019-12-13T05:00:00Z2019-12-13T12:30:07Z如何修改 MATLAB 启动时的工作路径？如何修改 MATLAB 启动时的工作路径？
MathWorks Support Teamhttps://ww2.mathworks.cn/matlabcentral/profile/authors/4622813-mathworks-support-teamtag:ww2.mathworks.cn,2005:Question/4964662019-12-13T05:00:00Z2019-12-13T12:30:05Z如何 MATLAB 中用一行指令或函数关闭 windows 操作系统？我有一个程序需要跑很久，希望能在结束程序且保存结果后，自动关闭电脑。
MathWorks Support Teamhttps://ww2.mathworks.cn/matlabcentral/profile/authors/4622813-mathworks-support-teamtag:ww2.mathworks.cn,2005:Question/4964652019-12-13T05:00:00Z2019-12-13T12:30:02Z如何在 LaTeX 中插入 MATLAB 绘制的图片？如何在 LaTeX 中插入 MATLAB 绘制的图片？
MathWorks Support Teamhttps://ww2.mathworks.cn/matlabcentral/profile/authors/4622813-mathworks-support-teamtag:ww2.mathworks.cn,2005:Question/4963512019-12-12T18:01:05Z2019-12-13T12:29:19ZHow do I get a symbolic differentiation of cfit/sfit functions?Hi everyone,
I am trying to get the symbolic partial derivative of a sfit function. Best case would be having the differentition as an sfit function, too.
Thanks in advance!
Tim Baurhttps://ww2.mathworks.cn/matlabcentral/profile/authors/17084738-tim-baurtag:ww2.mathworks.cn,2005:Question/4963522019-12-12T18:33:41Z2019-12-13T12:26:44Zhow can ı use find code in graphf='(x-1)^2*(x-2)*(x-3)';
g='2*(x-1)*(x-2)*(x-3)+(x-1)^2*(x-3)+(x-1)^2*(x-2)';
xx=0:0.01:3.5;
for z=1:length(xx)
ff(z)=feval(inline(f),xx(z));
gg(z)=feval(inline(g),xx(z));
end
a=10^(-10);
figure(1)
plot(xx,ff,'k','linewidth',2);
hold on
plot(xx,gg,'k-.','linewidth',2)
legend('f(x)','g(x)')
axis([min(xx) max(xx) -2 2])
xlabel('x')
plot(z(find(abs(gg)<a),ff(find(abs(gg)<a),'ro','ro'));
ı want to find min and max point in functionMert Inceogluhttps://ww2.mathworks.cn/matlabcentral/profile/authors/17046831-mert-inceoglutag:ww2.mathworks.cn,2005:Question/4964132019-12-13T04:37:22Z2019-12-13T12:21:26Zhow to convert ?3d meshgrid data to 3d matrix.i have a 3 2dimension data.
x(100x100)
y(100x100)
z(100x100)
i want 3 dimension data. like MRI data.
data(100x100x100);
example code
R=5;
r=1;
u=linspace(0,2*pi,100);
v=linspace(0,2*pi,100);
[u,v]=meshgrid(u,v);
x=(R+r.*cos(v)).*cos(u);
y=(R+r.*cos(v)).*sin(u);
z=r.*sin(v);
figure(1)
mesh(x,y,z);
x,y,z to data(100x100x100)
pliz help meDongsu Limhttps://ww2.mathworks.cn/matlabcentral/profile/authors/13877806-dongsu-limtag:ww2.mathworks.cn,2005:Question/4964632019-12-13T12:17:21Z2019-12-13T12:17:21Zidentify point in sector of circle matlab code I have this code
close all
x0 = 2; % origin x-coordinate
y0 = 1; % origin y-coordinate
r = 1; % radius of circle
n = 3; % number of pieces
p = [ 1.5, 2.0, 1.50, 2.4, 1.8, 1.5, 1.7, 2.0, 1.8, 2.5, 2.7, 2.0, 1.3, 1.2, 1.4, 2.2, 1.7, 1.2;
0.2, 0.8, 1.20, 1.0, 1.3, 0.8, 1.1, 1.4, 0.7, 0.6, 0.5, 0.4, 1.1, 1.3, 1.5, 1.8, 1.5, 1.1];
% calculate circle
theta = -pi:0.01:pi;
cirx = r*cos(theta) + x0;
ciry = r*sin(theta) + y0;
% initial plot
figure;
hold on;
axis square;
plot(x0,y0,'or'); % origin
plot(cirx,ciry); % circle
plot(p(1,:),p(2,:),'go'); % predefined points
% calculate and plot separations
ciro = linspace(-pi,pi,n+1);
for k = 1:(numel(ciro))
ph(k) = plot([x0,x0+r*cos(ciro(k))],[y0,y0+r*sin(ciro(k))]);
end
% Compute polar coordinates
[theta, rho] = cart2pol(p(1,:) - x0, p(2,:) - y0);
% Sector theta values with an extra one at the end for periodicity
sector_thetas = linspace(-pi,pi,n+1);
% Identify which sector it falls into by checking theta against one
% sector and then the next. Should be greater than one and less than the other
membership = bsxfun(@le, sector_thetas(1:end-1).', theta) & ...
bsxfun(@gt, sector_thetas(2:end).', theta);
% The result is [nRows x nPoints] and each column will contain a 1 in the row
% corresponding to the sector it belongs to.
% Get the index of which sector it belongs to
% This uses the trick here: http://stackoverflow.com/questions/35950922/finding-the-column-index-for-the-1-in-each-row-of-a-matrix/35951036#35951036
sector = (1:n) * membership;
colors = 'grb';
for k = 1:n
toplot = sector == k;
p(k) = plot(p(1, toplot), p(2, toplot), ...
'Color', colors(k), ...
'Marker', 'o', ...
'DisplayName', sprintf('Sector %d', k));
end
the result is
when I want to change it so the sector start from positive area I cange the radius to negative (r = 1) so the result was
when want to make it in positive part I make radius negative(r=-1) But part of the elements belonging to the same part is present in two different sector how I fix this problem
please Amany hajalihttps://ww2.mathworks.cn/matlabcentral/profile/authors/5447894-amany-hajalitag:ww2.mathworks.cn,2005:Question/4964622019-12-13T12:15:40Z2019-12-13T12:15:40ZHow can I run a Matlab file with MCR (Matlab Compiler Runtime) in a Linux terminal?I am trying to create a CI/CD pipeline with GitLab. In this CI/CD pipeline, matlab unit tests should be executed (class-based unit tests).
For this pipeline I use the Matlab Runtime in a Linux docker. The Matlab Runtime is installed in the docker and the projekt files are also in the docker.
My Question:
How can I run a matlab (*.m) file in this Linux docker with a terminal command?
And is it even possible to run matlab files with the MCR?
Thanks for your helpNils Schellhttps://ww2.mathworks.cn/matlabcentral/profile/authors/16676965-nils-schelltag:ww2.mathworks.cn,2005:Question/4963362019-12-12T16:19:46Z2019-12-13T12:15:27ZFitting data in x,y to a known functionHi everyone. I have a function that is f = 1/(a+b*x) where a and b are the values to obtain and I have some data:
x1 = linspace(1,32,32);
y1 = [0.01 0.02 0.02 0.02 0.02 0.02 0.03 0.03 0.03 0.04 0.04 0.05 0.05 0.06 0.07 0.07 0.08 0.10 0.11 0.14 0.14 0.17 0.17 0.16 0.21 0.31 0.31 2.43 2.43 29.53 29.53 29.53];
I need to fit these two variables x1 and y1 into the function above to obtain a and b. How can I do this?
I'm sorry I'm pretty new in Matlab. Thank you.Raúl Alonso Merinohttps://ww2.mathworks.cn/matlabcentral/profile/authors/14470986-raul-alonso-merinotag:ww2.mathworks.cn,2005:Question/4962342019-12-12T04:23:41Z2019-12-13T12:13:55ZPlot dB/dt vs. time by ODE45 (coupled differential equations)Hi,
I have plotted the solution for two odes against time.
Now, I would like to plot dB/dt vs. time.
Is there any command that can do this by using the code I have written?
My code is in the following
function diffeqs=odes(t,var)
format long
T=var(1);
A=var(2);
B=var(3);
E1=60;
k1=1e-2;
E2=100;
k2=1e-2;
R=8.314;
if t < 20
diffeqs(1,1)=5;%dT/dt
else
diffeqs(1,1)=0;%dT/dt
end
diffeqs(2,1)=-k1*exp(-E1/(R*T))*var(2);%dA/dt
diffeqs(3,1)=k1*exp(-E1/(R*T))*var(2)-k2*exp(-E2/(R*T))*var(3);%dB/dt
end
PLOTTING
clc;close all; clear all;
timerange=[0 100];
ICs= [25,10,0];
[tsol,varsol]=ode45(@(t,var) odes(t,var), timerange, ICs);
figure
yyaxis right
plot(tsol,varsol(:,1),'r','linewidth',2);
xlabel('Time, min');
ylabel('Temperature');
hold on
yyaxis left
plot(tsol,varsol(:,3),'b','linewidth',2);
xlabel('time');
ylabel('B(t)');
grid
legend('B' );
S Hhttps://ww2.mathworks.cn/matlabcentral/profile/authors/12117745-s-htag:ww2.mathworks.cn,2005:Question/4963162019-12-12T14:56:12Z2019-12-13T12:12:00ZConcatinate 4D Time-SignalsHi,
How can i concatinate 20 , 4D signals
I1 = rand(32,32,1,13) ;
I2 = rand(40,32,1,13) ;
...
I20 = rand(47,32,1,13) ;
Catq1=cat(4,I1,I2);
The above give me error? What can i do?ARNhttps://ww2.mathworks.cn/matlabcentral/profile/authors/10580544-arntag:ww2.mathworks.cn,2005:Question/4964612019-12-13T12:11:46Z2019-12-13T12:11:46ZModeling high order systemI have to model a control system where I face several problems with a large number of poles showing up in my transfer functions in matlab. This causes numerical instabilities (I guess...) which make my results hard to trust.
The system to be modelled is depicted here (left picture):
https://www.semanticscholar.org/paper/Comparative-evaluation-of-multi-loop-control-for-a-Cort%C3%A9s-Boillat/16b01bda410d89eff4c83ad1667633b3ad88e8b6/figure/13
The problem deals with a 5th order LC filter and a cascaded current/voltage controller. The PI and P controller are implemented in an FPGA, unfortunately their deadtime is crucial in my application and has to be contained in my model. Furthermore, all of the 4 measurements are modelled by a 1st order low pass filter as well as a measuring delay. The question I have now is: How would you model this in matlab? I would like to optimize the control parameters (P and PI gains) in order to maximize the bandwidth of the converter and plan to do this by iterating over a sets of parameters and qualifying the resulting system behaviour. However, I have difficulties finding a useful implementation in matlab.
First I tried to setup the transfer functions in matlab. I defined s = tf('s') and used this then to go through the system step by step. I defined the impedances (XL1 = s*L1 and so on, used complex voltage divider rules and similar circuit analysis to get the transferfunctions from the filter's input to all of the measured quantities and so on. This is actually working. However, it is a lot of effort to do these things manually, Furthermore, the resulting system turns out to have an order of 80 because I multiply and divide different transfer functions back and forth, thereby accumulating poles everywhere.
The other approach I tried is to express the transfer functions analytically in mathematica, using mason's gain rule. The resulting expressions I copied into matlab (they are huge, filling several screen pages), again I end up having transfer functions of order 100.
Plotting the bode plots of both systems gives identical results for my usefull frequency range (above and below certain frequencies, both plots diverge to strange nonsense values), so I guess what I do would be theoretically correct. I also checked several distinct points from the bodeplots and the step responses against a circuit simulation, the results are matching very well.
The problem is now the high order of the transfer functions and how to work with these functions in matlab. For example how do I judge stability of the system? There is a large number of poles, some of them are very close to instability. Some of them are compensated by according zeros at the same location. Some of these pole/zero pairs should cancel out due to being on the same location, but due to numerical deviations they don't exactly do. I tried using the "minreal" command, it helps but it's not enough. Furthermore, I figured out several cases in which using the "minreal" command severely changed the system dynamics.
An additional problem turns out to be the time delays. I used exp(-s*T) to model them, however, this seems to be a problem with certain matlab functions (e.g the "step" command). I then tried pade approximation, however, this also seems to cause numerical issues and for low orders (less than 3) turns out not to be accurate enough. Since I have 3 controllers and 4 measurements in my final system, approximating all of them by third order pade approximation would imply 21 additional poles in the system (I have no idea whether this would be a problem or not. Is it? It doesn't seem like a good idea to me).
So my question in the end breaks down to this: What is a useful strategy to deal with this? Where to start? What also came into my mind: The controllers are time discrete, I transferred them to the s-domain. I never really worked with z-transformation. Would it be better to us in z-doman and discretizing the time continuous filter into z-domain? I could imagine that modelling the time-delays (which are always integer multiples of the time step) could be easier in z-domain then in s-domain.
J Bhttps://ww2.mathworks.cn/matlabcentral/profile/authors/11796794-j-btag:ww2.mathworks.cn,2005:Question/4724122019-07-18T20:03:39Z2019-12-13T12:03:31ZHow to get time for 50%, 90% and 100% of a graph?Hi guys,
I have another question again. If you run the script which I have attached, you will get a graph and few calculations.
What I want to do is: I need to know after how many seconds the graph reaches 50% and 90% of its maximum.
I have come this far: My idea was it, to take the piece of the curve where the gradient is zero to calculate the average for 100%. From this value I would calculate the 50% and 90%. Because this values are calculated I won't find exactly them, so I'm searching for the next smaller and next bigger value. Now I would need the time, how long it took to get to 50% and to 90%.
I need to do this for 12 graphs to get more or less good average values. Can you please tell me if there is a better/easier way to get the result, or is my way ok? And how would you solve it for several graphs.
Thank you very much!
% Load cell array
load('c_test.mat');
% Take timetable from cell array
P1_100 = c{1}(100:end,1:1);
% Convert timetable to matrix
P1_100_T = timetable2table(P1_100);
P1_100_T = P1_100_T{:,2:2};
% Calculate the average for 100%
P1_100_av = mean(P1_100_T(:,:));
% Calculate 50% and 90%
P1_50 = P1_100_av*0.5;
P1_90 = P1_100_av*0.9;
% Load data for full curve
P1 = c{1}(:,1:1);
% Interpolate timetable and set the starttime at zero
dt = milliseconds(100);
IP1 = retime(P1,'regular','linear','TimeStep', dt);
StartTime = datetime('05.07.2019 00:00.000', 'InputFormat', 'dd.MM.yyyy mm:ss.SSS');
IP1.Properties.StartTime = StartTime;
scatter(IP1.Time, IP1.Volt);
% Convert interpolated timetable to matrix
IPM1 = timetable2table(IP1);
IPM1 = IPM1{:,2:2};
% Search for next smaller and next bigger value of 50%
P1_50_uG = IPM1(find(IPM1 < P1_50, 1, 'last'));
P1_50_oG = IPM1(find(IPM1 > P1_50, 1, 'first'));
% Search for next smaller and next bigger value of 90%
P1_90_uG = IPM1(find(IPM1 < P1_90, 1, 'last'));
P1_90_oG = IPM1(find(IPM1 > P1_90, 1, 'first'));S.Mhttps://ww2.mathworks.cn/matlabcentral/profile/authors/15590066-s-mtag:ww2.mathworks.cn,2005:Question/4962992019-12-12T13:36:27Z2019-12-13T12:01:04ZAnimating the movement of a figure I have written a code which makes makes a rectangle rotate about a certain point bya certain angle anticlockwise, but when I proceed on the animation, it draws every single rectangle as it is rotated. How can I make the rectangle rotate without leaving a trace after every iteration?
Here is the code which inolves some funtions:
function [colouredshape] = filledshape(shape,c)
% This function will draw a shape that is filled of a certain colour
fill(shape(1,:),shape(2,:),c);
end
%%%%%%%%%%%%%%%%%%%
function [finalshape] = rotateabout(shape,a,x,y)
% this function will rotate a shape anticlockwise about a certain point rather than the
% origin (angle a, coordinates (x,y))
[newshape] = translate(-x,-y,shape);
[rotatedshape] = rotate(newshape,a);
[finalshape] = translate(x,y,rotatedshape);
end
% This is animation
n = 10;
rectangle1 = [10 10 11 11 10; 1 5 5 1 1];
for a = linspace(0,pi/2,n)
[finalshape] = rotateabout(rectangle1,a,10,5);
filledshape(finalshape,'b')
drawnow
pause(0.1)
end
Iñigo de la Joyahttps://ww2.mathworks.cn/matlabcentral/profile/authors/16398062-inigo-de-la-joyatag:ww2.mathworks.cn,2005:Question/4816882019-09-23T12:19:03Z2019-12-13T12:00:03ZCalling a GUI button press from test sequence editor I currently have a GUI connected to a Simulink model; I am using the GUI to control the model.
Is it possible use a test sequence block to automatically call the button press functions? If so does anyone know of an example I could see.
Andrewhttps://ww2.mathworks.cn/matlabcentral/profile/authors/15749611-andrewtag:ww2.mathworks.cn,2005:Question/4933912019-11-26T23:11:53Z2019-12-13T11:59:39ZHow to detect the difference between two images with random dot pattern Hi,
I have generated different paterns using silver ink and I want to write the code which can do following things:
1) in attached images you can see that the patterns are very similar yet very different (randomly distributed. I want to use image processing technique which can efficiently detect the difference between two patterns even they are very similar.
2) Also I want to include the function which can discard all the part of patten which is higher or lower thn certain size and only compare the remaining part of the patern with desired size.
I know that the required function may be available in the image processing toolbox. I tried to find it but I got confuse as there were lots of option and I dont have much knowlwdge about which one to use in my case. If you know somthing can be used please providem me the link with the example of that perticular function.
Thank you in advance Devarshi Patelhttps://ww2.mathworks.cn/matlabcentral/profile/authors/13199198-devarshi-pateltag:ww2.mathworks.cn,2005:Question/4964602019-12-13T11:59:27Z2019-12-13T11:59:27ZMeasures to improve computation time with reinforcement learning block in SimulinkI am using the reinforcement learning toolbox to run control tasks, in particular using the DDPG agent. Unfortunately, each episode lasts 100 seconds with a 0.01 s time step (the control time step is 0.1 s, i.e. the RL control block is called that often). The computation time is unfortunately unamangeably high.
I have tried to reduce the training of the actor and critic neural networks to every 5 episodes by using a periodic TargetUpdateMethod and changing the TargetUpdateFrequency. However, by doing a deeper analysis, it is clear that it the computational time taken by each episode, which is too high. So, this is pointing the culpript to the RL Simulink block.
The way I see it, the block should run the neural networks (which is a matrix multiplication) and store the additional experience point in the memory (so some more matrix calculations, if the memory is full). So, this is not fully explaining the large overhead to me.
My code is running (more) efficiently on Python, so it is clear I am not fully exploiting the MATLAB/C++ implementation.
Any advice on how I could try to improve the computational efficiency?Enrico Anderlinihttps://ww2.mathworks.cn/matlabcentral/profile/authors/4704478-enrico-anderlinitag:ww2.mathworks.cn,2005:Question/4962842019-12-12T11:06:30Z2019-12-13T11:53:39ZHow to control GUI with external m-file
I created a GUI, named 'StartEngine', there is a button 'Run'(handles.pushbutton1) on the GUI.
Now, I opened the GUI and I want to make Background color of this button to be gray with a m-file.
It means how could I implement the following command out of StartEngine.m (corresponding to StartEnge.fig)
set(handles.pushbutton1, 'BackgroundColor', [0.5, 0.5, 0.5]);
Leo Zhaihttps://ww2.mathworks.cn/matlabcentral/profile/authors/15002158-leo-zhaitag:ww2.mathworks.cn,2005:Question/4964512019-12-13T11:02:12Z2019-12-13T11:52:28ZA problem related to 'isAlways'
Hi, I am not sure why is the answer not 1.Angus Wonghttps://ww2.mathworks.cn/matlabcentral/profile/authors/11377956-angus-wongtag:ww2.mathworks.cn,2005:Question/4964592019-12-13T11:43:34Z2019-12-13T11:43:34ZIEC 61131-3 Motion Control and PLC CoderI would like to use Simulink, especially Stateflow, to develop the PLC control logic of a system where the final result is to drive a PMSM with a dedicated driver (via CAN). To control the motor, now I'm using the usual Motion Control function blocks defined by the IEC 61131 standard.
How is this implemented with automatic code generation with PLC Coder? It automatically recognize that I want to drive a motor and so it uses the MC function blocks, or I've to "define" this behaviour in some way (eg by creating a "custom function" like some sort of wrap)?
Anyway, my doubt is in general about the use of IEC 61131-3 function block (eg Ton, Toff, etc...) by PLC Coder, if it automatically recognize when to use them.
Thank youandrea nicoterahttps://ww2.mathworks.cn/matlabcentral/profile/authors/6368285-andrea-nicoteratag:ww2.mathworks.cn,2005:Question/4961532019-12-11T16:17:26Z2019-12-13T11:38:13ZNormal components given by surfnorm function do not seem to be the correct onesHi,
I am trying to get the normal components of a surface using the surfnorm function ([U, V, W] = surfnorm(X,Y,Z)) but when I try to plot them using quiver3 (quiver3(X,Y,Z,U,V,W)), I realize that they are not the correct values.
I compare my plot to the one using surfnorm directly to plot (surfnorm(X,Y,Z)).
The full and simple code to show the issue is this:
[X,Y] = meshgrid(-2:0.2:2,-1:0.2:1);
Z = X.* exp(-X.^2 - Y.^2);
figure;
[U,V,W] = surfnorm(X,Y,Z);
surf(X,Y,Z);
hold on
quiver3(X,Y,Z,U,V,W);
figure;
surfnorm(X,Y,Z);
If I zoom in to the surface I can see that in the first plot, the vector coming from the surface are not actually normal to it. The second looks correct, but from this I cannot extract the normal components.
Thanks.
Jose Enrique Almanzahttps://ww2.mathworks.cn/matlabcentral/profile/authors/4641365-jose-enrique-almanzatag:ww2.mathworks.cn,2005:Question/4964582019-12-13T11:32:10Z2019-12-13T11:32:10Zhow to write the mapping of order pair on matlabT(x,y)={2sinx-1,1/3} how to write it on matlab using mann iterationamna bibihttps://ww2.mathworks.cn/matlabcentral/profile/authors/17089328-amna-bibitag:ww2.mathworks.cn,2005:Question/4964572019-12-13T11:30:36Z2019-12-13T11:30:36Zsupervised learning to transform multi-dimensional data to 2D?Hi there, I'm hoping to learn a 'mapping function' to transform a 14 dimentional data to 2 dimentional data
I want to know which matlab function to start with. is it something to do with multi dimention regression?
the input data is a set of 100,000*14 data and it's targeted 100,000*2 (x,y) data. the error function is simply the distance from the targeted points (x,y) Yuchun Dinghttps://ww2.mathworks.cn/matlabcentral/profile/authors/8499366-yuchun-dingtag:ww2.mathworks.cn,2005:Question/4834022019-10-03T11:35:31Z2019-12-13T11:26:10ZSimulink Desktop Real Time still having problemsAll my Simulink models (External mode) report the following error when I try to execute them.
The models use Simulink Desktop Real Time in MATLAB R2019a and Windows 10 (64 bits). The models use a data acquisition board (AD622) from Humusoft.
The Build process it is succesfully as showed in the attached image; but in the next step (loading the model) the error appears
(Previous versions of MATLAB had a similar problem in this step)
Surprisingly, the fist time I executed the models (after installing and configuring all the required components: compiler, kernel, etc.) it works ok. But, after restarting the computer the error message appears. I reinstalled the board drivers and still having the same error.Enrique Pintohttps://ww2.mathworks.cn/matlabcentral/profile/authors/2226039-enrique-pintotag:ww2.mathworks.cn,2005:Question/4964522019-12-13T11:05:43Z2019-12-13T11:21:58ZContrast vs brightness vs gammaPlease help, somebody in here can explain about picture i sent
<</matlabcentral/answers/uploaded_files/254333/images.png>>
Ridho Saragihhttps://ww2.mathworks.cn/matlabcentral/profile/authors/17089355-ridho-saragihtag:ww2.mathworks.cn,2005:Question/4964552019-12-13T11:17:12Z2019-12-13T11:17:12ZExternal voltage reference enabling for LAUNCHXL-F28379D, in Simulink, Embedded Coder Support Package for Texas Instruments C2000 Processors libraryHello everyone,
I'm facing a problem using a Texas device instrument and Simulink. I would like to acquire two channel (two differential voltages), using two 16 bit ADC blocks by the "Embedded Coder Support Package for Texas Instruments C2000 Processors" library.
I would like to change the Voltage High Reference (VREFHI), I saw that for many Texas devices there is the following option "Use External Reference", but for my device (Texas F28379D LaunchXL) not :(.
I was wondering if Have you faced with this problem and how did you solve?
How could I set VREF externally in Simulink?
I saw in device documentation that I should enable the voltage reference option, selecting the bit n 5 in the ADCCTL register (ADCCTL1[ADCREFSEL] bit), but
I don't know how I can change this value in Simulink.
Thanks a lotEdoardo Locorotondohttps://ww2.mathworks.cn/matlabcentral/profile/authors/6575900-edoardo-locorotondotag:ww2.mathworks.cn,2005:Question/4964502019-12-13T10:56:27Z2019-12-13T11:16:43ZF28379D blocksetI am using Matlab 2014 . I am unable to find blockset for F28379D using add on hardware package from matlab. It does a package download which does not have support for the said device. Please clarify or provide direct download linkGaryhttps://ww2.mathworks.cn/matlabcentral/profile/authors/12414499-gary