Category: Matlab
Category Archives: Matlab
Readtable is not reading all of my Data
I am trying to use readtable to read a large excel file (5185 rows) and sometimes this excel file will have columns 21 & 22 empty other then the title up until the 3000+ lines. When this is the case the entire column of 21 & 22 has <missing> as its variable. I assume that after a certain amount of rows the readtable just assumes that the rest of the column is empty. For example, when there are values stored in those columns starting at row 148 it will properly read the data and <missing> does not come up anywhere. Does anyone know a way that can bypass this assumption?
This is the code I am using to read the excel data:
FileName_excel_Results = sprintf(‘SmartChipData_A%06i%s_Results.xlsx’, app.ChipIDEditField.Value, app.dropdown.Value);
fullFileName_excel_Results = fullfile(app.result_filepath, FileName_excel_Results);
t = readtable(fullFileName_excel_Results, ‘Sheet’,’Summary’,’ExpectedNumVariables’,23); % storing the Selected_Spots excel
summary = table2cell(t);
disp(string(summary(3196,:)));
Here is the output I get from the Disp:
Columns 1 through 7
"3195" "45" "28" "45" "C:UsersSingle C…" "C:UsersSingle C…" "[0,0,0]"
Columns 8 through 17
"45/28" "0" "0" "1" "-1" "-1" "-1" "Auto" "A" "i7-28"
Columns 18 through 23
"CGATAACTGGTCTTAAG…" "i5-45" "TAAAGTAGTAAAGTAGG…" <missing> <missing> "47"
This is what is stored in that row from the excel sheet:
3195 45 28 45 C:UsersSingle Cell ScopeDesktopChip143881CyanS0000C45R45_C45_0000_00_Cyan.tif C:UsersSingle Cell ScopeDesktopChip143881GreenS0000C45R45_C45_0000_01_Green.tif [0,0,0] 45/28 0 0 1 -1 -1-1 Auto A i7-28 CGATAACTGGTCTTAAGATGTGAA i5-45 TAAAGTAGTAAAGTAGGATTCTCT C1 1A1 47I am trying to use readtable to read a large excel file (5185 rows) and sometimes this excel file will have columns 21 & 22 empty other then the title up until the 3000+ lines. When this is the case the entire column of 21 & 22 has <missing> as its variable. I assume that after a certain amount of rows the readtable just assumes that the rest of the column is empty. For example, when there are values stored in those columns starting at row 148 it will properly read the data and <missing> does not come up anywhere. Does anyone know a way that can bypass this assumption?
This is the code I am using to read the excel data:
FileName_excel_Results = sprintf(‘SmartChipData_A%06i%s_Results.xlsx’, app.ChipIDEditField.Value, app.dropdown.Value);
fullFileName_excel_Results = fullfile(app.result_filepath, FileName_excel_Results);
t = readtable(fullFileName_excel_Results, ‘Sheet’,’Summary’,’ExpectedNumVariables’,23); % storing the Selected_Spots excel
summary = table2cell(t);
disp(string(summary(3196,:)));
Here is the output I get from the Disp:
Columns 1 through 7
"3195" "45" "28" "45" "C:UsersSingle C…" "C:UsersSingle C…" "[0,0,0]"
Columns 8 through 17
"45/28" "0" "0" "1" "-1" "-1" "-1" "Auto" "A" "i7-28"
Columns 18 through 23
"CGATAACTGGTCTTAAG…" "i5-45" "TAAAGTAGTAAAGTAGG…" <missing> <missing> "47"
This is what is stored in that row from the excel sheet:
3195 45 28 45 C:UsersSingle Cell ScopeDesktopChip143881CyanS0000C45R45_C45_0000_00_Cyan.tif C:UsersSingle Cell ScopeDesktopChip143881GreenS0000C45R45_C45_0000_01_Green.tif [0,0,0] 45/28 0 0 1 -1 -1-1 Auto A i7-28 CGATAACTGGTCTTAAGATGTGAA i5-45 TAAAGTAGTAAAGTAGGATTCTCT C1 1A1 47 I am trying to use readtable to read a large excel file (5185 rows) and sometimes this excel file will have columns 21 & 22 empty other then the title up until the 3000+ lines. When this is the case the entire column of 21 & 22 has <missing> as its variable. I assume that after a certain amount of rows the readtable just assumes that the rest of the column is empty. For example, when there are values stored in those columns starting at row 148 it will properly read the data and <missing> does not come up anywhere. Does anyone know a way that can bypass this assumption?
This is the code I am using to read the excel data:
FileName_excel_Results = sprintf(‘SmartChipData_A%06i%s_Results.xlsx’, app.ChipIDEditField.Value, app.dropdown.Value);
fullFileName_excel_Results = fullfile(app.result_filepath, FileName_excel_Results);
t = readtable(fullFileName_excel_Results, ‘Sheet’,’Summary’,’ExpectedNumVariables’,23); % storing the Selected_Spots excel
summary = table2cell(t);
disp(string(summary(3196,:)));
Here is the output I get from the Disp:
Columns 1 through 7
"3195" "45" "28" "45" "C:UsersSingle C…" "C:UsersSingle C…" "[0,0,0]"
Columns 8 through 17
"45/28" "0" "0" "1" "-1" "-1" "-1" "Auto" "A" "i7-28"
Columns 18 through 23
"CGATAACTGGTCTTAAG…" "i5-45" "TAAAGTAGTAAAGTAGG…" <missing> <missing> "47"
This is what is stored in that row from the excel sheet:
3195 45 28 45 C:UsersSingle Cell ScopeDesktopChip143881CyanS0000C45R45_C45_0000_00_Cyan.tif C:UsersSingle Cell ScopeDesktopChip143881GreenS0000C45R45_C45_0000_01_Green.tif [0,0,0] 45/28 0 0 1 -1 -1-1 Auto A i7-28 CGATAACTGGTCTTAAGATGTGAA i5-45 TAAAGTAGTAAAGTAGGATTCTCT C1 1A1 47 importing excel data, data import MATLAB Answers — New Questions
Obtaining the proportional, derivative and integral gains from a specific settling time and overshoot inputted using a Simulink block diagram
I need to find the PID gains that are obtained from a 10% overshoot and a 10 second settling time from the block diagram system attached. every resource I use online points me to PID Tuner App and it doesn’t seem to have a tool which can set the overshoot and settling time to specific values and I have been running into a lot of issuesI need to find the PID gains that are obtained from a 10% overshoot and a 10 second settling time from the block diagram system attached. every resource I use online points me to PID Tuner App and it doesn’t seem to have a tool which can set the overshoot and settling time to specific values and I have been running into a lot of issues I need to find the PID gains that are obtained from a 10% overshoot and a 10 second settling time from the block diagram system attached. every resource I use online points me to PID Tuner App and it doesn’t seem to have a tool which can set the overshoot and settling time to specific values and I have been running into a lot of issues pid control, simulink MATLAB Answers — New Questions
Having issues writing matrix to a binary file.
For example,
A = [ 0.1712 0.2769 0.8235
0.7060 0.0462 0.6948
0.0318 0.0971 0.3171]
file1 = ‘mat1.dat’
fileID = fopen(file1, ‘w’);
fwrite(fileID, size(data1), ‘int32’);
fwrite(fileID, data1, ‘double’)
fclose("all")
Using this code, the matrix size remains the same, but when I read it, the last row gets messed up, for example, see the output below:
0.0000 0.0318 0.0971
0.1712 0.2769 0.8235
0.7060 0.0462 0.6948
>> Anyone knows how to fix this?For example,
A = [ 0.1712 0.2769 0.8235
0.7060 0.0462 0.6948
0.0318 0.0971 0.3171]
file1 = ‘mat1.dat’
fileID = fopen(file1, ‘w’);
fwrite(fileID, size(data1), ‘int32’);
fwrite(fileID, data1, ‘double’)
fclose("all")
Using this code, the matrix size remains the same, but when I read it, the last row gets messed up, for example, see the output below:
0.0000 0.0318 0.0971
0.1712 0.2769 0.8235
0.7060 0.0462 0.6948
>> Anyone knows how to fix this? For example,
A = [ 0.1712 0.2769 0.8235
0.7060 0.0462 0.6948
0.0318 0.0971 0.3171]
file1 = ‘mat1.dat’
fileID = fopen(file1, ‘w’);
fwrite(fileID, size(data1), ‘int32’);
fwrite(fileID, data1, ‘double’)
fclose("all")
Using this code, the matrix size remains the same, but when I read it, the last row gets messed up, for example, see the output below:
0.0000 0.0318 0.0971
0.1712 0.2769 0.8235
0.7060 0.0462 0.6948
>> Anyone knows how to fix this? binary, matrix, fread, fwrite MATLAB Answers — New Questions
How can I move an object in Simscape Multibody in 6DOF with position inputs?
I have a project which I need to simulate an UAV in Simscape Multibody environment. I used "6-DOF Joint" block for this purpose however,I can use position inputs for only translational motion. For rotational motion there is only one actuation option which is torque. I want to provide the angles directly. Is there a way to do that?I have a project which I need to simulate an UAV in Simscape Multibody environment. I used "6-DOF Joint" block for this purpose however,I can use position inputs for only translational motion. For rotational motion there is only one actuation option which is torque. I want to provide the angles directly. Is there a way to do that? I have a project which I need to simulate an UAV in Simscape Multibody environment. I used "6-DOF Joint" block for this purpose however,I can use position inputs for only translational motion. For rotational motion there is only one actuation option which is torque. I want to provide the angles directly. Is there a way to do that? simscape, multibody MATLAB Answers — New Questions
does anyone have matlab code for routing of underwater wireless sensor networks?
I am finding the codes for any routing protocol of underwater wsn for my m.tech thesis.I am finding the codes for any routing protocol of underwater wsn for my m.tech thesis. I am finding the codes for any routing protocol of underwater wsn for my m.tech thesis. matlab MATLAB Answers — New Questions
I want to graph a voltage and current of each of my elements. please help me
Post Content Post Content electrical networks MATLAB Answers — New Questions
Access to Matlab 2018a version?
Hey guys, I’m trying to run code that someone else wrote on the 2018a version of Matlab. It won’t run on the 2022 version on my PC. Does anyone know of a safe website to download the 2018a version of Matlab? I’m hoping this will solve the problem. Thanks.Hey guys, I’m trying to run code that someone else wrote on the 2018a version of Matlab. It won’t run on the 2022 version on my PC. Does anyone know of a safe website to download the 2018a version of Matlab? I’m hoping this will solve the problem. Thanks. Hey guys, I’m trying to run code that someone else wrote on the 2018a version of Matlab. It won’t run on the 2022 version on my PC. Does anyone know of a safe website to download the 2018a version of Matlab? I’m hoping this will solve the problem. Thanks. matlab 2018a MATLAB Answers — New Questions
what type of error is this? is this the logical error, simulation error, code error?
what type of error is this? is this the logical error, simulation error, code error?what type of error is this? is this the logical error, simulation error, code error? what type of error is this? is this the logical error, simulation error, code error? what type of error is this? is this the logical er MATLAB Answers — New Questions
Can’t identify hadoop home from Matlab to use Matlab Parallel Toolbox.
I have Matlab2014b + Hadoop installed. The hadoop works well on my CentOS Linux system.
I run the mablab on that CentOS Linux machine. With the commands as follows.
It shows the folder is not a valid Hadop no matter how to change it ( I tried set the directory with hadoop 1.2.1, hadoop 2.5.2, hadoop cloudera distribution; also tried with parameter HADOOP_PREFIX,HADOOP_HOME,MATLAB_HADOOP_INSTALL ).
Anything else I can do to fix this error ?
>> setenv(‘MATLAB_HADOOP_INSTALL’,’/mnt/disk1/data/soft/hadoop-2.5.2′);
>> cluster = parallel.cluster.Hadoop;
Error using parallel.cluster.Hadoop (line 107)
Unable to recognize ‘/mnt/disk1/data/soft/hadoop-2.5.2’ as a valid HADOOP
installation folder. Check that this is the root of your HADOOP installation
folder.
Thanks and Best Wishes.
XinsongI have Matlab2014b + Hadoop installed. The hadoop works well on my CentOS Linux system.
I run the mablab on that CentOS Linux machine. With the commands as follows.
It shows the folder is not a valid Hadop no matter how to change it ( I tried set the directory with hadoop 1.2.1, hadoop 2.5.2, hadoop cloudera distribution; also tried with parameter HADOOP_PREFIX,HADOOP_HOME,MATLAB_HADOOP_INSTALL ).
Anything else I can do to fix this error ?
>> setenv(‘MATLAB_HADOOP_INSTALL’,’/mnt/disk1/data/soft/hadoop-2.5.2′);
>> cluster = parallel.cluster.Hadoop;
Error using parallel.cluster.Hadoop (line 107)
Unable to recognize ‘/mnt/disk1/data/soft/hadoop-2.5.2’ as a valid HADOOP
installation folder. Check that this is the root of your HADOOP installation
folder.
Thanks and Best Wishes.
Xinsong I have Matlab2014b + Hadoop installed. The hadoop works well on my CentOS Linux system.
I run the mablab on that CentOS Linux machine. With the commands as follows.
It shows the folder is not a valid Hadop no matter how to change it ( I tried set the directory with hadoop 1.2.1, hadoop 2.5.2, hadoop cloudera distribution; also tried with parameter HADOOP_PREFIX,HADOOP_HOME,MATLAB_HADOOP_INSTALL ).
Anything else I can do to fix this error ?
>> setenv(‘MATLAB_HADOOP_INSTALL’,’/mnt/disk1/data/soft/hadoop-2.5.2′);
>> cluster = parallel.cluster.Hadoop;
Error using parallel.cluster.Hadoop (line 107)
Unable to recognize ‘/mnt/disk1/data/soft/hadoop-2.5.2’ as a valid HADOOP
installation folder. Check that this is the root of your HADOOP installation
folder.
Thanks and Best Wishes.
Xinsong hadoop, parallel computing tool, hadoop_home, linux MATLAB Answers — New Questions
uigetfile Multiselect option not working for single file select
I have been trying to figure out why you can’t select a single file, if you have the ‘MultiSelect’ option set to ‘on’ in the uigetfile command. I need to be able to select ‘one or more’ files. Here is the code:
% Select and Open file
[files, pathname] = uigetfile({‘*.xls*’; ‘*.csv’}, …
‘Select One or More Files’, ‘MultiSelect’, ‘on’);
for filecount = 1:length(files)
filename = convertCharsToStrings(files(filecount));
opts = detectImportOptions(filename, VariableNamingRule="modify");
MDRdata = readtable(filename, opts);
time = datetime(MDRdata.ZuluDate_Time,"InputFormat",’yyyy-MM-dd”T”HH:mm:ss.SSS”Z’);
time.Format = ‘HH:mm:ss.SSS’;
….
I keep getting the following:
"Error using detectImportOptions
Unable to find or open ‘1’. Check the path and filename or file permissions.
Error in ApacheMDRProgram_04162024 (line 12)
opts = detectImportOptions(filename, VariableNamingRule="modify");"
It works fine if you select more than one file.I have been trying to figure out why you can’t select a single file, if you have the ‘MultiSelect’ option set to ‘on’ in the uigetfile command. I need to be able to select ‘one or more’ files. Here is the code:
% Select and Open file
[files, pathname] = uigetfile({‘*.xls*’; ‘*.csv’}, …
‘Select One or More Files’, ‘MultiSelect’, ‘on’);
for filecount = 1:length(files)
filename = convertCharsToStrings(files(filecount));
opts = detectImportOptions(filename, VariableNamingRule="modify");
MDRdata = readtable(filename, opts);
time = datetime(MDRdata.ZuluDate_Time,"InputFormat",’yyyy-MM-dd”T”HH:mm:ss.SSS”Z’);
time.Format = ‘HH:mm:ss.SSS’;
….
I keep getting the following:
"Error using detectImportOptions
Unable to find or open ‘1’. Check the path and filename or file permissions.
Error in ApacheMDRProgram_04162024 (line 12)
opts = detectImportOptions(filename, VariableNamingRule="modify");"
It works fine if you select more than one file. I have been trying to figure out why you can’t select a single file, if you have the ‘MultiSelect’ option set to ‘on’ in the uigetfile command. I need to be able to select ‘one or more’ files. Here is the code:
% Select and Open file
[files, pathname] = uigetfile({‘*.xls*’; ‘*.csv’}, …
‘Select One or More Files’, ‘MultiSelect’, ‘on’);
for filecount = 1:length(files)
filename = convertCharsToStrings(files(filecount));
opts = detectImportOptions(filename, VariableNamingRule="modify");
MDRdata = readtable(filename, opts);
time = datetime(MDRdata.ZuluDate_Time,"InputFormat",’yyyy-MM-dd”T”HH:mm:ss.SSS”Z’);
time.Format = ‘HH:mm:ss.SSS’;
….
I keep getting the following:
"Error using detectImportOptions
Unable to find or open ‘1’. Check the path and filename or file permissions.
Error in ApacheMDRProgram_04162024 (line 12)
opts = detectImportOptions(filename, VariableNamingRule="modify");"
It works fine if you select more than one file. transferred MATLAB Answers — New Questions
Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-15. – can’t find what the problem is
I keep getting this error, but I have no idea what’s causing it.
Using the step button brings up nothing until the very end when it tries to finish.
all of the X’s,Z’s,Y’s within Fd are 15×1 with positive integers
Any ideas as to what’s causing this?
Fd=zeros(cap, cap, cap,term); %here is the original matrix I created cap=15 and term=20
…
for tt=19:-1:1
for i=1:cap
for j=1:cap
for k=1:cap
… %for brevity’s sake I’ve ommitted some stuff here that doesn’t seem to be causing issues
Fd(i,j,k,tt)= b*(…
m1*(…
p(y(j),z1(k))*(n1*Fd(xp(i),zp(j),y(k),tt+1) + (1-n1)*Fd(xpp(i),zpp(j),y(k),tt+1))+…
(1-p(y(j),z1(k)))*(n2*Fd(xp(i),z1(j),yg(k),tt+1) + (1-n2)*Fd(xpp(i),z1(j),ygg(k),tt+1)))+…
(1-m1)*(…
p2(y(j),z2(k))*(n1*Fd(xp(i),zd(j),y(k),tt+1) + (1-n1)*Fd(xpp(i),zdd(j),y(k),tt+1))+…
(1-p2(y(j),z2(k)))*(n2*Fd(xp(i),z2(j),yg(k),tt+1) + (1-n2)*Fd(xpp(i),z2(j),ygg(k),tt+1))…
))+…
(1-b)*( …
m2*(…
p(y1(j),z(k))*(n1*Fd(xp(i),zg(j),y1(k),tt+1) + (1-n1)*Fd(xpp(i) ,zgg(j),y1(k),tt+1))+ …
(1-p(y1(j),z(k)))*(n2*Fd(xp(i),z(j),yp(k),tt+1) + (1-n2)*Fd(xpp(i) ,z(j),ypp(k),tt+1)))+ …
(1-m2)*(p(y2(j),z(k))*(n1*Fd(xp(i),zg(j),y2(k),tt+1) + (1-n1)*Fd(xpp(i),zgg(j),y2(k),tt+1))+…
(1-p(y2(j),z(k)))*(n2*Fd(xp(i),z(j),yd(k),tt+1) + (1-n2)*Fd(xpp(i),z(j),ydd(k),tt+1))));
If more code is required, I can certainly provideI keep getting this error, but I have no idea what’s causing it.
Using the step button brings up nothing until the very end when it tries to finish.
all of the X’s,Z’s,Y’s within Fd are 15×1 with positive integers
Any ideas as to what’s causing this?
Fd=zeros(cap, cap, cap,term); %here is the original matrix I created cap=15 and term=20
…
for tt=19:-1:1
for i=1:cap
for j=1:cap
for k=1:cap
… %for brevity’s sake I’ve ommitted some stuff here that doesn’t seem to be causing issues
Fd(i,j,k,tt)= b*(…
m1*(…
p(y(j),z1(k))*(n1*Fd(xp(i),zp(j),y(k),tt+1) + (1-n1)*Fd(xpp(i),zpp(j),y(k),tt+1))+…
(1-p(y(j),z1(k)))*(n2*Fd(xp(i),z1(j),yg(k),tt+1) + (1-n2)*Fd(xpp(i),z1(j),ygg(k),tt+1)))+…
(1-m1)*(…
p2(y(j),z2(k))*(n1*Fd(xp(i),zd(j),y(k),tt+1) + (1-n1)*Fd(xpp(i),zdd(j),y(k),tt+1))+…
(1-p2(y(j),z2(k)))*(n2*Fd(xp(i),z2(j),yg(k),tt+1) + (1-n2)*Fd(xpp(i),z2(j),ygg(k),tt+1))…
))+…
(1-b)*( …
m2*(…
p(y1(j),z(k))*(n1*Fd(xp(i),zg(j),y1(k),tt+1) + (1-n1)*Fd(xpp(i) ,zgg(j),y1(k),tt+1))+ …
(1-p(y1(j),z(k)))*(n2*Fd(xp(i),z(j),yp(k),tt+1) + (1-n2)*Fd(xpp(i) ,z(j),ypp(k),tt+1)))+ …
(1-m2)*(p(y2(j),z(k))*(n1*Fd(xp(i),zg(j),y2(k),tt+1) + (1-n1)*Fd(xpp(i),zgg(j),y2(k),tt+1))+…
(1-p(y2(j),z(k)))*(n2*Fd(xp(i),z(j),yd(k),tt+1) + (1-n2)*Fd(xpp(i),z(j),ydd(k),tt+1))));
If more code is required, I can certainly provide I keep getting this error, but I have no idea what’s causing it.
Using the step button brings up nothing until the very end when it tries to finish.
all of the X’s,Z’s,Y’s within Fd are 15×1 with positive integers
Any ideas as to what’s causing this?
Fd=zeros(cap, cap, cap,term); %here is the original matrix I created cap=15 and term=20
…
for tt=19:-1:1
for i=1:cap
for j=1:cap
for k=1:cap
… %for brevity’s sake I’ve ommitted some stuff here that doesn’t seem to be causing issues
Fd(i,j,k,tt)= b*(…
m1*(…
p(y(j),z1(k))*(n1*Fd(xp(i),zp(j),y(k),tt+1) + (1-n1)*Fd(xpp(i),zpp(j),y(k),tt+1))+…
(1-p(y(j),z1(k)))*(n2*Fd(xp(i),z1(j),yg(k),tt+1) + (1-n2)*Fd(xpp(i),z1(j),ygg(k),tt+1)))+…
(1-m1)*(…
p2(y(j),z2(k))*(n1*Fd(xp(i),zd(j),y(k),tt+1) + (1-n1)*Fd(xpp(i),zdd(j),y(k),tt+1))+…
(1-p2(y(j),z2(k)))*(n2*Fd(xp(i),z2(j),yg(k),tt+1) + (1-n2)*Fd(xpp(i),z2(j),ygg(k),tt+1))…
))+…
(1-b)*( …
m2*(…
p(y1(j),z(k))*(n1*Fd(xp(i),zg(j),y1(k),tt+1) + (1-n1)*Fd(xpp(i) ,zgg(j),y1(k),tt+1))+ …
(1-p(y1(j),z(k)))*(n2*Fd(xp(i),z(j),yp(k),tt+1) + (1-n2)*Fd(xpp(i) ,z(j),ypp(k),tt+1)))+ …
(1-m2)*(p(y2(j),z(k))*(n1*Fd(xp(i),zg(j),y2(k),tt+1) + (1-n1)*Fd(xpp(i),zgg(j),y2(k),tt+1))+…
(1-p(y2(j),z(k)))*(n2*Fd(xp(i),z(j),yd(k),tt+1) + (1-n2)*Fd(xpp(i),z(j),ydd(k),tt+1))));
If more code is required, I can certainly provide matrix array MATLAB Answers — New Questions
copy subplots within same figure
I am copying subplots within a figure, to show the same data with an expaned time base.
As you can see from the example below, the copied subplots overlap somewhat, and the legends do not get copied. Is there a better way to do this?
t=(0:200)’;
x=[cos(t),sin(t),cos(t/2),sin(t/2),cos(t/4),sin(t/4)];
fig1=figure(1);
ax1(1,1)=subplot(321);
plot(t,x(:,1),’-r’,t,x(:,2),’-g’);
xlabel(‘Time’); ylabel(‘XA’); title(‘A’); legend({‘cos’,’sin’})
ax1(2,1)=subplot(323);
plot(t,x(:,3),’-b’,t,x(:,4),’-c’);
xlabel(‘Time’); ylabel(‘XB’); title(‘B’); legend({‘cos’,’sin’})
ax1(3,1)=subplot(325);
plot(t,x(:,5),’-m’,t,x(:,6),’-y’);
xlabel(‘Time’); ylabel(‘XC’); title(‘C’); legend({‘cos’,’sin’})
% Next: Copy column 1 plots to column 2, and adjust axes.
for i=1:3
axtemp=subplot(3,2,2*i);
ax1(i,2)=copyobj(ax1(i,1),fig1);
ax1(i,2).Position=axtemp.Position;
delete(axtemp)
xlim([0,20])
end
Thank you.I am copying subplots within a figure, to show the same data with an expaned time base.
As you can see from the example below, the copied subplots overlap somewhat, and the legends do not get copied. Is there a better way to do this?
t=(0:200)’;
x=[cos(t),sin(t),cos(t/2),sin(t/2),cos(t/4),sin(t/4)];
fig1=figure(1);
ax1(1,1)=subplot(321);
plot(t,x(:,1),’-r’,t,x(:,2),’-g’);
xlabel(‘Time’); ylabel(‘XA’); title(‘A’); legend({‘cos’,’sin’})
ax1(2,1)=subplot(323);
plot(t,x(:,3),’-b’,t,x(:,4),’-c’);
xlabel(‘Time’); ylabel(‘XB’); title(‘B’); legend({‘cos’,’sin’})
ax1(3,1)=subplot(325);
plot(t,x(:,5),’-m’,t,x(:,6),’-y’);
xlabel(‘Time’); ylabel(‘XC’); title(‘C’); legend({‘cos’,’sin’})
% Next: Copy column 1 plots to column 2, and adjust axes.
for i=1:3
axtemp=subplot(3,2,2*i);
ax1(i,2)=copyobj(ax1(i,1),fig1);
ax1(i,2).Position=axtemp.Position;
delete(axtemp)
xlim([0,20])
end
Thank you. I am copying subplots within a figure, to show the same data with an expaned time base.
As you can see from the example below, the copied subplots overlap somewhat, and the legends do not get copied. Is there a better way to do this?
t=(0:200)’;
x=[cos(t),sin(t),cos(t/2),sin(t/2),cos(t/4),sin(t/4)];
fig1=figure(1);
ax1(1,1)=subplot(321);
plot(t,x(:,1),’-r’,t,x(:,2),’-g’);
xlabel(‘Time’); ylabel(‘XA’); title(‘A’); legend({‘cos’,’sin’})
ax1(2,1)=subplot(323);
plot(t,x(:,3),’-b’,t,x(:,4),’-c’);
xlabel(‘Time’); ylabel(‘XB’); title(‘B’); legend({‘cos’,’sin’})
ax1(3,1)=subplot(325);
plot(t,x(:,5),’-m’,t,x(:,6),’-y’);
xlabel(‘Time’); ylabel(‘XC’); title(‘C’); legend({‘cos’,’sin’})
% Next: Copy column 1 plots to column 2, and adjust axes.
for i=1:3
axtemp=subplot(3,2,2*i);
ax1(i,2)=copyobj(ax1(i,1),fig1);
ax1(i,2).Position=axtemp.Position;
delete(axtemp)
xlim([0,20])
end
Thank you. subplot copyobj MATLAB Answers — New Questions
How to detect (programmatically induced) value changes of uidropdown?
Situation:
I am currently creating a UI with some dropdown menus that are dynamically populated (based on hardware devices that are connected to the PC). I might change the selectable items and the value programmatically without a user action required.
I want to trigger the execution of a function on the change of the value of the dropdown menu.
I am aware of the ValueChangedFcn callback. However this callback is only executed when the change is made by the user via the UI and not by programmatically induced changes to the value, as one can read in the documentation (This callback function executes when the user selects a different option from the drop-down list. It does not execute if the Value property changes programmatically).
What does not work:
Typically I would add an event listener but the property Value is not SetObservable so this does not work.
Subclassing the class to register a custom set method is also not possible cause uidropdown is sealed.
In my case I always know when I change the value programatically and I could trigger the desired function manually but this does not seem right to me because I then need use the ValueChngedFcn for user changes, add the function every time I change the value and/or write a custom function that changes the value and then automatically executes the function.
All of that seems not right because I would really like to bind the triggering of the function to the change of the value itself and not the cause of it.
Am I misusing uidropdown?
Do I misuse the dropdown menu to store a value and should rather have a separate variable for it that will be changed when the user makes a selection in the dropdown menu?
When using a separate variable I need to take special care to properly initalize that variable and the dropdown menu so that it reflects the value in the dropdown manu at all times.
>> What are your ideas to solve this problem in an elegant way?Situation:
I am currently creating a UI with some dropdown menus that are dynamically populated (based on hardware devices that are connected to the PC). I might change the selectable items and the value programmatically without a user action required.
I want to trigger the execution of a function on the change of the value of the dropdown menu.
I am aware of the ValueChangedFcn callback. However this callback is only executed when the change is made by the user via the UI and not by programmatically induced changes to the value, as one can read in the documentation (This callback function executes when the user selects a different option from the drop-down list. It does not execute if the Value property changes programmatically).
What does not work:
Typically I would add an event listener but the property Value is not SetObservable so this does not work.
Subclassing the class to register a custom set method is also not possible cause uidropdown is sealed.
In my case I always know when I change the value programatically and I could trigger the desired function manually but this does not seem right to me because I then need use the ValueChngedFcn for user changes, add the function every time I change the value and/or write a custom function that changes the value and then automatically executes the function.
All of that seems not right because I would really like to bind the triggering of the function to the change of the value itself and not the cause of it.
Am I misusing uidropdown?
Do I misuse the dropdown menu to store a value and should rather have a separate variable for it that will be changed when the user makes a selection in the dropdown menu?
When using a separate variable I need to take special care to properly initalize that variable and the dropdown menu so that it reflects the value in the dropdown manu at all times.
>> What are your ideas to solve this problem in an elegant way? Situation:
I am currently creating a UI with some dropdown menus that are dynamically populated (based on hardware devices that are connected to the PC). I might change the selectable items and the value programmatically without a user action required.
I want to trigger the execution of a function on the change of the value of the dropdown menu.
I am aware of the ValueChangedFcn callback. However this callback is only executed when the change is made by the user via the UI and not by programmatically induced changes to the value, as one can read in the documentation (This callback function executes when the user selects a different option from the drop-down list. It does not execute if the Value property changes programmatically).
What does not work:
Typically I would add an event listener but the property Value is not SetObservable so this does not work.
Subclassing the class to register a custom set method is also not possible cause uidropdown is sealed.
In my case I always know when I change the value programatically and I could trigger the desired function manually but this does not seem right to me because I then need use the ValueChngedFcn for user changes, add the function every time I change the value and/or write a custom function that changes the value and then automatically executes the function.
All of that seems not right because I would really like to bind the triggering of the function to the change of the value itself and not the cause of it.
Am I misusing uidropdown?
Do I misuse the dropdown menu to store a value and should rather have a separate variable for it that will be changed when the user makes a selection in the dropdown menu?
When using a separate variable I need to take special care to properly initalize that variable and the dropdown menu so that it reflects the value in the dropdown manu at all times.
>> What are your ideas to solve this problem in an elegant way? app designer, callback, uidropdown MATLAB Answers — New Questions
New Desktop for MATLAB not working on Ubuntu (Wayland)
On Linux Ubuntu, when I switch to using the New Desktop for MATLAB, I can’t type in the command window or inside a script, and I cannot change any settings in the prefferences window. I wanted to use the dark theme, but the dropdown menus are not functional – if I select something in a dropdown menu, it just closes without updating.
On Ubuntu login, there’s a cogwheel in the bottom right to choose if I wanna start my session using Ubuntu (on Wayland, default) or Ubuntu on Xorg. Wayland is the default windowing system that I’ve been using ever since, but once I tried with Xorg, New Desktop for MATLAB worked as expected. However, I don’t wanna keep using Xorg since a lot of Ubuntu options that I had set in Wayland don’t stick (examples are natural scrolling, disable touchpad while typing, three-finger middle-click). I managed to change the theme in Xorg and switch back to Wayland and the dark theme stayed, but other issues still persist.
Is this a known issue?
More importantly, is there a way to make New Desktop for MATLAB work on default Ubuntu (Wayland), or do I have to use Ubuntu on Xorg?
Version: Ubuntu 22.04.4 LTSOn Linux Ubuntu, when I switch to using the New Desktop for MATLAB, I can’t type in the command window or inside a script, and I cannot change any settings in the prefferences window. I wanted to use the dark theme, but the dropdown menus are not functional – if I select something in a dropdown menu, it just closes without updating.
On Ubuntu login, there’s a cogwheel in the bottom right to choose if I wanna start my session using Ubuntu (on Wayland, default) or Ubuntu on Xorg. Wayland is the default windowing system that I’ve been using ever since, but once I tried with Xorg, New Desktop for MATLAB worked as expected. However, I don’t wanna keep using Xorg since a lot of Ubuntu options that I had set in Wayland don’t stick (examples are natural scrolling, disable touchpad while typing, three-finger middle-click). I managed to change the theme in Xorg and switch back to Wayland and the dark theme stayed, but other issues still persist.
Is this a known issue?
More importantly, is there a way to make New Desktop for MATLAB work on default Ubuntu (Wayland), or do I have to use Ubuntu on Xorg?
Version: Ubuntu 22.04.4 LTS On Linux Ubuntu, when I switch to using the New Desktop for MATLAB, I can’t type in the command window or inside a script, and I cannot change any settings in the prefferences window. I wanted to use the dark theme, but the dropdown menus are not functional – if I select something in a dropdown menu, it just closes without updating.
On Ubuntu login, there’s a cogwheel in the bottom right to choose if I wanna start my session using Ubuntu (on Wayland, default) or Ubuntu on Xorg. Wayland is the default windowing system that I’ve been using ever since, but once I tried with Xorg, New Desktop for MATLAB worked as expected. However, I don’t wanna keep using Xorg since a lot of Ubuntu options that I had set in Wayland don’t stick (examples are natural scrolling, disable touchpad while typing, three-finger middle-click). I managed to change the theme in Xorg and switch back to Wayland and the dark theme stayed, but other issues still persist.
Is this a known issue?
More importantly, is there a way to make New Desktop for MATLAB work on default Ubuntu (Wayland), or do I have to use Ubuntu on Xorg?
Version: Ubuntu 22.04.4 LTS linux, ubuntu MATLAB Answers — New Questions
second trial license won’t load
I have a trial license set to expire in 2 days, so I have received a new trial license for another month. Trying install it says I already have the items so can’t load them. Do I have to wait for the current one to expire? Or do I have to unload the old ones first?I have a trial license set to expire in 2 days, so I have received a new trial license for another month. Trying install it says I already have the items so can’t load them. Do I have to wait for the current one to expire? Or do I have to unload the old ones first? I have a trial license set to expire in 2 days, so I have received a new trial license for another month. Trying install it says I already have the items so can’t load them. Do I have to wait for the current one to expire? Or do I have to unload the old ones first? trial license MATLAB Answers — New Questions
Does struct manipulation work in place when using arrayfun on an array of structs?
I would like to better understand how arrays of structured data are handled when they are manipulated using arrayfun. I’ve created a minimal example below to show the kind of situation I’m looking at. In this example, I’m applying a function to each struct in an array of structs using arrayfun to manipulate the structures by adding an additional field. Does this code work "in place" on the structs in the array, only allocating new memory for the new field? If this will copy the structures (or the entire array) as the fields are added, then it seems better to use a loop than arrayfun. I’d appreciate any insight into how the memory is managed during this kind of operation.
s = struct("x", {1, 2, 3});
s = arrayfun(@update, s);
function s = update(s)
s.y = s.x^2;
endI would like to better understand how arrays of structured data are handled when they are manipulated using arrayfun. I’ve created a minimal example below to show the kind of situation I’m looking at. In this example, I’m applying a function to each struct in an array of structs using arrayfun to manipulate the structures by adding an additional field. Does this code work "in place" on the structs in the array, only allocating new memory for the new field? If this will copy the structures (or the entire array) as the fields are added, then it seems better to use a loop than arrayfun. I’d appreciate any insight into how the memory is managed during this kind of operation.
s = struct("x", {1, 2, 3});
s = arrayfun(@update, s);
function s = update(s)
s.y = s.x^2;
end I would like to better understand how arrays of structured data are handled when they are manipulated using arrayfun. I’ve created a minimal example below to show the kind of situation I’m looking at. In this example, I’m applying a function to each struct in an array of structs using arrayfun to manipulate the structures by adding an additional field. Does this code work "in place" on the structs in the array, only allocating new memory for the new field? If this will copy the structures (or the entire array) as the fields are added, then it seems better to use a loop than arrayfun. I’d appreciate any insight into how the memory is managed during this kind of operation.
s = struct("x", {1, 2, 3});
s = arrayfun(@update, s);
function s = update(s)
s.y = s.x^2;
end struct, arrayfun, struct arrays, memory, speed, performance MATLAB Answers — New Questions
Using fmincon to solve objective function in integral form.
Hello, I’m attempting to solve an optimal control problem using the fmincon function however I have very little experience with it. I’m wondering if there is some fundamental flaw to my code or if there is a more obscure mistake like using the trapz function for integration. My current batch of code is no longer spitting out errors so trying to troubleshoot my way to a solution has slowed down.
The problem is as follows:
And here is the code:
%% Optimization
clear all
close all
clc
%Initialize constants
global U tspan N div
tspan = 10;
div = 10;
N = tspan*div;
control_ic = [linspace(0, 10, 1/5*N), linspace(10, 0, 4/5*N)]; %Control profile guess
%% Routine
u_t = fmincon(@fun, control_ic, [], [],[],[], [], [], [], optimoptions(‘fmincon’, ‘MaxFunctionEvaluations’, 1e5))
function [J] = fun(u) %Objective function
global U tspan div
U = u;
dynamic_ic = [2;2]; %Initial Conditions
[t, X] = ode45(@dynamics, [0 tspan], dynamic_ic); %Find x, xd using u
x = X(:, 1);
xd = X(:, 2);
x_cost = trapz(tspan/length(t), 2*x.^2 + 2*xd.^2); %Calculate objective function
finalx_cost = x(length(x))^2;
control_cost = trapz(div^(-1), .1*u.^2);
J = x_cost + finalx_cost + control_cost;
end
%System Dynamics
function [X] = dynamics(t, x)
global U tspan N
xd = x(2);
vd = -.2*x(2) – 3*x(1) + interp1(linspace(0, tspan, N), U, t);
X = [xd; vd];
endHello, I’m attempting to solve an optimal control problem using the fmincon function however I have very little experience with it. I’m wondering if there is some fundamental flaw to my code or if there is a more obscure mistake like using the trapz function for integration. My current batch of code is no longer spitting out errors so trying to troubleshoot my way to a solution has slowed down.
The problem is as follows:
And here is the code:
%% Optimization
clear all
close all
clc
%Initialize constants
global U tspan N div
tspan = 10;
div = 10;
N = tspan*div;
control_ic = [linspace(0, 10, 1/5*N), linspace(10, 0, 4/5*N)]; %Control profile guess
%% Routine
u_t = fmincon(@fun, control_ic, [], [],[],[], [], [], [], optimoptions(‘fmincon’, ‘MaxFunctionEvaluations’, 1e5))
function [J] = fun(u) %Objective function
global U tspan div
U = u;
dynamic_ic = [2;2]; %Initial Conditions
[t, X] = ode45(@dynamics, [0 tspan], dynamic_ic); %Find x, xd using u
x = X(:, 1);
xd = X(:, 2);
x_cost = trapz(tspan/length(t), 2*x.^2 + 2*xd.^2); %Calculate objective function
finalx_cost = x(length(x))^2;
control_cost = trapz(div^(-1), .1*u.^2);
J = x_cost + finalx_cost + control_cost;
end
%System Dynamics
function [X] = dynamics(t, x)
global U tspan N
xd = x(2);
vd = -.2*x(2) – 3*x(1) + interp1(linspace(0, tspan, N), U, t);
X = [xd; vd];
end Hello, I’m attempting to solve an optimal control problem using the fmincon function however I have very little experience with it. I’m wondering if there is some fundamental flaw to my code or if there is a more obscure mistake like using the trapz function for integration. My current batch of code is no longer spitting out errors so trying to troubleshoot my way to a solution has slowed down.
The problem is as follows:
And here is the code:
%% Optimization
clear all
close all
clc
%Initialize constants
global U tspan N div
tspan = 10;
div = 10;
N = tspan*div;
control_ic = [linspace(0, 10, 1/5*N), linspace(10, 0, 4/5*N)]; %Control profile guess
%% Routine
u_t = fmincon(@fun, control_ic, [], [],[],[], [], [], [], optimoptions(‘fmincon’, ‘MaxFunctionEvaluations’, 1e5))
function [J] = fun(u) %Objective function
global U tspan div
U = u;
dynamic_ic = [2;2]; %Initial Conditions
[t, X] = ode45(@dynamics, [0 tspan], dynamic_ic); %Find x, xd using u
x = X(:, 1);
xd = X(:, 2);
x_cost = trapz(tspan/length(t), 2*x.^2 + 2*xd.^2); %Calculate objective function
finalx_cost = x(length(x))^2;
control_cost = trapz(div^(-1), .1*u.^2);
J = x_cost + finalx_cost + control_cost;
end
%System Dynamics
function [X] = dynamics(t, x)
global U tspan N
xd = x(2);
vd = -.2*x(2) – 3*x(1) + interp1(linspace(0, tspan, N), U, t);
X = [xd; vd];
end optimization, fmincon, ode45 MATLAB Answers — New Questions
Find State Space Representation of Linearized non-linear System to find State Space Representation in Canonical form MATLAB
I have the following non-linear system and accompanying ODE:
I have the following ODE45 solution:
fun = @(t,X)odefun(X,K,C,M,F(t),resSize);
[t_ode,X_answer] = ode45(fun,tspan,X_0);
The input matrices are stiffness K(X), damping C mass M, and force F. resSize is the total number of masses in the system. To find the solution indirectly I introduced a change of variables to reduce the non-linear ODE to a first order linear ODE.
I would like to find the state space (SS) representation of my linearized non-linear system and corresponding ODE. More specifically, I would like to find the SS matrices A,B,C,D. I did not explicitly perform the change of variables. Rather, MATLAB did it for me. The ultimate goal of finding the SS representation (matrices A,B,C,D) is to then be able to find the canonical form of the non-linear ODE. I’m not sure if this can be done with a transfer function estimate of my ODE45 solution or some other MATLAB tool.I have the following non-linear system and accompanying ODE:
I have the following ODE45 solution:
fun = @(t,X)odefun(X,K,C,M,F(t),resSize);
[t_ode,X_answer] = ode45(fun,tspan,X_0);
The input matrices are stiffness K(X), damping C mass M, and force F. resSize is the total number of masses in the system. To find the solution indirectly I introduced a change of variables to reduce the non-linear ODE to a first order linear ODE.
I would like to find the state space (SS) representation of my linearized non-linear system and corresponding ODE. More specifically, I would like to find the SS matrices A,B,C,D. I did not explicitly perform the change of variables. Rather, MATLAB did it for me. The ultimate goal of finding the SS representation (matrices A,B,C,D) is to then be able to find the canonical form of the non-linear ODE. I’m not sure if this can be done with a transfer function estimate of my ODE45 solution or some other MATLAB tool. I have the following non-linear system and accompanying ODE:
I have the following ODE45 solution:
fun = @(t,X)odefun(X,K,C,M,F(t),resSize);
[t_ode,X_answer] = ode45(fun,tspan,X_0);
The input matrices are stiffness K(X), damping C mass M, and force F. resSize is the total number of masses in the system. To find the solution indirectly I introduced a change of variables to reduce the non-linear ODE to a first order linear ODE.
I would like to find the state space (SS) representation of my linearized non-linear system and corresponding ODE. More specifically, I would like to find the SS matrices A,B,C,D. I did not explicitly perform the change of variables. Rather, MATLAB did it for me. The ultimate goal of finding the SS representation (matrices A,B,C,D) is to then be able to find the canonical form of the non-linear ODE. I’m not sure if this can be done with a transfer function estimate of my ODE45 solution or some other MATLAB tool. transfer function, nonlinear, ode45, ode, matlab function, system, model MATLAB Answers — New Questions
How to apply PCA for my dataset
Hi,
I am trying to apply pca on my dataset but I am not able to get bioplot. I just need bioplot for my data. Please help me. I am very new to Matlab.
I have attached by dataset
ThanksHi,
I am trying to apply pca on my dataset but I am not able to get bioplot. I just need bioplot for my data. Please help me. I am very new to Matlab.
I have attached by dataset
Thanks Hi,
I am trying to apply pca on my dataset but I am not able to get bioplot. I just need bioplot for my data. Please help me. I am very new to Matlab.
I have attached by dataset
Thanks pca MATLAB Answers — New Questions
using if else statements to assign a value to a variable based on the value of a specific element in a matrix
I’ve created a matrix and I’m trying to write a for loop and within the for loop, and I have a variable, p, that I want to change based on a specific element within a matrix that changes with every loop
Here’s what I have:
if M(j,k)>0
p(j,k)=0.9;
elseif M(j,k)<0
p(j,k)=0.1;
elseif M(j,k)==0
p(j,k)=0.5;
end
However, when I try to run it, it just creates p as equalling 0.5 and I get an "index in position 2 exceeds array bounds"
Here’s an example of how I’m trying to run it
p(y(j),z1(k))*(n1*Fd(xp(i),zp(j),y(k),tt+1)
So what I’m trying to say here is that if we have value j, modified by y, and value k, modified by z1, looking at that placement in matrix M, if its above 0, then p = 0.9I’ve created a matrix and I’m trying to write a for loop and within the for loop, and I have a variable, p, that I want to change based on a specific element within a matrix that changes with every loop
Here’s what I have:
if M(j,k)>0
p(j,k)=0.9;
elseif M(j,k)<0
p(j,k)=0.1;
elseif M(j,k)==0
p(j,k)=0.5;
end
However, when I try to run it, it just creates p as equalling 0.5 and I get an "index in position 2 exceeds array bounds"
Here’s an example of how I’m trying to run it
p(y(j),z1(k))*(n1*Fd(xp(i),zp(j),y(k),tt+1)
So what I’m trying to say here is that if we have value j, modified by y, and value k, modified by z1, looking at that placement in matrix M, if its above 0, then p = 0.9 I’ve created a matrix and I’m trying to write a for loop and within the for loop, and I have a variable, p, that I want to change based on a specific element within a matrix that changes with every loop
Here’s what I have:
if M(j,k)>0
p(j,k)=0.9;
elseif M(j,k)<0
p(j,k)=0.1;
elseif M(j,k)==0
p(j,k)=0.5;
end
However, when I try to run it, it just creates p as equalling 0.5 and I get an "index in position 2 exceeds array bounds"
Here’s an example of how I’m trying to run it
p(y(j),z1(k))*(n1*Fd(xp(i),zp(j),y(k),tt+1)
So what I’m trying to say here is that if we have value j, modified by y, and value k, modified by z1, looking at that placement in matrix M, if its above 0, then p = 0.9 matrix array, if statement, for loop MATLAB Answers — New Questions