Tag Archives: matlab
how to ask to the user to insert a command while a function is being executing?
In a previous version of my code, I implemented a function which reads data from serial port. While the function was storing and managing data received through the serial port, the user can each time insert a command "stop" which advice the connected device to stop the communication. The code was this:
configureCallback(controller,"byte",1,@readData)
%%
val = input(‘Comando: ‘,’s’);
% command start
writeline(controller, val);
%%
val = input(‘Comando: ‘,’s’);
% command stop
writeline(controller, val);
I’m trying to do the same but with my peripheral connected to my pc through bluetooth low energy.
However, I don’t know how to make the funcion works and the script asking for the command simultaneously, it seems like configureCallback function is not working in this case.
%% write START
val = input(‘Comando: ‘,’s’);
% Convert string to uint8 array
val_bytes = uint8(val);
% command start
write(c_write,val_bytes,’WithoutResponse’);
%% read data
if finished==0
% Set the callback function for the characteristic
c_read.DataAvailableFcn = @bleDataReceived;
end
%% write STOP
val = input(‘Comando: ‘,’s’);
% Convert string to uint8 array
val_bytes = uint8(val);
% command start
write(c_write,val_bytes,’WithoutResponse’);
i wrote this, but the function is continuosly reading after command start is sent without letting me the chance to insert the command stop.In a previous version of my code, I implemented a function which reads data from serial port. While the function was storing and managing data received through the serial port, the user can each time insert a command "stop" which advice the connected device to stop the communication. The code was this:
configureCallback(controller,"byte",1,@readData)
%%
val = input(‘Comando: ‘,’s’);
% command start
writeline(controller, val);
%%
val = input(‘Comando: ‘,’s’);
% command stop
writeline(controller, val);
I’m trying to do the same but with my peripheral connected to my pc through bluetooth low energy.
However, I don’t know how to make the funcion works and the script asking for the command simultaneously, it seems like configureCallback function is not working in this case.
%% write START
val = input(‘Comando: ‘,’s’);
% Convert string to uint8 array
val_bytes = uint8(val);
% command start
write(c_write,val_bytes,’WithoutResponse’);
%% read data
if finished==0
% Set the callback function for the characteristic
c_read.DataAvailableFcn = @bleDataReceived;
end
%% write STOP
val = input(‘Comando: ‘,’s’);
% Convert string to uint8 array
val_bytes = uint8(val);
% command start
write(c_write,val_bytes,’WithoutResponse’);
i wrote this, but the function is continuosly reading after command start is sent without letting me the chance to insert the command stop. In a previous version of my code, I implemented a function which reads data from serial port. While the function was storing and managing data received through the serial port, the user can each time insert a command "stop" which advice the connected device to stop the communication. The code was this:
configureCallback(controller,"byte",1,@readData)
%%
val = input(‘Comando: ‘,’s’);
% command start
writeline(controller, val);
%%
val = input(‘Comando: ‘,’s’);
% command stop
writeline(controller, val);
I’m trying to do the same but with my peripheral connected to my pc through bluetooth low energy.
However, I don’t know how to make the funcion works and the script asking for the command simultaneously, it seems like configureCallback function is not working in this case.
%% write START
val = input(‘Comando: ‘,’s’);
% Convert string to uint8 array
val_bytes = uint8(val);
% command start
write(c_write,val_bytes,’WithoutResponse’);
%% read data
if finished==0
% Set the callback function for the characteristic
c_read.DataAvailableFcn = @bleDataReceived;
end
%% write STOP
val = input(‘Comando: ‘,’s’);
% Convert string to uint8 array
val_bytes = uint8(val);
% command start
write(c_write,val_bytes,’WithoutResponse’);
i wrote this, but the function is continuosly reading after command start is sent without letting me the chance to insert the command stop. matlab, functions, function, callback, ble, bluetooth MATLAB Answers — New Questions
Extract certain values in .mat file
Hi,
I have a mat file with 1250×1 data and I want to extract specific values from the 1250. For example I want to extract every 2nd or every 5th values.
As an example, the mat file contains the following values:
-0.13793 -0.114746 -0.204773 -0.224304 -0.181274 -0.192566 -0.159912 -0.174866 -0.134888 -0.148926
For every 2nd I want to read: -0.114746 -0.224304 -0.192566 -0.174866 -0.148926
For every 5th I want to read: -0.181274 -0.148926
What is the best command to do this? Thank you.Hi,
I have a mat file with 1250×1 data and I want to extract specific values from the 1250. For example I want to extract every 2nd or every 5th values.
As an example, the mat file contains the following values:
-0.13793 -0.114746 -0.204773 -0.224304 -0.181274 -0.192566 -0.159912 -0.174866 -0.134888 -0.148926
For every 2nd I want to read: -0.114746 -0.224304 -0.192566 -0.174866 -0.148926
For every 5th I want to read: -0.181274 -0.148926
What is the best command to do this? Thank you. Hi,
I have a mat file with 1250×1 data and I want to extract specific values from the 1250. For example I want to extract every 2nd or every 5th values.
As an example, the mat file contains the following values:
-0.13793 -0.114746 -0.204773 -0.224304 -0.181274 -0.192566 -0.159912 -0.174866 -0.134888 -0.148926
For every 2nd I want to read: -0.114746 -0.224304 -0.192566 -0.174866 -0.148926
For every 5th I want to read: -0.181274 -0.148926
What is the best command to do this? Thank you. extract data MATLAB Answers — New Questions
Function definitions are not permitted in this context.
help me
Function definitions are not permitted in this context.help me
Function definitions are not permitted in this context. help me
Function definitions are not permitted in this context. matlab, matlab 2014b MATLAB Answers — New Questions
Error when installing NCORR Program in MATLAB for DIC
Hello,
I want to use DIC in MATLAB. I tried to import/install NCORR program to MATLAB but I get these two errors (screeshots below) that I need help resolving.
The .cpp files in my PC (screenshot-top) does not appear the same as in the reference tutorial i was seeing (screenshot-bottom).
Can someone pls tell me how to fix it. Thankyou!Hello,
I want to use DIC in MATLAB. I tried to import/install NCORR program to MATLAB but I get these two errors (screeshots below) that I need help resolving.
The .cpp files in my PC (screenshot-top) does not appear the same as in the reference tutorial i was seeing (screenshot-bottom).
Can someone pls tell me how to fix it. Thankyou! Hello,
I want to use DIC in MATLAB. I tried to import/install NCORR program to MATLAB but I get these two errors (screeshots below) that I need help resolving.
The .cpp files in my PC (screenshot-top) does not appear the same as in the reference tutorial i was seeing (screenshot-bottom).
Can someone pls tell me how to fix it. Thankyou! ncorr, compiler MATLAB Answers — New Questions
Error using (function_name). Too many output arguments.
I’m having some issues with this code:
clear
clc
fluido = "aria"
i=0;
for temp = 15:0.1:1500
i=i+1;
temperatura(i)=temp;
vettore_cp(i)=calcolo_cp(temp, fluido);
end
plot(temperatura,vettore_cp,"b-")
title("grafico del calore specifico in funzione della temperatura")
xlabel("temperatura [°C]")
ylabel("calore specifico [J/kg*K]")
The program should plot a diagram using the function calcolo_cp that calculates the specific heat and uses as inputs the temperature of the fluid and what type of fluid we want to examinate ( in this case "aria", "air" in english).
The function calcolo_cp is in the same folder and seems to work fine, the code of this function is:
function calcolo_cp(T,fluido)
if strcmp(fluido, "aria")
indice_riga=1;
elseif strcmp(fluido, "gas")
indice_riga=2;
else
disp("ERRORE FLUIDO")
end
coeff=[1000.61 0.482759e-1 0.462906e-3 -0.454035e-6 0.129112e-9;
1013.71 0.641882e-1 0.452287e-3 -0.444350e-6 0.125765e-9];
for i = 1:5
elementi_cp(i) = coeff(indice_riga,i)*T^(i-1);
end
cp = sum(elementi_cp)
end
I’m here if you need more information about my code.
Thank you all.I’m having some issues with this code:
clear
clc
fluido = "aria"
i=0;
for temp = 15:0.1:1500
i=i+1;
temperatura(i)=temp;
vettore_cp(i)=calcolo_cp(temp, fluido);
end
plot(temperatura,vettore_cp,"b-")
title("grafico del calore specifico in funzione della temperatura")
xlabel("temperatura [°C]")
ylabel("calore specifico [J/kg*K]")
The program should plot a diagram using the function calcolo_cp that calculates the specific heat and uses as inputs the temperature of the fluid and what type of fluid we want to examinate ( in this case "aria", "air" in english).
The function calcolo_cp is in the same folder and seems to work fine, the code of this function is:
function calcolo_cp(T,fluido)
if strcmp(fluido, "aria")
indice_riga=1;
elseif strcmp(fluido, "gas")
indice_riga=2;
else
disp("ERRORE FLUIDO")
end
coeff=[1000.61 0.482759e-1 0.462906e-3 -0.454035e-6 0.129112e-9;
1013.71 0.641882e-1 0.452287e-3 -0.444350e-6 0.125765e-9];
for i = 1:5
elementi_cp(i) = coeff(indice_riga,i)*T^(i-1);
end
cp = sum(elementi_cp)
end
I’m here if you need more information about my code.
Thank you all. I’m having some issues with this code:
clear
clc
fluido = "aria"
i=0;
for temp = 15:0.1:1500
i=i+1;
temperatura(i)=temp;
vettore_cp(i)=calcolo_cp(temp, fluido);
end
plot(temperatura,vettore_cp,"b-")
title("grafico del calore specifico in funzione della temperatura")
xlabel("temperatura [°C]")
ylabel("calore specifico [J/kg*K]")
The program should plot a diagram using the function calcolo_cp that calculates the specific heat and uses as inputs the temperature of the fluid and what type of fluid we want to examinate ( in this case "aria", "air" in english).
The function calcolo_cp is in the same folder and seems to work fine, the code of this function is:
function calcolo_cp(T,fluido)
if strcmp(fluido, "aria")
indice_riga=1;
elseif strcmp(fluido, "gas")
indice_riga=2;
else
disp("ERRORE FLUIDO")
end
coeff=[1000.61 0.482759e-1 0.462906e-3 -0.454035e-6 0.129112e-9;
1013.71 0.641882e-1 0.452287e-3 -0.444350e-6 0.125765e-9];
for i = 1:5
elementi_cp(i) = coeff(indice_riga,i)*T^(i-1);
end
cp = sum(elementi_cp)
end
I’m here if you need more information about my code.
Thank you all. error function, plot, too many output arguments, too many inputs arguments MATLAB Answers — New Questions
How do I assign frequency values to the obtained fft values without plotting . I need to know the value of the peak at 16Hz for further calculations.
clear all
close all
clc
L=10;
n=1.45;
c=2.9979e8;
dt = 6e-12;
T=10*2*L*n/c;
t = (-T/2/dt:1:T/2/dt)*dt;
Nt=round(T/dt);
fsine = 1e9;
vsine = 1;
phi = vsine*sin(2*pi*fsine*t);
EL1t=1.274e7*exp(1i*phi);
FP=fft(phi);
fs=1/dt/Nt;
Fs=(-1/dt/2:fs:1/dt/2-1);
Z=plot(Fs,fftshift(abs(fft(EL1t))));
I need to know the value at 16GHz in the plot.clear all
close all
clc
L=10;
n=1.45;
c=2.9979e8;
dt = 6e-12;
T=10*2*L*n/c;
t = (-T/2/dt:1:T/2/dt)*dt;
Nt=round(T/dt);
fsine = 1e9;
vsine = 1;
phi = vsine*sin(2*pi*fsine*t);
EL1t=1.274e7*exp(1i*phi);
FP=fft(phi);
fs=1/dt/Nt;
Fs=(-1/dt/2:fs:1/dt/2-1);
Z=plot(Fs,fftshift(abs(fft(EL1t))));
I need to know the value at 16GHz in the plot. clear all
close all
clc
L=10;
n=1.45;
c=2.9979e8;
dt = 6e-12;
T=10*2*L*n/c;
t = (-T/2/dt:1:T/2/dt)*dt;
Nt=round(T/dt);
fsine = 1e9;
vsine = 1;
phi = vsine*sin(2*pi*fsine*t);
EL1t=1.274e7*exp(1i*phi);
FP=fft(phi);
fs=1/dt/Nt;
Fs=(-1/dt/2:fs:1/dt/2-1);
Z=plot(Fs,fftshift(abs(fft(EL1t))));
I need to know the value at 16GHz in the plot. fft, plot MATLAB Answers — New Questions
Using c language to call the dll file generated by matlab, there is an error when running
I use matlab to generate a matlab code dll file (including library files and header files), and then write a c program to call this dll file, but the following error:I use matlab to generate a matlab code dll file (including library files and header files), and then write a c program to call this dll file, but the following error: I use matlab to generate a matlab code dll file (including library files and header files), and then write a c program to call this dll file, but the following error: dll MATLAB Answers — New Questions
What caused this error message?
—————————————————————————————————————
% Define the input signal x[n]
n = 0:49;
x = (-1).^n .* (n >= 0);
% Define the length of the input signal
N = length(x);
% Initialize the output signal y[n]
y = zeros(1, N);
% Define the initial conditions
y_minus_1 = 4;
y_minus_2 = -2;
% Calculate the output y[n] using the difference equation
for n = 1:50
if n == 1
y(n) = x(n) + x(n – 1) – (1/4) * y_minus_1 + (1/8) * y_minus_2;
elseif n == 2
y(n) = x(n) + x(n – 1) – (1/4) * y(n – 1) + (1/8) * y_minus_1;
else
y(n) = x(n) + x(n – 1) – (1/4) * y(n – 1) + (1/8) * y(n – 2);
end
end
% Plot the output signal y[n]
stem(0:N-1, y);
grid on;
xlabel(‘n’);
ylabel(‘y[n]’);
title(‘Output of the System’);
——————————————————————————————————————–
Array index should be positive integer or logical value.
Error: HW2_2_59_c (18th line)
y(n) = x(n) + x(n – 1) – (1/4) * y_minus_1 + (1/8) * y_minus_2;—————————————————————————————————————
% Define the input signal x[n]
n = 0:49;
x = (-1).^n .* (n >= 0);
% Define the length of the input signal
N = length(x);
% Initialize the output signal y[n]
y = zeros(1, N);
% Define the initial conditions
y_minus_1 = 4;
y_minus_2 = -2;
% Calculate the output y[n] using the difference equation
for n = 1:50
if n == 1
y(n) = x(n) + x(n – 1) – (1/4) * y_minus_1 + (1/8) * y_minus_2;
elseif n == 2
y(n) = x(n) + x(n – 1) – (1/4) * y(n – 1) + (1/8) * y_minus_1;
else
y(n) = x(n) + x(n – 1) – (1/4) * y(n – 1) + (1/8) * y(n – 2);
end
end
% Plot the output signal y[n]
stem(0:N-1, y);
grid on;
xlabel(‘n’);
ylabel(‘y[n]’);
title(‘Output of the System’);
——————————————————————————————————————–
Array index should be positive integer or logical value.
Error: HW2_2_59_c (18th line)
y(n) = x(n) + x(n – 1) – (1/4) * y_minus_1 + (1/8) * y_minus_2; —————————————————————————————————————
% Define the input signal x[n]
n = 0:49;
x = (-1).^n .* (n >= 0);
% Define the length of the input signal
N = length(x);
% Initialize the output signal y[n]
y = zeros(1, N);
% Define the initial conditions
y_minus_1 = 4;
y_minus_2 = -2;
% Calculate the output y[n] using the difference equation
for n = 1:50
if n == 1
y(n) = x(n) + x(n – 1) – (1/4) * y_minus_1 + (1/8) * y_minus_2;
elseif n == 2
y(n) = x(n) + x(n – 1) – (1/4) * y(n – 1) + (1/8) * y_minus_1;
else
y(n) = x(n) + x(n – 1) – (1/4) * y(n – 1) + (1/8) * y(n – 2);
end
end
% Plot the output signal y[n]
stem(0:N-1, y);
grid on;
xlabel(‘n’);
ylabel(‘y[n]’);
title(‘Output of the System’);
——————————————————————————————————————–
Array index should be positive integer or logical value.
Error: HW2_2_59_c (18th line)
y(n) = x(n) + x(n – 1) – (1/4) * y_minus_1 + (1/8) * y_minus_2; #difference equation, #index error MATLAB Answers — New Questions
Automatic Nuclei Count tif stacks
I am using this borrowed code to automatically count DAPI stained nuclei:
Hs.dirPath = pwd;
[Hs.dataFiles,Hs.dataNums] = getDataFiles(Hs.dirPath);
[Hs.foundChannels,Hs.fileNums,Hs.imgExts] = getImageFiles(Hs.dirPath);
for i = 1:length(Hs.fileNums)
Hs.fileNum = Hs.fileNums(i);
fileName = [‘dapi’ sprintf(‘%03d’,Hs.fileNum) ‘.tif’];
Hs.DI = readmm(fileName);
Hs.DI = Hs.DI.imagedata;
Hs.DI = scale(max(Hs.DI(:,:,round(linspace(10,size(Hs.DI,10),50))),[],3));
binDapi = Hs.DI>adaptthresh(Hs.DI);
masktmp = imclearborder(binDapi | ~Hs.DI);
masktmp = bwareaopen(masktmp,50);
if any(masktmp(:))
binDapi = masktmp;
end
L = bwlabel(binDapi);
numObjects = max(L,[],1:2);
Hs.currObjs = [];
Hs.allMasks = [];
for j = 1:numObjects
fnumStr = sprintf(‘%03d’,Hs.fileNum);
objMask = L == j;
newObj = improc2.buildImageObject(objMask, fnumStr, Hs.dirPath);
Hs.currObjs = [Hs.currObjs, newObj];
Hs.allMasks = cat(3,Hs.allMasks,objMask);
end
objects = Hs.currObjs;
save(sprintf(‘%s%sdata%03d.mat’,Hs.dirPath,filesep,Hs.fileNum),’objects’);
[Hs.dataFiles,Hs.dataNums] = getDataFiles(Hs.dirPath);
clear objects;
Hs.currObjs = [];
end
It ends up fusing nuclei that are on different planes. In the image shown here (Matlab Output on the left and original on the right)there should be 14 nuclei but 1 and 6, 2 and 5 and 3 and 7 end up being counted as the same.
I have tried changing the threshold etc. but it does not help with the stacks.
Any suggestions to modify the code? Thanks.I am using this borrowed code to automatically count DAPI stained nuclei:
Hs.dirPath = pwd;
[Hs.dataFiles,Hs.dataNums] = getDataFiles(Hs.dirPath);
[Hs.foundChannels,Hs.fileNums,Hs.imgExts] = getImageFiles(Hs.dirPath);
for i = 1:length(Hs.fileNums)
Hs.fileNum = Hs.fileNums(i);
fileName = [‘dapi’ sprintf(‘%03d’,Hs.fileNum) ‘.tif’];
Hs.DI = readmm(fileName);
Hs.DI = Hs.DI.imagedata;
Hs.DI = scale(max(Hs.DI(:,:,round(linspace(10,size(Hs.DI,10),50))),[],3));
binDapi = Hs.DI>adaptthresh(Hs.DI);
masktmp = imclearborder(binDapi | ~Hs.DI);
masktmp = bwareaopen(masktmp,50);
if any(masktmp(:))
binDapi = masktmp;
end
L = bwlabel(binDapi);
numObjects = max(L,[],1:2);
Hs.currObjs = [];
Hs.allMasks = [];
for j = 1:numObjects
fnumStr = sprintf(‘%03d’,Hs.fileNum);
objMask = L == j;
newObj = improc2.buildImageObject(objMask, fnumStr, Hs.dirPath);
Hs.currObjs = [Hs.currObjs, newObj];
Hs.allMasks = cat(3,Hs.allMasks,objMask);
end
objects = Hs.currObjs;
save(sprintf(‘%s%sdata%03d.mat’,Hs.dirPath,filesep,Hs.fileNum),’objects’);
[Hs.dataFiles,Hs.dataNums] = getDataFiles(Hs.dirPath);
clear objects;
Hs.currObjs = [];
end
It ends up fusing nuclei that are on different planes. In the image shown here (Matlab Output on the left and original on the right)there should be 14 nuclei but 1 and 6, 2 and 5 and 3 and 7 end up being counted as the same.
I have tried changing the threshold etc. but it does not help with the stacks.
Any suggestions to modify the code? Thanks. I am using this borrowed code to automatically count DAPI stained nuclei:
Hs.dirPath = pwd;
[Hs.dataFiles,Hs.dataNums] = getDataFiles(Hs.dirPath);
[Hs.foundChannels,Hs.fileNums,Hs.imgExts] = getImageFiles(Hs.dirPath);
for i = 1:length(Hs.fileNums)
Hs.fileNum = Hs.fileNums(i);
fileName = [‘dapi’ sprintf(‘%03d’,Hs.fileNum) ‘.tif’];
Hs.DI = readmm(fileName);
Hs.DI = Hs.DI.imagedata;
Hs.DI = scale(max(Hs.DI(:,:,round(linspace(10,size(Hs.DI,10),50))),[],3));
binDapi = Hs.DI>adaptthresh(Hs.DI);
masktmp = imclearborder(binDapi | ~Hs.DI);
masktmp = bwareaopen(masktmp,50);
if any(masktmp(:))
binDapi = masktmp;
end
L = bwlabel(binDapi);
numObjects = max(L,[],1:2);
Hs.currObjs = [];
Hs.allMasks = [];
for j = 1:numObjects
fnumStr = sprintf(‘%03d’,Hs.fileNum);
objMask = L == j;
newObj = improc2.buildImageObject(objMask, fnumStr, Hs.dirPath);
Hs.currObjs = [Hs.currObjs, newObj];
Hs.allMasks = cat(3,Hs.allMasks,objMask);
end
objects = Hs.currObjs;
save(sprintf(‘%s%sdata%03d.mat’,Hs.dirPath,filesep,Hs.fileNum),’objects’);
[Hs.dataFiles,Hs.dataNums] = getDataFiles(Hs.dirPath);
clear objects;
Hs.currObjs = [];
end
It ends up fusing nuclei that are on different planes. In the image shown here (Matlab Output on the left and original on the right)there should be 14 nuclei but 1 and 6, 2 and 5 and 3 and 7 end up being counted as the same.
I have tried changing the threshold etc. but it does not help with the stacks.
Any suggestions to modify the code? Thanks. image processing, image analysis MATLAB Answers — New Questions
How to use ode15s to solve a stiff ode with mass?
Now I get a matlab code using ode15s to solve a function with singular Mass. My question is:
what does it mean (there is no function called ‘M’ in my dir)
opts = odeset(‘Mass’, ‘M’, ‘MassSingular’, ‘yes’);
and why in the ode function, there is an extra input called ‘flag’ ?
The main code is as following:
tf = 200;
x0 = [0 0 0 0 2500 0 0.8 0 50 50 50 50 5];
opts = odeset(‘Mass’, ‘M’, ‘MassSingular’, ‘yes’);
[t, x] = ode15s(‘ff’, [0 tf], x0,opts);
function xdot=Methanol_Water(t,x)
if isempty(flag),
P=1;
x1=x(1);
x2=x(2);
x3=x(3);
x4=x(4);
x0=x(6);
xB=x(7);
T1=x(9);
T2=x(10);
T3=x(11);
T4=x(12);
TB=x(13);
D1=-4617.8;
C1=13.676;
D2=-5042.6;
C2=13.519;
A=0.85;
B=0.48;
R=10;
V=10;
Di=V/(R+1);
L=V-Di;
y1=exp(A*(1-x1^2)/(1-x1+A*x1/B)^2)*exp(C1+D1/(273.15+T1))*x1/P;
y2=exp(A*(1-x2^2)/(1-x2+A*x2/B)^2)*exp(C1+D1/(273.15+T2))*x2/P;
y3=exp(A*(1-x3^2)/(1-x3+A*x3/B)^2)*exp(C1+D1/(273.15+T3))*x3/P;
y4=exp(A*(1-x4^2)/(1-x4+A*x4/B)^2)*exp(C1+D1/(273.15+T4))*x4/P;
yB=exp(A*(1-xB^2)/(1-xB+A*xB/B)^2)*exp(C1+D1/(273.15+TB))*xB/P;
MH0=100;
MH=20;
xdot(1)=1/MH*(L*(x0-x1)+V*(y2-y1));
xdot(2)=1/MH*(L*(x1-x2)+V*(y3-y2));
xdot(3)=1/MH*(L*(x2-x3)+V*(y4-y3));
xdot(4)=1/MH*(L*(x3-x4)+V*(yB-y4));
xdot(5)=L-V;
xdot(6)=(V/MH0*y1-(L+Di)/MH0*x0);
xdot(7)=(L*(x4-xB)-V*(yB-xB));
xdot(8)=Di;
xdot(9)=P-exp(A*(1-x1^2)/(1-x1+A*x1/B)^2)*exp(C1+D1/(273.15+T1))*x1-exp(A*x1^2/(x1+A*(1-x1)/B)^2)*exp(C2+D2/(273.15+T1))*(1-x1);
xdot(10)=P-exp(A*(1-x2^2)/(1-x2+A*x2/B)^2)*exp(C1+D1/(273.15+T2))*x2- exp(A*x2^2/(x2+A*(1-x2)/B)^2)*exp(C2+D2/(273.15+T2))*(1-x2);
xdot(11)=P-exp(A*(1-x3^2)/(1-x3+A*x3/B)^2)*exp(C1+D1/(273.15+T3))*x3-exp(A*x3^2/(x3+A*(1-x3)/B)^2)*exp(C2+D2/(273.15+T3))*(1-x3);
xdot(12)=P-exp(A*(1-x4^2)/(1-x4+A*x4/B)^2)*exp(C1+D1/(273.15+T4))*x4- exp(A*x4^2/(x4+A*(1-x4)/B)^2)*exp(C2+D2/(273.15+T4))*(1-x4);
xdot(13)=P-exp(A*(1-xB^2)/(1-xB+A*xB/B)^2)*exp(C1+D1/(273.15+TB))*xB- exp(A*xB^2/(xB+A*(1-xB)/B)^2)*exp(C2+D2/(273.15+TB))*(1-xB);
xdot = xdot(:);
else
M = zeros(13,13);
M(1,1) = 1;
M(2,2) = 1;
M(3,3) = 1;
M(4,4) = 1;
M(5,5) = 1;
M(6,6) = 1;
M(7,7) = x(5);
M(8,8) = 1;
xdot = M;
endNow I get a matlab code using ode15s to solve a function with singular Mass. My question is:
what does it mean (there is no function called ‘M’ in my dir)
opts = odeset(‘Mass’, ‘M’, ‘MassSingular’, ‘yes’);
and why in the ode function, there is an extra input called ‘flag’ ?
The main code is as following:
tf = 200;
x0 = [0 0 0 0 2500 0 0.8 0 50 50 50 50 5];
opts = odeset(‘Mass’, ‘M’, ‘MassSingular’, ‘yes’);
[t, x] = ode15s(‘ff’, [0 tf], x0,opts);
function xdot=Methanol_Water(t,x)
if isempty(flag),
P=1;
x1=x(1);
x2=x(2);
x3=x(3);
x4=x(4);
x0=x(6);
xB=x(7);
T1=x(9);
T2=x(10);
T3=x(11);
T4=x(12);
TB=x(13);
D1=-4617.8;
C1=13.676;
D2=-5042.6;
C2=13.519;
A=0.85;
B=0.48;
R=10;
V=10;
Di=V/(R+1);
L=V-Di;
y1=exp(A*(1-x1^2)/(1-x1+A*x1/B)^2)*exp(C1+D1/(273.15+T1))*x1/P;
y2=exp(A*(1-x2^2)/(1-x2+A*x2/B)^2)*exp(C1+D1/(273.15+T2))*x2/P;
y3=exp(A*(1-x3^2)/(1-x3+A*x3/B)^2)*exp(C1+D1/(273.15+T3))*x3/P;
y4=exp(A*(1-x4^2)/(1-x4+A*x4/B)^2)*exp(C1+D1/(273.15+T4))*x4/P;
yB=exp(A*(1-xB^2)/(1-xB+A*xB/B)^2)*exp(C1+D1/(273.15+TB))*xB/P;
MH0=100;
MH=20;
xdot(1)=1/MH*(L*(x0-x1)+V*(y2-y1));
xdot(2)=1/MH*(L*(x1-x2)+V*(y3-y2));
xdot(3)=1/MH*(L*(x2-x3)+V*(y4-y3));
xdot(4)=1/MH*(L*(x3-x4)+V*(yB-y4));
xdot(5)=L-V;
xdot(6)=(V/MH0*y1-(L+Di)/MH0*x0);
xdot(7)=(L*(x4-xB)-V*(yB-xB));
xdot(8)=Di;
xdot(9)=P-exp(A*(1-x1^2)/(1-x1+A*x1/B)^2)*exp(C1+D1/(273.15+T1))*x1-exp(A*x1^2/(x1+A*(1-x1)/B)^2)*exp(C2+D2/(273.15+T1))*(1-x1);
xdot(10)=P-exp(A*(1-x2^2)/(1-x2+A*x2/B)^2)*exp(C1+D1/(273.15+T2))*x2- exp(A*x2^2/(x2+A*(1-x2)/B)^2)*exp(C2+D2/(273.15+T2))*(1-x2);
xdot(11)=P-exp(A*(1-x3^2)/(1-x3+A*x3/B)^2)*exp(C1+D1/(273.15+T3))*x3-exp(A*x3^2/(x3+A*(1-x3)/B)^2)*exp(C2+D2/(273.15+T3))*(1-x3);
xdot(12)=P-exp(A*(1-x4^2)/(1-x4+A*x4/B)^2)*exp(C1+D1/(273.15+T4))*x4- exp(A*x4^2/(x4+A*(1-x4)/B)^2)*exp(C2+D2/(273.15+T4))*(1-x4);
xdot(13)=P-exp(A*(1-xB^2)/(1-xB+A*xB/B)^2)*exp(C1+D1/(273.15+TB))*xB- exp(A*xB^2/(xB+A*(1-xB)/B)^2)*exp(C2+D2/(273.15+TB))*(1-xB);
xdot = xdot(:);
else
M = zeros(13,13);
M(1,1) = 1;
M(2,2) = 1;
M(3,3) = 1;
M(4,4) = 1;
M(5,5) = 1;
M(6,6) = 1;
M(7,7) = x(5);
M(8,8) = 1;
xdot = M;
end Now I get a matlab code using ode15s to solve a function with singular Mass. My question is:
what does it mean (there is no function called ‘M’ in my dir)
opts = odeset(‘Mass’, ‘M’, ‘MassSingular’, ‘yes’);
and why in the ode function, there is an extra input called ‘flag’ ?
The main code is as following:
tf = 200;
x0 = [0 0 0 0 2500 0 0.8 0 50 50 50 50 5];
opts = odeset(‘Mass’, ‘M’, ‘MassSingular’, ‘yes’);
[t, x] = ode15s(‘ff’, [0 tf], x0,opts);
function xdot=Methanol_Water(t,x)
if isempty(flag),
P=1;
x1=x(1);
x2=x(2);
x3=x(3);
x4=x(4);
x0=x(6);
xB=x(7);
T1=x(9);
T2=x(10);
T3=x(11);
T4=x(12);
TB=x(13);
D1=-4617.8;
C1=13.676;
D2=-5042.6;
C2=13.519;
A=0.85;
B=0.48;
R=10;
V=10;
Di=V/(R+1);
L=V-Di;
y1=exp(A*(1-x1^2)/(1-x1+A*x1/B)^2)*exp(C1+D1/(273.15+T1))*x1/P;
y2=exp(A*(1-x2^2)/(1-x2+A*x2/B)^2)*exp(C1+D1/(273.15+T2))*x2/P;
y3=exp(A*(1-x3^2)/(1-x3+A*x3/B)^2)*exp(C1+D1/(273.15+T3))*x3/P;
y4=exp(A*(1-x4^2)/(1-x4+A*x4/B)^2)*exp(C1+D1/(273.15+T4))*x4/P;
yB=exp(A*(1-xB^2)/(1-xB+A*xB/B)^2)*exp(C1+D1/(273.15+TB))*xB/P;
MH0=100;
MH=20;
xdot(1)=1/MH*(L*(x0-x1)+V*(y2-y1));
xdot(2)=1/MH*(L*(x1-x2)+V*(y3-y2));
xdot(3)=1/MH*(L*(x2-x3)+V*(y4-y3));
xdot(4)=1/MH*(L*(x3-x4)+V*(yB-y4));
xdot(5)=L-V;
xdot(6)=(V/MH0*y1-(L+Di)/MH0*x0);
xdot(7)=(L*(x4-xB)-V*(yB-xB));
xdot(8)=Di;
xdot(9)=P-exp(A*(1-x1^2)/(1-x1+A*x1/B)^2)*exp(C1+D1/(273.15+T1))*x1-exp(A*x1^2/(x1+A*(1-x1)/B)^2)*exp(C2+D2/(273.15+T1))*(1-x1);
xdot(10)=P-exp(A*(1-x2^2)/(1-x2+A*x2/B)^2)*exp(C1+D1/(273.15+T2))*x2- exp(A*x2^2/(x2+A*(1-x2)/B)^2)*exp(C2+D2/(273.15+T2))*(1-x2);
xdot(11)=P-exp(A*(1-x3^2)/(1-x3+A*x3/B)^2)*exp(C1+D1/(273.15+T3))*x3-exp(A*x3^2/(x3+A*(1-x3)/B)^2)*exp(C2+D2/(273.15+T3))*(1-x3);
xdot(12)=P-exp(A*(1-x4^2)/(1-x4+A*x4/B)^2)*exp(C1+D1/(273.15+T4))*x4- exp(A*x4^2/(x4+A*(1-x4)/B)^2)*exp(C2+D2/(273.15+T4))*(1-x4);
xdot(13)=P-exp(A*(1-xB^2)/(1-xB+A*xB/B)^2)*exp(C1+D1/(273.15+TB))*xB- exp(A*xB^2/(xB+A*(1-xB)/B)^2)*exp(C2+D2/(273.15+TB))*(1-xB);
xdot = xdot(:);
else
M = zeros(13,13);
M(1,1) = 1;
M(2,2) = 1;
M(3,3) = 1;
M(4,4) = 1;
M(5,5) = 1;
M(6,6) = 1;
M(7,7) = x(5);
M(8,8) = 1;
xdot = M;
end ode15s, mass MATLAB Answers — New Questions
Taylor diagram plot method
Hello,
I have to plot taylor diagram in order to compare the results of three models to observations. Could anyone help me how to proceed step by step because I found some solutions in the net but I didn’t understand how it works.
Thanks in advanceHello,
I have to plot taylor diagram in order to compare the results of three models to observations. Could anyone help me how to proceed step by step because I found some solutions in the net but I didn’t understand how it works.
Thanks in advance Hello,
I have to plot taylor diagram in order to compare the results of three models to observations. Could anyone help me how to proceed step by step because I found some solutions in the net but I didn’t understand how it works.
Thanks in advance taylor diagram, comparison, models MATLAB Answers — New Questions
Performance of SystemObject vs Class
Lately, I was introduced to SystemObjects and some of its benefits, for example that one can use them in Matlab as well as in Simulink which is important to me. So I started playing around with SystemObjects and quickly ended up in a scenario where I have a main SystemObject in which I create instances of other SystemObjects which themselves create instances of other SystemObjects. So to say: nested SystemObjects.
Now, for my understanding: If I want to use the main SystemObject only in both Matlab and Simulink then I could replace the other SystemObjects with normal classes. And here comes the question: Would that increase the performance of the generated code?
The reason why I am wondering is the fact that in languages like C++ inheritance from classes with virtual methods cause runtime costs (at least that’s what I understood). I guess in the case of SystemObjects which inherit from the matlab.System class something similar will happen?Lately, I was introduced to SystemObjects and some of its benefits, for example that one can use them in Matlab as well as in Simulink which is important to me. So I started playing around with SystemObjects and quickly ended up in a scenario where I have a main SystemObject in which I create instances of other SystemObjects which themselves create instances of other SystemObjects. So to say: nested SystemObjects.
Now, for my understanding: If I want to use the main SystemObject only in both Matlab and Simulink then I could replace the other SystemObjects with normal classes. And here comes the question: Would that increase the performance of the generated code?
The reason why I am wondering is the fact that in languages like C++ inheritance from classes with virtual methods cause runtime costs (at least that’s what I understood). I guess in the case of SystemObjects which inherit from the matlab.System class something similar will happen? Lately, I was introduced to SystemObjects and some of its benefits, for example that one can use them in Matlab as well as in Simulink which is important to me. So I started playing around with SystemObjects and quickly ended up in a scenario where I have a main SystemObject in which I create instances of other SystemObjects which themselves create instances of other SystemObjects. So to say: nested SystemObjects.
Now, for my understanding: If I want to use the main SystemObject only in both Matlab and Simulink then I could replace the other SystemObjects with normal classes. And here comes the question: Would that increase the performance of the generated code?
The reason why I am wondering is the fact that in languages like C++ inheritance from classes with virtual methods cause runtime costs (at least that’s what I understood). I guess in the case of SystemObjects which inherit from the matlab.System class something similar will happen? performance, systemobject, class, inheritance, matlab.system, code generation MATLAB Answers — New Questions
How to check mixer upconverted output at 2.4GHz?
I am using mixer to upconvert baseband signal of 1-2 MHz to 2.4GHz.
I want to verify this upconverted signal before transmission.
I used ps to simulink block but with that it shows baseband signal only. I would like to know how can I verify that the signal is upconverted to passband signal?I am using mixer to upconvert baseband signal of 1-2 MHz to 2.4GHz.
I want to verify this upconverted signal before transmission.
I used ps to simulink block but with that it shows baseband signal only. I would like to know how can I verify that the signal is upconverted to passband signal? I am using mixer to upconvert baseband signal of 1-2 MHz to 2.4GHz.
I want to verify this upconverted signal before transmission.
I used ps to simulink block but with that it shows baseband signal only. I would like to know how can I verify that the signal is upconverted to passband signal? baseband to passband conversion, mixer, rf MATLAB Answers — New Questions
automation of code for many variables
לק"י
Hi guys,
I got alot of variables due to high amount of samples to analyze. I import csv data from the import tool and then process each variable that was imported.
I want to know, what is the recommended way to run code on several variables and then save the outputs in an ordered way? can you refer me to a page or video that can explain it?
tanks very much!
Amit.לק"י
Hi guys,
I got alot of variables due to high amount of samples to analyze. I import csv data from the import tool and then process each variable that was imported.
I want to know, what is the recommended way to run code on several variables and then save the outputs in an ordered way? can you refer me to a page or video that can explain it?
tanks very much!
Amit. לק"י
Hi guys,
I got alot of variables due to high amount of samples to analyze. I import csv data from the import tool and then process each variable that was imported.
I want to know, what is the recommended way to run code on several variables and then save the outputs in an ordered way? can you refer me to a page or video that can explain it?
tanks very much!
Amit. automation of code, high throughput analysis, automation MATLAB Answers — New Questions
Scatter plot to contour plot
Can you help me using this function https://www.mathworks.com/matlabcentral/fileexchange/38858-contour-plot-for-scattered-data with the data attached?
I want to go from a scatter plot (below) to a contour plot.Can you help me using this function https://www.mathworks.com/matlabcentral/fileexchange/38858-contour-plot-for-scattered-data with the data attached?
I want to go from a scatter plot (below) to a contour plot. Can you help me using this function https://www.mathworks.com/matlabcentral/fileexchange/38858-contour-plot-for-scattered-data with the data attached?
I want to go from a scatter plot (below) to a contour plot. contour MATLAB Answers — New Questions
Vectorize nested for loops with indices and subscripts
How can I vectorize these nested loops?
A=[1 2 0 2;2 1 3 0;0 3 1 3;2 0 3 1];
matArray=unifrnd(-1,1,[4,4,10]);
m=size(matArray,3);
n=size(unique(A),1)-1;
out=zeros(m,n);
for i = 1 : m
mat=matArray(:,:,i);
for j = 1 : n
out(i,j) = sum(mat(A(:)==j));
end
end
Imo, the main challenge seems to be that if I vectorize i, then I don’t seem how to combine indices and subscripts for mat(A(:)==j) to work. I guess one option could be to arrange m copies of A in the same shape as matArray but I am not sure whether that is the best approach.How can I vectorize these nested loops?
A=[1 2 0 2;2 1 3 0;0 3 1 3;2 0 3 1];
matArray=unifrnd(-1,1,[4,4,10]);
m=size(matArray,3);
n=size(unique(A),1)-1;
out=zeros(m,n);
for i = 1 : m
mat=matArray(:,:,i);
for j = 1 : n
out(i,j) = sum(mat(A(:)==j));
end
end
Imo, the main challenge seems to be that if I vectorize i, then I don’t seem how to combine indices and subscripts for mat(A(:)==j) to work. I guess one option could be to arrange m copies of A in the same shape as matArray but I am not sure whether that is the best approach. How can I vectorize these nested loops?
A=[1 2 0 2;2 1 3 0;0 3 1 3;2 0 3 1];
matArray=unifrnd(-1,1,[4,4,10]);
m=size(matArray,3);
n=size(unique(A),1)-1;
out=zeros(m,n);
for i = 1 : m
mat=matArray(:,:,i);
for j = 1 : n
out(i,j) = sum(mat(A(:)==j));
end
end
Imo, the main challenge seems to be that if I vectorize i, then I don’t seem how to combine indices and subscripts for mat(A(:)==j) to work. I guess one option could be to arrange m copies of A in the same shape as matArray but I am not sure whether that is the best approach. vectorization, indexing MATLAB Answers — New Questions
Visualization of decision tree
I have a table in which all of its columns are categorical variables, and the label associated with them is also a categorical variable. I use this piece of code to fit a tree to the data:
Mdld = fitctree(X,Ycat,’MaxNumSplits’,5,’PredictorSelection’,’curvature’,…
‘CrossVal’,’on’);
when I plot the tree, the variables will be converted to a ordinal variable which is hard to track back what is the original variables that I had in table X. You can see the results here:
view(Mdld.Trained{1},’Mode’,’graph’)
As you see, the labels are correct, but for example, in the X table, I have "Small" and "Large" categories for vessel radius, but I get 1 and 2 labels instead, for which I event do not know readily which one is which!
I could not find a way to keep the labels on the node. Can anyone help me to do so?I have a table in which all of its columns are categorical variables, and the label associated with them is also a categorical variable. I use this piece of code to fit a tree to the data:
Mdld = fitctree(X,Ycat,’MaxNumSplits’,5,’PredictorSelection’,’curvature’,…
‘CrossVal’,’on’);
when I plot the tree, the variables will be converted to a ordinal variable which is hard to track back what is the original variables that I had in table X. You can see the results here:
view(Mdld.Trained{1},’Mode’,’graph’)
As you see, the labels are correct, but for example, in the X table, I have "Small" and "Large" categories for vessel radius, but I get 1 and 2 labels instead, for which I event do not know readily which one is which!
I could not find a way to keep the labels on the node. Can anyone help me to do so? I have a table in which all of its columns are categorical variables, and the label associated with them is also a categorical variable. I use this piece of code to fit a tree to the data:
Mdld = fitctree(X,Ycat,’MaxNumSplits’,5,’PredictorSelection’,’curvature’,…
‘CrossVal’,’on’);
when I plot the tree, the variables will be converted to a ordinal variable which is hard to track back what is the original variables that I had in table X. You can see the results here:
view(Mdld.Trained{1},’Mode’,’graph’)
As you see, the labels are correct, but for example, in the X table, I have "Small" and "Large" categories for vessel radius, but I get 1 and 2 labels instead, for which I event do not know readily which one is which!
I could not find a way to keep the labels on the node. Can anyone help me to do so? decision tree, view, visualize, categorical variables MATLAB Answers — New Questions
Unzipping MDF4 files and combining them into 1
I am trying unzip a collection of mdf4 files, combine them into 1 mdf4 file and save it. I am getting the following error (attached):
‘Time_10Hz’ does exist in the dataset as shown (attached)
Any help would be appreciated! Code is attached below
% Open file selection dialog to choose MDF files
[fileNames, folderPath] = uigetfile(‘*.mdf;*.zip’, ‘Select MDF or ZIP files’, ‘MultiSelect’, ‘on’);
if ~ischar(fileNames) % Check if any file is selected
combinedData = struct(); % Initialize an empty structure to store combined data
for i = 1:numel(fileNames)
filePath = fullfile(folderPath, fileNames{i});
% Check if the file is a ZIP archive
if endsWith(filePath, ‘.zip’)
% Unzip the archive to a temporary folder
tempFolder = tempname;
unzip(filePath, tempFolder);
% Find the MDF file(s) in the temporary folder
mdfFiles = dir(fullfile(tempFolder, ‘*.mdf’));
for j = 1:numel(mdfFiles)
mdfFilePath = fullfile(tempFolder, mdfFiles(j).name);
data = mdfimport(mdfFilePath);
% Extract desired channel groups and signals
desiredChannelGroups = {’32’}; % Replace with your desired channel groups
desiredSignals = {‘YawRate’, ‘EPBMode’}; % Replace with your desired signals
for k = 1:numel(desiredChannelGroups)
channelGroup = desiredChannelGroups{k};
if isfield(data, channelGroup)
for l = 1:numel(desiredSignals)
signal = desiredSignals{l};
if isfield(data.(channelGroup), signal)
if isfield(combinedData, [channelGroup ‘.’ signal])
combinedData.([channelGroup ‘.’ signal]) = [combinedData.([channelGroup ‘.’ signal]), data.(channelGroup).(signal)];
else
combinedData.([channelGroup ‘.’ signal]) = data.(channelGroup).(signal);
end
end
end
end
end
end
% Clean up the temporary folder
rmdir(tempFolder, ‘s’);
else
% Import the MDF file directly
data = mdfimport(filePath);
% Extract desired channel groups and signals
desiredChannelGroups = {’32’}; % Replace with your desired channel groups
desiredSignals = {‘YawRate’, ‘EPBMode’}; % Replace with your desired signals
for k = 1:numel(desiredChannelGroups)
channelGroup = desiredChannelGroups{k};
if isfield(data, channelGroup)
for l = 1:numel(desiredSignals)
signal = desiredSignals{l};
if isfield(data.(channelGroup), signal)
if isfield(combinedData, [channelGroup ‘.’ signal])
combinedData.([channelGroup ‘.’ signal]) = [combinedData.([channelGroup ‘.’ signal]), data.(channelGroup).(signal)];
else
combinedData.([channelGroup ‘.’ signal]) = data.(channelGroup).(signal);
end
end
end
end
end
end
end
% Create a timetable from the combined data structure
timeField = ‘Time_10Hz’; % Assuming ‘Time’ is the field containing time information
combinedTimetable = timetable(combinedData.(timeField), combinedData);
% Save the combined data to a new MDF file
[fileName, folderPath] = uiputfile(‘*.mdf’, ‘Save Combined MDF File’);
if fileName ~= 0 % Check if a file name is provided
outputFilePath = fullfile(folderPath, fileName);
mdfWrite(outputFilePath, combinedTimetable);
msgbox(‘MDF files combined successfully!’, ‘Success’);
end
endI am trying unzip a collection of mdf4 files, combine them into 1 mdf4 file and save it. I am getting the following error (attached):
‘Time_10Hz’ does exist in the dataset as shown (attached)
Any help would be appreciated! Code is attached below
% Open file selection dialog to choose MDF files
[fileNames, folderPath] = uigetfile(‘*.mdf;*.zip’, ‘Select MDF or ZIP files’, ‘MultiSelect’, ‘on’);
if ~ischar(fileNames) % Check if any file is selected
combinedData = struct(); % Initialize an empty structure to store combined data
for i = 1:numel(fileNames)
filePath = fullfile(folderPath, fileNames{i});
% Check if the file is a ZIP archive
if endsWith(filePath, ‘.zip’)
% Unzip the archive to a temporary folder
tempFolder = tempname;
unzip(filePath, tempFolder);
% Find the MDF file(s) in the temporary folder
mdfFiles = dir(fullfile(tempFolder, ‘*.mdf’));
for j = 1:numel(mdfFiles)
mdfFilePath = fullfile(tempFolder, mdfFiles(j).name);
data = mdfimport(mdfFilePath);
% Extract desired channel groups and signals
desiredChannelGroups = {’32’}; % Replace with your desired channel groups
desiredSignals = {‘YawRate’, ‘EPBMode’}; % Replace with your desired signals
for k = 1:numel(desiredChannelGroups)
channelGroup = desiredChannelGroups{k};
if isfield(data, channelGroup)
for l = 1:numel(desiredSignals)
signal = desiredSignals{l};
if isfield(data.(channelGroup), signal)
if isfield(combinedData, [channelGroup ‘.’ signal])
combinedData.([channelGroup ‘.’ signal]) = [combinedData.([channelGroup ‘.’ signal]), data.(channelGroup).(signal)];
else
combinedData.([channelGroup ‘.’ signal]) = data.(channelGroup).(signal);
end
end
end
end
end
end
% Clean up the temporary folder
rmdir(tempFolder, ‘s’);
else
% Import the MDF file directly
data = mdfimport(filePath);
% Extract desired channel groups and signals
desiredChannelGroups = {’32’}; % Replace with your desired channel groups
desiredSignals = {‘YawRate’, ‘EPBMode’}; % Replace with your desired signals
for k = 1:numel(desiredChannelGroups)
channelGroup = desiredChannelGroups{k};
if isfield(data, channelGroup)
for l = 1:numel(desiredSignals)
signal = desiredSignals{l};
if isfield(data.(channelGroup), signal)
if isfield(combinedData, [channelGroup ‘.’ signal])
combinedData.([channelGroup ‘.’ signal]) = [combinedData.([channelGroup ‘.’ signal]), data.(channelGroup).(signal)];
else
combinedData.([channelGroup ‘.’ signal]) = data.(channelGroup).(signal);
end
end
end
end
end
end
end
% Create a timetable from the combined data structure
timeField = ‘Time_10Hz’; % Assuming ‘Time’ is the field containing time information
combinedTimetable = timetable(combinedData.(timeField), combinedData);
% Save the combined data to a new MDF file
[fileName, folderPath] = uiputfile(‘*.mdf’, ‘Save Combined MDF File’);
if fileName ~= 0 % Check if a file name is provided
outputFilePath = fullfile(folderPath, fileName);
mdfWrite(outputFilePath, combinedTimetable);
msgbox(‘MDF files combined successfully!’, ‘Success’);
end
end I am trying unzip a collection of mdf4 files, combine them into 1 mdf4 file and save it. I am getting the following error (attached):
‘Time_10Hz’ does exist in the dataset as shown (attached)
Any help would be appreciated! Code is attached below
% Open file selection dialog to choose MDF files
[fileNames, folderPath] = uigetfile(‘*.mdf;*.zip’, ‘Select MDF or ZIP files’, ‘MultiSelect’, ‘on’);
if ~ischar(fileNames) % Check if any file is selected
combinedData = struct(); % Initialize an empty structure to store combined data
for i = 1:numel(fileNames)
filePath = fullfile(folderPath, fileNames{i});
% Check if the file is a ZIP archive
if endsWith(filePath, ‘.zip’)
% Unzip the archive to a temporary folder
tempFolder = tempname;
unzip(filePath, tempFolder);
% Find the MDF file(s) in the temporary folder
mdfFiles = dir(fullfile(tempFolder, ‘*.mdf’));
for j = 1:numel(mdfFiles)
mdfFilePath = fullfile(tempFolder, mdfFiles(j).name);
data = mdfimport(mdfFilePath);
% Extract desired channel groups and signals
desiredChannelGroups = {’32’}; % Replace with your desired channel groups
desiredSignals = {‘YawRate’, ‘EPBMode’}; % Replace with your desired signals
for k = 1:numel(desiredChannelGroups)
channelGroup = desiredChannelGroups{k};
if isfield(data, channelGroup)
for l = 1:numel(desiredSignals)
signal = desiredSignals{l};
if isfield(data.(channelGroup), signal)
if isfield(combinedData, [channelGroup ‘.’ signal])
combinedData.([channelGroup ‘.’ signal]) = [combinedData.([channelGroup ‘.’ signal]), data.(channelGroup).(signal)];
else
combinedData.([channelGroup ‘.’ signal]) = data.(channelGroup).(signal);
end
end
end
end
end
end
% Clean up the temporary folder
rmdir(tempFolder, ‘s’);
else
% Import the MDF file directly
data = mdfimport(filePath);
% Extract desired channel groups and signals
desiredChannelGroups = {’32’}; % Replace with your desired channel groups
desiredSignals = {‘YawRate’, ‘EPBMode’}; % Replace with your desired signals
for k = 1:numel(desiredChannelGroups)
channelGroup = desiredChannelGroups{k};
if isfield(data, channelGroup)
for l = 1:numel(desiredSignals)
signal = desiredSignals{l};
if isfield(data.(channelGroup), signal)
if isfield(combinedData, [channelGroup ‘.’ signal])
combinedData.([channelGroup ‘.’ signal]) = [combinedData.([channelGroup ‘.’ signal]), data.(channelGroup).(signal)];
else
combinedData.([channelGroup ‘.’ signal]) = data.(channelGroup).(signal);
end
end
end
end
end
end
end
% Create a timetable from the combined data structure
timeField = ‘Time_10Hz’; % Assuming ‘Time’ is the field containing time information
combinedTimetable = timetable(combinedData.(timeField), combinedData);
% Save the combined data to a new MDF file
[fileName, folderPath] = uiputfile(‘*.mdf’, ‘Save Combined MDF File’);
if fileName ~= 0 % Check if a file name is provided
outputFilePath = fullfile(folderPath, fileName);
mdfWrite(outputFilePath, combinedTimetable);
msgbox(‘MDF files combined successfully!’, ‘Success’);
end
end mdf4, matlab, import, mdfimport MATLAB Answers — New Questions
parse error and Directory error for class definition
hey guys, in the below code, i am having 2 errors:
I cannot run the code due to this error: "A class definition must be in an "@" directory",
there is a "parse error usage might be invalid matlab syntax" at line "inputData"
the working folder in this case is : C:UsersautoCar@PCTemplates@preprocessor
inside templates folder I have also other working .m scripts, but without any class definitions.
matlab verison R2015b
How to solve this issues?
Thankyou!
classdef preprocessor
properties
vehicleParameters
weatherConditions
roadDefinitions
maneuvers
faultInjections
end
methods
function obj = preprocessor(vehicleParams, weatherConds, roadDefs, maneuvers, faults)
if nargin > 0
obj.vehicleParameters = vehicleParams;
obj.weatherConditions = weatherConds;
obj.roadDefinitions = roadDefs;
obj.maneuvers = maneuvers;
obj.faultInjections = faults;
end
end
end
end
%% %% Define the structure containing all parameters and values
inputData = struct();hey guys, in the below code, i am having 2 errors:
I cannot run the code due to this error: "A class definition must be in an "@" directory",
there is a "parse error usage might be invalid matlab syntax" at line "inputData"
the working folder in this case is : C:UsersautoCar@PCTemplates@preprocessor
inside templates folder I have also other working .m scripts, but without any class definitions.
matlab verison R2015b
How to solve this issues?
Thankyou!
classdef preprocessor
properties
vehicleParameters
weatherConditions
roadDefinitions
maneuvers
faultInjections
end
methods
function obj = preprocessor(vehicleParams, weatherConds, roadDefs, maneuvers, faults)
if nargin > 0
obj.vehicleParameters = vehicleParams;
obj.weatherConditions = weatherConds;
obj.roadDefinitions = roadDefs;
obj.maneuvers = maneuvers;
obj.faultInjections = faults;
end
end
end
end
%% %% Define the structure containing all parameters and values
inputData = struct(); hey guys, in the below code, i am having 2 errors:
I cannot run the code due to this error: "A class definition must be in an "@" directory",
there is a "parse error usage might be invalid matlab syntax" at line "inputData"
the working folder in this case is : C:UsersautoCar@PCTemplates@preprocessor
inside templates folder I have also other working .m scripts, but without any class definitions.
matlab verison R2015b
How to solve this issues?
Thankyou!
classdef preprocessor
properties
vehicleParameters
weatherConditions
roadDefinitions
maneuvers
faultInjections
end
methods
function obj = preprocessor(vehicleParams, weatherConds, roadDefs, maneuvers, faults)
if nargin > 0
obj.vehicleParameters = vehicleParams;
obj.weatherConditions = weatherConds;
obj.roadDefinitions = roadDefs;
obj.maneuvers = maneuvers;
obj.faultInjections = faults;
end
end
end
end
%% %% Define the structure containing all parameters and values
inputData = struct(); matlab, oop, student, class MATLAB Answers — New Questions
plot normal distribution plot on histogram of residuals
Does anyone know how to do that?
Would be great if it could look like that:
<</matlabcentral/answers/uploaded_files/15773/heavy_tailed_plot.gif>>
Thank you!!!Does anyone know how to do that?
Would be great if it could look like that:
<</matlabcentral/answers/uploaded_files/15773/heavy_tailed_plot.gif>>
Thank you!!! Does anyone know how to do that?
Would be great if it could look like that:
<</matlabcentral/answers/uploaded_files/15773/heavy_tailed_plot.gif>>
Thank you!!! histogram of residuals, normal probability MATLAB Answers — New Questions