Tag Archives: matlab
Shallow Neural Networks does not work with MATLAB APP after compilation
Dear all,
I am trying to implement a trained Shallow Neural Network inside a designed Matlab APP. The ANN has 2 hidden layers and has been calibrated through a code with the function train, and then saved in the workspace, as follows:
[net,tr] = train(net,x,t);
save("namefile.mat",’net’)
At this point, for using the ANN inside an .mlapp file, I am loading it with the function "load", and the using it recalling the name of the net itself, as follows:
load("namefile")
output=net(input);
As long as I am using this approach, before the APP compilation, everything works fine with the execution of the .mlapp file. However, after the creation of the standalone application through the "Application Compiler", the prevision line gives the following error:
"Index exceeds the number of array elements. Index must not exceed 1."
I am trying to solve the problem also by using the "sim" function, but it didn’t work, since it seems a compatibility issue between the .mlapp and the .exe file.
How can I solve this problem?
Thank you and regardsDear all,
I am trying to implement a trained Shallow Neural Network inside a designed Matlab APP. The ANN has 2 hidden layers and has been calibrated through a code with the function train, and then saved in the workspace, as follows:
[net,tr] = train(net,x,t);
save("namefile.mat",’net’)
At this point, for using the ANN inside an .mlapp file, I am loading it with the function "load", and the using it recalling the name of the net itself, as follows:
load("namefile")
output=net(input);
As long as I am using this approach, before the APP compilation, everything works fine with the execution of the .mlapp file. However, after the creation of the standalone application through the "Application Compiler", the prevision line gives the following error:
"Index exceeds the number of array elements. Index must not exceed 1."
I am trying to solve the problem also by using the "sim" function, but it didn’t work, since it seems a compatibility issue between the .mlapp and the .exe file.
How can I solve this problem?
Thank you and regards Dear all,
I am trying to implement a trained Shallow Neural Network inside a designed Matlab APP. The ANN has 2 hidden layers and has been calibrated through a code with the function train, and then saved in the workspace, as follows:
[net,tr] = train(net,x,t);
save("namefile.mat",’net’)
At this point, for using the ANN inside an .mlapp file, I am loading it with the function "load", and the using it recalling the name of the net itself, as follows:
load("namefile")
output=net(input);
As long as I am using this approach, before the APP compilation, everything works fine with the execution of the .mlapp file. However, after the creation of the standalone application through the "Application Compiler", the prevision line gives the following error:
"Index exceeds the number of array elements. Index must not exceed 1."
I am trying to solve the problem also by using the "sim" function, but it didn’t work, since it seems a compatibility issue between the .mlapp and the .exe file.
How can I solve this problem?
Thank you and regards app designer, neural network, not working, compatibility, compiler MATLAB Answers — New Questions
安裝在電腦上的其他位置 (Installation of MATLAB on a different drive)
請問我能安裝到C槽以外的地方嗎,每次當我想要這麼做時,安裝程式就會出現錯誤(網路錯誤),但這很明顯和網路無關,因為我安裝至C槽時沒有問題。
Can I install MATLAB on drives other than the C drive? Each time I attempt to do this, the installer displays a network error; however, it is clearly not network-related, as I have previously installed it without any issues on the C drive.請問我能安裝到C槽以外的地方嗎,每次當我想要這麼做時,安裝程式就會出現錯誤(網路錯誤),但這很明顯和網路無關,因為我安裝至C槽時沒有問題。
Can I install MATLAB on drives other than the C drive? Each time I attempt to do this, the installer displays a network error; however, it is clearly not network-related, as I have previously installed it without any issues on the C drive. 請問我能安裝到C槽以外的地方嗎,每次當我想要這麼做時,安裝程式就會出現錯誤(網路錯誤),但這很明顯和網路無關,因為我安裝至C槽時沒有問題。
Can I install MATLAB on drives other than the C drive? Each time I attempt to do this, the installer displays a network error; however, it is clearly not network-related, as I have previously installed it without any issues on the C drive. installation MATLAB Answers — New Questions
Plot exceeding time limit due to large dataset
I have a dataset in which I have to categorize the rise of UV levels from a satellite.
As this satellite orbits the Earth there are periods where it cannot process the UV levels.
The raw data that I am getting is close to a square wave.
I have to categorize this data into rising where the satellite comes out of the influence of the Earth,
OnDuty where the satellite is able to record data properly and falling where it is going behind the Earth.
The dataset contains a timestamp of frequency 1Hz where one value is recorded every second.
I want to categorize these three classes into for atleast now, red, green and blue.
The dataset contains the said timestamp and the corresponding UV values.
I am currently using a threshold to categorize the data and is working for a smaller dataset, however with a dataset of 80000 entry points my code is not able to run efficiently.
Here is the code. Where states stores the ith values nature. The problem lies within the for loop that runs for the plotting.
function categorize_square_wave()
y_foo = readtable("80000entries.csv", Range=’uv_values’);
y_values = table2array(y_foo);
k = 1; % Sensitivity parameter
differences = diff(y_values);
% comments have been put to remove staticstical computation
mu = mean(differences);
sigma = std(differences);
% Thresholds
T_high = mu + k * sigma;
T_low = mu – k * sigma;
% coded directly to reduce computation
% T_high = 500;
% T_low = -100;
% Categorizing based on threshold values
% Initialize states with zeros (default to dutiful)
states = zeros(1, length(differences));
% Assign states using vectorization
states(differences > T_high) = 1; % Rising
states(differences < T_low) = -1; % Falling
% Collect dutiful values
% dutiful_values = y_values(states == 0);
figure;
hold on;
for i = 1:length(differences)
if states(i) == 1
plot([i, i+1], [y_values(i), y_values(i+1)], ‘g’, ‘LineWidth’, 2); % Green for Rising
elseif states(i) == -1
plot([i, i+1], [y_values(i), y_values(i+1)], ‘r’, ‘LineWidth’, 2); % Red for Falling
else
plot([i, i+1], [y_values(i), y_values(i+1)], ‘b’, ‘LineWidth’, 2); % Blue for Dutiful
% dutiful_values = [dutiful_values, y_values(i)]; % Collect Dutiful values
end
end
% Plot original values in dashed lines
plot(y_values, ‘k–‘, ‘LineWidth’, 1); % Black dashed line for original values
yline(T_high, ‘r–‘, ‘T_{high}’, ‘LabelVerticalAlignment’, ‘bottom’, ‘LabelHorizontalAlignment’, ‘right’); % Dashed red line for T_high
xlabel(‘Sample Number’);
ylabel(‘y(t)’);
title(‘Classification of Changes in Square Wave’);
hold off;
end
categorize_square_wave();
% we have a good estimation of what values are
% is there a way to use flags on each state so that we can just plot
% without checking each value?
% implemetn flagsI have a dataset in which I have to categorize the rise of UV levels from a satellite.
As this satellite orbits the Earth there are periods where it cannot process the UV levels.
The raw data that I am getting is close to a square wave.
I have to categorize this data into rising where the satellite comes out of the influence of the Earth,
OnDuty where the satellite is able to record data properly and falling where it is going behind the Earth.
The dataset contains a timestamp of frequency 1Hz where one value is recorded every second.
I want to categorize these three classes into for atleast now, red, green and blue.
The dataset contains the said timestamp and the corresponding UV values.
I am currently using a threshold to categorize the data and is working for a smaller dataset, however with a dataset of 80000 entry points my code is not able to run efficiently.
Here is the code. Where states stores the ith values nature. The problem lies within the for loop that runs for the plotting.
function categorize_square_wave()
y_foo = readtable("80000entries.csv", Range=’uv_values’);
y_values = table2array(y_foo);
k = 1; % Sensitivity parameter
differences = diff(y_values);
% comments have been put to remove staticstical computation
mu = mean(differences);
sigma = std(differences);
% Thresholds
T_high = mu + k * sigma;
T_low = mu – k * sigma;
% coded directly to reduce computation
% T_high = 500;
% T_low = -100;
% Categorizing based on threshold values
% Initialize states with zeros (default to dutiful)
states = zeros(1, length(differences));
% Assign states using vectorization
states(differences > T_high) = 1; % Rising
states(differences < T_low) = -1; % Falling
% Collect dutiful values
% dutiful_values = y_values(states == 0);
figure;
hold on;
for i = 1:length(differences)
if states(i) == 1
plot([i, i+1], [y_values(i), y_values(i+1)], ‘g’, ‘LineWidth’, 2); % Green for Rising
elseif states(i) == -1
plot([i, i+1], [y_values(i), y_values(i+1)], ‘r’, ‘LineWidth’, 2); % Red for Falling
else
plot([i, i+1], [y_values(i), y_values(i+1)], ‘b’, ‘LineWidth’, 2); % Blue for Dutiful
% dutiful_values = [dutiful_values, y_values(i)]; % Collect Dutiful values
end
end
% Plot original values in dashed lines
plot(y_values, ‘k–‘, ‘LineWidth’, 1); % Black dashed line for original values
yline(T_high, ‘r–‘, ‘T_{high}’, ‘LabelVerticalAlignment’, ‘bottom’, ‘LabelHorizontalAlignment’, ‘right’); % Dashed red line for T_high
xlabel(‘Sample Number’);
ylabel(‘y(t)’);
title(‘Classification of Changes in Square Wave’);
hold off;
end
categorize_square_wave();
% we have a good estimation of what values are
% is there a way to use flags on each state so that we can just plot
% without checking each value?
% implemetn flags I have a dataset in which I have to categorize the rise of UV levels from a satellite.
As this satellite orbits the Earth there are periods where it cannot process the UV levels.
The raw data that I am getting is close to a square wave.
I have to categorize this data into rising where the satellite comes out of the influence of the Earth,
OnDuty where the satellite is able to record data properly and falling where it is going behind the Earth.
The dataset contains a timestamp of frequency 1Hz where one value is recorded every second.
I want to categorize these three classes into for atleast now, red, green and blue.
The dataset contains the said timestamp and the corresponding UV values.
I am currently using a threshold to categorize the data and is working for a smaller dataset, however with a dataset of 80000 entry points my code is not able to run efficiently.
Here is the code. Where states stores the ith values nature. The problem lies within the for loop that runs for the plotting.
function categorize_square_wave()
y_foo = readtable("80000entries.csv", Range=’uv_values’);
y_values = table2array(y_foo);
k = 1; % Sensitivity parameter
differences = diff(y_values);
% comments have been put to remove staticstical computation
mu = mean(differences);
sigma = std(differences);
% Thresholds
T_high = mu + k * sigma;
T_low = mu – k * sigma;
% coded directly to reduce computation
% T_high = 500;
% T_low = -100;
% Categorizing based on threshold values
% Initialize states with zeros (default to dutiful)
states = zeros(1, length(differences));
% Assign states using vectorization
states(differences > T_high) = 1; % Rising
states(differences < T_low) = -1; % Falling
% Collect dutiful values
% dutiful_values = y_values(states == 0);
figure;
hold on;
for i = 1:length(differences)
if states(i) == 1
plot([i, i+1], [y_values(i), y_values(i+1)], ‘g’, ‘LineWidth’, 2); % Green for Rising
elseif states(i) == -1
plot([i, i+1], [y_values(i), y_values(i+1)], ‘r’, ‘LineWidth’, 2); % Red for Falling
else
plot([i, i+1], [y_values(i), y_values(i+1)], ‘b’, ‘LineWidth’, 2); % Blue for Dutiful
% dutiful_values = [dutiful_values, y_values(i)]; % Collect Dutiful values
end
end
% Plot original values in dashed lines
plot(y_values, ‘k–‘, ‘LineWidth’, 1); % Black dashed line for original values
yline(T_high, ‘r–‘, ‘T_{high}’, ‘LabelVerticalAlignment’, ‘bottom’, ‘LabelHorizontalAlignment’, ‘right’); % Dashed red line for T_high
xlabel(‘Sample Number’);
ylabel(‘y(t)’);
title(‘Classification of Changes in Square Wave’);
hold off;
end
categorize_square_wave();
% we have a good estimation of what values are
% is there a way to use flags on each state so that we can just plot
% without checking each value?
% implemetn flags data cleaning, plotting, optimization MATLAB Answers — New Questions
How to periodically change Simulink Output file name?
Hello,
I am trying to periodically output data from Simulink using the "To File" block, and I would like to use the current date and time as the name for each output file. Currently, I am trying to periodically call a function which determines the real-world time, converts that to a string, and uses the set_param command to change the output file name accordingly.
function [Y, M, D, H, MN, S] = fcn()
coder.extrinsic(‘now’);
coder.extrinsic(‘datevec’);
coder.extrinsic(‘sprintf’);
coder.extrinsic(‘set_param’);
[Y, M, D, H, MN, S] = datevec(now,’HH:MM:SS.FFF’);
date_time=[Y M D H MN S];
fname = sprintf(‘OutputFile_%04d_%02d_%02d_%02d_%02d_%02f.mat’, date_time);
set_param(‘untitled/To File’,’Filename’,fname);
end
Unfortunately, I am receiving the error message "Cannot change parameter ‘File name: (Filename)’ of ‘untitled/To File’ while simulation is running".
I have only been able to successfully use the set_param command while Simulink is not running, but I need to change my output file name during simulation. Is this possible? Is there a better way to approach this problem?
Thank you,
DrewHello,
I am trying to periodically output data from Simulink using the "To File" block, and I would like to use the current date and time as the name for each output file. Currently, I am trying to periodically call a function which determines the real-world time, converts that to a string, and uses the set_param command to change the output file name accordingly.
function [Y, M, D, H, MN, S] = fcn()
coder.extrinsic(‘now’);
coder.extrinsic(‘datevec’);
coder.extrinsic(‘sprintf’);
coder.extrinsic(‘set_param’);
[Y, M, D, H, MN, S] = datevec(now,’HH:MM:SS.FFF’);
date_time=[Y M D H MN S];
fname = sprintf(‘OutputFile_%04d_%02d_%02d_%02d_%02d_%02f.mat’, date_time);
set_param(‘untitled/To File’,’Filename’,fname);
end
Unfortunately, I am receiving the error message "Cannot change parameter ‘File name: (Filename)’ of ‘untitled/To File’ while simulation is running".
I have only been able to successfully use the set_param command while Simulink is not running, but I need to change my output file name during simulation. Is this possible? Is there a better way to approach this problem?
Thank you,
Drew Hello,
I am trying to periodically output data from Simulink using the "To File" block, and I would like to use the current date and time as the name for each output file. Currently, I am trying to periodically call a function which determines the real-world time, converts that to a string, and uses the set_param command to change the output file name accordingly.
function [Y, M, D, H, MN, S] = fcn()
coder.extrinsic(‘now’);
coder.extrinsic(‘datevec’);
coder.extrinsic(‘sprintf’);
coder.extrinsic(‘set_param’);
[Y, M, D, H, MN, S] = datevec(now,’HH:MM:SS.FFF’);
date_time=[Y M D H MN S];
fname = sprintf(‘OutputFile_%04d_%02d_%02d_%02d_%02d_%02f.mat’, date_time);
set_param(‘untitled/To File’,’Filename’,fname);
end
Unfortunately, I am receiving the error message "Cannot change parameter ‘File name: (Filename)’ of ‘untitled/To File’ while simulation is running".
I have only been able to successfully use the set_param command while Simulink is not running, but I need to change my output file name during simulation. Is this possible? Is there a better way to approach this problem?
Thank you,
Drew periodically change simulink output file name MATLAB Answers — New Questions
Issue with fixed-point FFT using the dsp.FFT function
Hello everyone, I’m new to MATLAB and MATLAB coder. I’m trying to self-generate a code for a STM32F446RE microcontroller with MATLAB coder to use it on STM32CubeIDE. The code that I’m trying to generate is an FFT of fixed-point data. I’m using the dsp.FFT function with a 16-bit world length because it supports fixed-point.
This is my MATLAB script
s = readmatrix("FFT_data.txt");
c = fi(s,1,16);
q = fourier(c);
This is my MATLAB function
function [Y] = fourier(X)
ft = dsp.FFT("FFTLengthSource","Property","Normalize",true,"FFTLength",1024);
Y = abs(ft(X));
end
The program is working on my Micro, but it’s a little bit too slow. I think the problem is that the C code generated by MATLAB uses 64-bit Multiword functions despite the data being 16 bits long in MATLAB.
static void c_MWDSPCG_FFT_DblLen_Nrm_YCs16n(cint16_T y[], int nChans, int nRows,
const short twiddleTable[],
int twiddleStep)
{
int64m_T r;
int64m_T r1;
int64m_T r10;
int64m_T r11;
int64m_T r12;
int64m_T r13;
int64m_T r14;
int64m_T r15;
int64m_T r16;
int64m_T r2;
int64m_T r3;
int64m_T r4;
int64m_T r5;
int64m_T r6;
int64m_T r7;
int64m_T r8;
int64m_T r9;
int N2;
int N4;
int i;
int ix;
int tempOut0Re_tmp;
int yIdx;
short tempOut0Im;
short tempOut0Re;
/* In-place "double-length" data recovery
Table-based mem-optimized twiddle computation
Used to recover linear-ordered length-N point complex FFT result
from a linear-ordered complex length-N/2 point FFT, performed
on N interleaved real values.
*/
N2 = nRows >> 1;
N4 = N2 >> 1;
yIdx = nRows * (nChans – 1);
if (nRows > 2) {
tempOut0Re_tmp = N4 + yIdx;
tempOut0Re = (short)(y[tempOut0Re_tmp].re >> 1);
tempOut0Im = (short)(y[tempOut0Re_tmp].im >> 1);
i = (N2 + N4) + yIdx;
y[i].re = tempOut0Re;
y[i].im = tempOut0Im;
y[tempOut0Re_tmp].re = tempOut0Re;
y[tempOut0Re_tmp].im = (short)-tempOut0Im;
}
if (nRows > 1) {
sLong2MultiWord(y[yIdx].re << 15, (unsigned int *)&r.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r.chunks[0U], 21U,
(unsigned int *)&r2.chunks[0U]);
sLong2MultiWord(y[yIdx].im << 15, (unsigned int *)&r1.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r1.chunks[0U], 21U,
(unsigned int *)&r.chunks[0U]);
MultiWordSub((unsigned int *)&r2.chunks[0U], (unsigned int *)&r.chunks[0U],
(unsigned int *)&r4.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r4.chunks[0U], 21U,
(unsigned int *)&r5.chunks[0U]);
sMultiWordShr((unsigned int *)&r5.chunks[0U], 1U,
(unsigned int *)&r6.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r6.chunks[0U], 21U,
(unsigned int *)&r7.chunks[0U]);
sMultiWordShr((unsigned int *)&r7.chunks[0U], 15U,
(unsigned int *)&r8.chunks[0U]);
i = N2 + yIdx;
y[i].re = (short)MultiWord2sLong((unsigned int *)&r8.chunks[0U]);
y[i].im = 0;
}
sLong2MultiWord(y[yIdx].re << 15, (unsigned int *)&r1.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r1.chunks[0U], 21U,
(unsigned int *)&r.chunks[0U]);
sLong2MultiWord(y[yIdx].im << 15, (unsigned int *)&r3.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r3.chunks[0U], 21U,
(unsigned int *)&r1.chunks[0U]);
MultiWordAdd((unsigned int *)&r.chunks[0U], (unsigned int *)&r1.chunks[0U],
(unsigned int *)&r2.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r2.chunks[0U], 21U,
(unsigned int *)&r4.chunks[0U]);
sMultiWordShr((unsigned int *)&r4.chunks[0U], 1U,
(unsigned int *)&r5.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r5.chunks[0U], 21U,
(unsigned int *)&r6.chunks[0U]);
sMultiWordShr((unsigned int *)&r6.chunks[0U], 15U,
(unsigned int *)&r7.chunks[0U]);
y[yIdx].re = (short)MultiWord2sLong((unsigned int *)&r7.chunks[0U]);
y[yIdx].im = 0;
tempOut0Re_tmp = twiddleStep;
for (ix = 1; ix < N4; ix++) {
int i1;
int i2;
int i3;
int i4;
int i5;
int i6;
short cTemp_im;
short cTemp_re;
i = ix + yIdx;
i1 = y[i].re << 15;
sLong2MultiWord(i1, (unsigned int *)&r3.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r3.chunks[0U], 21U,
(unsigned int *)&r1.chunks[0U]);
i2 = (N2 – ix) + yIdx;
i3 = y[i2].re << 15;
sLong2MultiWord(i3, (unsigned int *)&r9.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r9.chunks[0U], 21U,
(unsigned int *)&r3.chunks[0U]);
MultiWordAdd((unsigned int *)&r1.chunks[0U], (unsigned int *)&r3.chunks[0U],
(unsigned int *)&r.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r.chunks[0U], 21U,
(unsigned int *)&r2.chunks[0U]);
sMultiWordShr((unsigned int *)&r2.chunks[0U], 2U,
(unsigned int *)&r4.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r4.chunks[0U], 21U,
(unsigned int *)&r5.chunks[0U]);
sMultiWordShr((unsigned int *)&r5.chunks[0U], 15U,
(unsigned int *)&r6.chunks[0U]);
tempOut0Re = (short)MultiWord2sLong((unsigned int *)&r6.chunks[0U]);
i4 = y[i].im << 15;
sLong2MultiWord(i4, (unsigned int *)&r9.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r9.chunks[0U], 21U,
(unsigned int *)&r3.chunks[0U]);
i5 = y[i2].im << 15;
sLong2MultiWord(i5, (unsigned int *)&r10.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r10.chunks[0U], 21U,
(unsigned int *)&r9.chunks[0U]);
MultiWordSub((unsigned int *)&r3.chunks[0U], (unsigned int *)&r9.chunks[0U],
(unsigned int *)&r1.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r1.chunks[0U], 21U,
(unsigned int *)&r.chunks[0U]);
sMultiWordShr((unsigned int *)&r.chunks[0U], 2U,
(unsigned int *)&r2.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r2.chunks[0U], 21U,
(unsigned int *)&r4.chunks[0U]);
sMultiWordShr((unsigned int *)&r4.chunks[0U], 15U,
(unsigned int *)&r5.chunks[0U]);
tempOut0Im = (short)MultiWord2sLong((unsigned int *)&r5.chunks[0U]);
sLong2MultiWord(i4, (unsigned int *)&r10.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r10.chunks[0U], 21U,
(unsigned int *)&r9.chunks[0U]);
sLong2MultiWord(i5, (unsigned int *)&r11.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r11.chunks[0U], 21U,
(unsigned int *)&r10.chunks[0U]);
MultiWordAdd((unsigned int *)&r9.chunks[0U],
(unsigned int *)&r10.chunks[0U],
(unsigned int *)&r3.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r3.chunks[0U], 21U,
(unsigned int *)&r1.chunks[0U]);
sMultiWordShr((unsigned int *)&r1.chunks[0U], 2U,
(unsigned int *)&r.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r.chunks[0U], 21U,
(unsigned int *)&r2.chunks[0U]);
sMultiWordShr((unsigned int *)&r2.chunks[0U], 15U,
(unsigned int *)&r4.chunks[0U]);
y[i].re = (short)MultiWord2sLong((unsigned int *)&r4.chunks[0U]);
sLong2MultiWord(i3, (unsigned int *)&r11.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r11.chunks[0U], 21U,
(unsigned int *)&r10.chunks[0U]);
sLong2MultiWord(i1, (unsigned int *)&r12.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r12.chunks[0U], 21U,
(unsigned int *)&r11.chunks[0U]);
MultiWordSub((unsigned int *)&r10.chunks[0U],
(unsigned int *)&r11.chunks[0U],
(unsigned int *)&r9.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r9.chunks[0U], 21U,
(unsigned int *)&r3.chunks[0U]);
sMultiWordShr((unsigned int *)&r3.chunks[0U], 2U,
(unsigned int *)&r1.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r1.chunks[0U], 21U,
(unsigned int *)&r.chunks[0U]);
sMultiWordShr((unsigned int *)&r.chunks[0U], 15U,
(unsigned int *)&r2.chunks[0U]);
y[i].im = (short)MultiWord2sLong((unsigned int *)&r2.chunks[0U]);
i1 = twiddleTable[tempOut0Re_tmp + N4 * twiddleStep];
i3 = y[i].re;
sLong2MultiWord(i1 * i3, (unsigned int *)&r11.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r11.chunks[0U], 22U,
(unsigned int *)&r10.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r10.chunks[0U], 21U,
(unsigned int *)&r9.chunks[0U]);
i4 = y[i].im;
i5 = twiddleTable[tempOut0Re_tmp + N2 * twiddleStep];
sLong2MultiWord(i5 * i4, (unsigned int *)&r12.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r12.chunks[0U], 22U,
(unsigned int *)&r11.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r11.chunks[0U], 21U,
(unsigned int *)&r10.chunks[0U]);
MultiWordSub((unsigned int *)&r9.chunks[0U],
(unsigned int *)&r10.chunks[0U],
(unsigned int *)&r3.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r3.chunks[0U], 21U,
(unsigned int *)&r1.chunks[0U]);
sMultiWordShr((unsigned int *)&r1.chunks[0U], 15U,
(unsigned int *)&r.chunks[0U]);
cTemp_re = (short)MultiWord2sLong((unsigned int *)&r.chunks[0U]);
sLong2MultiWord(i1 * i4, (unsigned int *)&r12.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r12.chunks[0U], 22U,
(unsigned int *)&r11.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r11.chunks[0U], 21U,
(unsigned int *)&r10.chunks[0U]);
sLong2MultiWord(i5 * i3, (unsigned int *)&r13.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r13.chunks[0U], 22U,
(unsigned int *)&r12.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r12.chunks[0U], 21U,
(unsigned int *)&r11.chunks[0U]);
MultiWordAdd((unsigned int *)&r10.chunks[0U],
(unsigned int *)&r11.chunks[0U],
(unsigned int *)&r9.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r9.chunks[0U], 21U,
(unsigned int *)&r3.chunks[0U]);
sMultiWordShr((unsigned int *)&r3.chunks[0U], 15U,
(unsigned int *)&r1.chunks[0U]);
cTemp_im = (short)MultiWord2sLong((unsigned int *)&r1.chunks[0U]);
i1 = tempOut0Re << 15;
sLong2MultiWord(i1, (unsigned int *)&r12.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r12.chunks[0U], 21U,
(unsigned int *)&r11.chunks[0U]);
i3 = cTemp_re << 15;
sLong2MultiWord(i3, (unsigned int *)&r13.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r13.chunks[0U], 21U,
(unsigned int *)&r12.chunks[0U]);
MultiWordAdd((unsigned int *)&r11.chunks[0U],
(unsigned int *)&r12.chunks[0U],
(unsigned int *)&r10.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r10.chunks[0U], 21U,
(unsigned int *)&r9.chunks[0U]);
sMultiWordShr((unsigned int *)&r9.chunks[0U], 15U,
(unsigned int *)&r3.chunks[0U]);
y[i].re = (short)MultiWord2sLong((unsigned int *)&r3.chunks[0U]);
i4 = tempOut0Im << 15;
sLong2MultiWord(i4, (unsigned int *)&r13.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r13.chunks[0U], 21U,
(unsigned int *)&r12.chunks[0U]);
i5 = cTemp_im << 15;
sLong2MultiWord(i5, (unsigned int *)&r14.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r14.chunks[0U], 21U,
(unsigned int *)&r13.chunks[0U]);
MultiWordAdd((unsigned int *)&r12.chunks[0U],
(unsigned int *)&r13.chunks[0U],
(unsigned int *)&r11.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r11.chunks[0U], 21U,
(unsigned int *)&r10.chunks[0U]);
sMultiWordShr((unsigned int *)&r10.chunks[0U], 15U,
(unsigned int *)&r9.chunks[0U]);
y[i].im = (short)MultiWord2sLong((unsigned int *)&r9.chunks[0U]);
i6 = (nRows – ix) + yIdx;
y[i6].re = y[i].re;
y[i6].im = (short)-y[i].im;
sLong2MultiWord(i1, (unsigned int *)&r14.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r14.chunks[0U], 21U,
(unsigned int *)&r13.chunks[0U]);
sLong2MultiWord(i3, (unsigned int *)&r15.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r15.chunks[0U], 21U,
(unsigned int *)&r14.chunks[0U]);
MultiWordSub((unsigned int *)&r13.chunks[0U],
(unsigned int *)&r14.chunks[0U],
(unsigned int *)&r12.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r12.chunks[0U], 21U,
(unsigned int *)&r11.chunks[0U]);
sMultiWordShr((unsigned int *)&r11.chunks[0U], 15U,
(unsigned int *)&r10.chunks[0U]);
i = (N2 + ix) + yIdx;
y[i].re = (short)MultiWord2sLong((unsigned int *)&r10.chunks[0U]);
sLong2MultiWord(i4, (unsigned int *)&r15.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r15.chunks[0U], 21U,
(unsigned int *)&r14.chunks[0U]);
sLong2MultiWord(i5, (unsigned int *)&r16.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r16.chunks[0U], 21U,
(unsigned int *)&r15.chunks[0U]);
MultiWordSub((unsigned int *)&r14.chunks[0U],
(unsigned int *)&r15.chunks[0U],
(unsigned int *)&r13.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r13.chunks[0U], 21U,
(unsigned int *)&r12.chunks[0U]);
sMultiWordShr((unsigned int *)&r12.chunks[0U], 15U,
(unsigned int *)&r11.chunks[0U]);
y[i].im = (short)MultiWord2sLong((unsigned int *)&r11.chunks[0U]);
y[i2].re = y[i].re;
y[i2].im = (short)-y[i].im;
tempOut0Re_tmp += twiddleStep;
}
}
Is there a way to self-generate code that operates on 16-bit data and remove the Multiword functions?Hello everyone, I’m new to MATLAB and MATLAB coder. I’m trying to self-generate a code for a STM32F446RE microcontroller with MATLAB coder to use it on STM32CubeIDE. The code that I’m trying to generate is an FFT of fixed-point data. I’m using the dsp.FFT function with a 16-bit world length because it supports fixed-point.
This is my MATLAB script
s = readmatrix("FFT_data.txt");
c = fi(s,1,16);
q = fourier(c);
This is my MATLAB function
function [Y] = fourier(X)
ft = dsp.FFT("FFTLengthSource","Property","Normalize",true,"FFTLength",1024);
Y = abs(ft(X));
end
The program is working on my Micro, but it’s a little bit too slow. I think the problem is that the C code generated by MATLAB uses 64-bit Multiword functions despite the data being 16 bits long in MATLAB.
static void c_MWDSPCG_FFT_DblLen_Nrm_YCs16n(cint16_T y[], int nChans, int nRows,
const short twiddleTable[],
int twiddleStep)
{
int64m_T r;
int64m_T r1;
int64m_T r10;
int64m_T r11;
int64m_T r12;
int64m_T r13;
int64m_T r14;
int64m_T r15;
int64m_T r16;
int64m_T r2;
int64m_T r3;
int64m_T r4;
int64m_T r5;
int64m_T r6;
int64m_T r7;
int64m_T r8;
int64m_T r9;
int N2;
int N4;
int i;
int ix;
int tempOut0Re_tmp;
int yIdx;
short tempOut0Im;
short tempOut0Re;
/* In-place "double-length" data recovery
Table-based mem-optimized twiddle computation
Used to recover linear-ordered length-N point complex FFT result
from a linear-ordered complex length-N/2 point FFT, performed
on N interleaved real values.
*/
N2 = nRows >> 1;
N4 = N2 >> 1;
yIdx = nRows * (nChans – 1);
if (nRows > 2) {
tempOut0Re_tmp = N4 + yIdx;
tempOut0Re = (short)(y[tempOut0Re_tmp].re >> 1);
tempOut0Im = (short)(y[tempOut0Re_tmp].im >> 1);
i = (N2 + N4) + yIdx;
y[i].re = tempOut0Re;
y[i].im = tempOut0Im;
y[tempOut0Re_tmp].re = tempOut0Re;
y[tempOut0Re_tmp].im = (short)-tempOut0Im;
}
if (nRows > 1) {
sLong2MultiWord(y[yIdx].re << 15, (unsigned int *)&r.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r.chunks[0U], 21U,
(unsigned int *)&r2.chunks[0U]);
sLong2MultiWord(y[yIdx].im << 15, (unsigned int *)&r1.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r1.chunks[0U], 21U,
(unsigned int *)&r.chunks[0U]);
MultiWordSub((unsigned int *)&r2.chunks[0U], (unsigned int *)&r.chunks[0U],
(unsigned int *)&r4.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r4.chunks[0U], 21U,
(unsigned int *)&r5.chunks[0U]);
sMultiWordShr((unsigned int *)&r5.chunks[0U], 1U,
(unsigned int *)&r6.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r6.chunks[0U], 21U,
(unsigned int *)&r7.chunks[0U]);
sMultiWordShr((unsigned int *)&r7.chunks[0U], 15U,
(unsigned int *)&r8.chunks[0U]);
i = N2 + yIdx;
y[i].re = (short)MultiWord2sLong((unsigned int *)&r8.chunks[0U]);
y[i].im = 0;
}
sLong2MultiWord(y[yIdx].re << 15, (unsigned int *)&r1.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r1.chunks[0U], 21U,
(unsigned int *)&r.chunks[0U]);
sLong2MultiWord(y[yIdx].im << 15, (unsigned int *)&r3.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r3.chunks[0U], 21U,
(unsigned int *)&r1.chunks[0U]);
MultiWordAdd((unsigned int *)&r.chunks[0U], (unsigned int *)&r1.chunks[0U],
(unsigned int *)&r2.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r2.chunks[0U], 21U,
(unsigned int *)&r4.chunks[0U]);
sMultiWordShr((unsigned int *)&r4.chunks[0U], 1U,
(unsigned int *)&r5.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r5.chunks[0U], 21U,
(unsigned int *)&r6.chunks[0U]);
sMultiWordShr((unsigned int *)&r6.chunks[0U], 15U,
(unsigned int *)&r7.chunks[0U]);
y[yIdx].re = (short)MultiWord2sLong((unsigned int *)&r7.chunks[0U]);
y[yIdx].im = 0;
tempOut0Re_tmp = twiddleStep;
for (ix = 1; ix < N4; ix++) {
int i1;
int i2;
int i3;
int i4;
int i5;
int i6;
short cTemp_im;
short cTemp_re;
i = ix + yIdx;
i1 = y[i].re << 15;
sLong2MultiWord(i1, (unsigned int *)&r3.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r3.chunks[0U], 21U,
(unsigned int *)&r1.chunks[0U]);
i2 = (N2 – ix) + yIdx;
i3 = y[i2].re << 15;
sLong2MultiWord(i3, (unsigned int *)&r9.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r9.chunks[0U], 21U,
(unsigned int *)&r3.chunks[0U]);
MultiWordAdd((unsigned int *)&r1.chunks[0U], (unsigned int *)&r3.chunks[0U],
(unsigned int *)&r.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r.chunks[0U], 21U,
(unsigned int *)&r2.chunks[0U]);
sMultiWordShr((unsigned int *)&r2.chunks[0U], 2U,
(unsigned int *)&r4.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r4.chunks[0U], 21U,
(unsigned int *)&r5.chunks[0U]);
sMultiWordShr((unsigned int *)&r5.chunks[0U], 15U,
(unsigned int *)&r6.chunks[0U]);
tempOut0Re = (short)MultiWord2sLong((unsigned int *)&r6.chunks[0U]);
i4 = y[i].im << 15;
sLong2MultiWord(i4, (unsigned int *)&r9.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r9.chunks[0U], 21U,
(unsigned int *)&r3.chunks[0U]);
i5 = y[i2].im << 15;
sLong2MultiWord(i5, (unsigned int *)&r10.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r10.chunks[0U], 21U,
(unsigned int *)&r9.chunks[0U]);
MultiWordSub((unsigned int *)&r3.chunks[0U], (unsigned int *)&r9.chunks[0U],
(unsigned int *)&r1.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r1.chunks[0U], 21U,
(unsigned int *)&r.chunks[0U]);
sMultiWordShr((unsigned int *)&r.chunks[0U], 2U,
(unsigned int *)&r2.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r2.chunks[0U], 21U,
(unsigned int *)&r4.chunks[0U]);
sMultiWordShr((unsigned int *)&r4.chunks[0U], 15U,
(unsigned int *)&r5.chunks[0U]);
tempOut0Im = (short)MultiWord2sLong((unsigned int *)&r5.chunks[0U]);
sLong2MultiWord(i4, (unsigned int *)&r10.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r10.chunks[0U], 21U,
(unsigned int *)&r9.chunks[0U]);
sLong2MultiWord(i5, (unsigned int *)&r11.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r11.chunks[0U], 21U,
(unsigned int *)&r10.chunks[0U]);
MultiWordAdd((unsigned int *)&r9.chunks[0U],
(unsigned int *)&r10.chunks[0U],
(unsigned int *)&r3.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r3.chunks[0U], 21U,
(unsigned int *)&r1.chunks[0U]);
sMultiWordShr((unsigned int *)&r1.chunks[0U], 2U,
(unsigned int *)&r.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r.chunks[0U], 21U,
(unsigned int *)&r2.chunks[0U]);
sMultiWordShr((unsigned int *)&r2.chunks[0U], 15U,
(unsigned int *)&r4.chunks[0U]);
y[i].re = (short)MultiWord2sLong((unsigned int *)&r4.chunks[0U]);
sLong2MultiWord(i3, (unsigned int *)&r11.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r11.chunks[0U], 21U,
(unsigned int *)&r10.chunks[0U]);
sLong2MultiWord(i1, (unsigned int *)&r12.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r12.chunks[0U], 21U,
(unsigned int *)&r11.chunks[0U]);
MultiWordSub((unsigned int *)&r10.chunks[0U],
(unsigned int *)&r11.chunks[0U],
(unsigned int *)&r9.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r9.chunks[0U], 21U,
(unsigned int *)&r3.chunks[0U]);
sMultiWordShr((unsigned int *)&r3.chunks[0U], 2U,
(unsigned int *)&r1.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r1.chunks[0U], 21U,
(unsigned int *)&r.chunks[0U]);
sMultiWordShr((unsigned int *)&r.chunks[0U], 15U,
(unsigned int *)&r2.chunks[0U]);
y[i].im = (short)MultiWord2sLong((unsigned int *)&r2.chunks[0U]);
i1 = twiddleTable[tempOut0Re_tmp + N4 * twiddleStep];
i3 = y[i].re;
sLong2MultiWord(i1 * i3, (unsigned int *)&r11.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r11.chunks[0U], 22U,
(unsigned int *)&r10.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r10.chunks[0U], 21U,
(unsigned int *)&r9.chunks[0U]);
i4 = y[i].im;
i5 = twiddleTable[tempOut0Re_tmp + N2 * twiddleStep];
sLong2MultiWord(i5 * i4, (unsigned int *)&r12.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r12.chunks[0U], 22U,
(unsigned int *)&r11.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r11.chunks[0U], 21U,
(unsigned int *)&r10.chunks[0U]);
MultiWordSub((unsigned int *)&r9.chunks[0U],
(unsigned int *)&r10.chunks[0U],
(unsigned int *)&r3.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r3.chunks[0U], 21U,
(unsigned int *)&r1.chunks[0U]);
sMultiWordShr((unsigned int *)&r1.chunks[0U], 15U,
(unsigned int *)&r.chunks[0U]);
cTemp_re = (short)MultiWord2sLong((unsigned int *)&r.chunks[0U]);
sLong2MultiWord(i1 * i4, (unsigned int *)&r12.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r12.chunks[0U], 22U,
(unsigned int *)&r11.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r11.chunks[0U], 21U,
(unsigned int *)&r10.chunks[0U]);
sLong2MultiWord(i5 * i3, (unsigned int *)&r13.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r13.chunks[0U], 22U,
(unsigned int *)&r12.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r12.chunks[0U], 21U,
(unsigned int *)&r11.chunks[0U]);
MultiWordAdd((unsigned int *)&r10.chunks[0U],
(unsigned int *)&r11.chunks[0U],
(unsigned int *)&r9.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r9.chunks[0U], 21U,
(unsigned int *)&r3.chunks[0U]);
sMultiWordShr((unsigned int *)&r3.chunks[0U], 15U,
(unsigned int *)&r1.chunks[0U]);
cTemp_im = (short)MultiWord2sLong((unsigned int *)&r1.chunks[0U]);
i1 = tempOut0Re << 15;
sLong2MultiWord(i1, (unsigned int *)&r12.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r12.chunks[0U], 21U,
(unsigned int *)&r11.chunks[0U]);
i3 = cTemp_re << 15;
sLong2MultiWord(i3, (unsigned int *)&r13.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r13.chunks[0U], 21U,
(unsigned int *)&r12.chunks[0U]);
MultiWordAdd((unsigned int *)&r11.chunks[0U],
(unsigned int *)&r12.chunks[0U],
(unsigned int *)&r10.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r10.chunks[0U], 21U,
(unsigned int *)&r9.chunks[0U]);
sMultiWordShr((unsigned int *)&r9.chunks[0U], 15U,
(unsigned int *)&r3.chunks[0U]);
y[i].re = (short)MultiWord2sLong((unsigned int *)&r3.chunks[0U]);
i4 = tempOut0Im << 15;
sLong2MultiWord(i4, (unsigned int *)&r13.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r13.chunks[0U], 21U,
(unsigned int *)&r12.chunks[0U]);
i5 = cTemp_im << 15;
sLong2MultiWord(i5, (unsigned int *)&r14.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r14.chunks[0U], 21U,
(unsigned int *)&r13.chunks[0U]);
MultiWordAdd((unsigned int *)&r12.chunks[0U],
(unsigned int *)&r13.chunks[0U],
(unsigned int *)&r11.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r11.chunks[0U], 21U,
(unsigned int *)&r10.chunks[0U]);
sMultiWordShr((unsigned int *)&r10.chunks[0U], 15U,
(unsigned int *)&r9.chunks[0U]);
y[i].im = (short)MultiWord2sLong((unsigned int *)&r9.chunks[0U]);
i6 = (nRows – ix) + yIdx;
y[i6].re = y[i].re;
y[i6].im = (short)-y[i].im;
sLong2MultiWord(i1, (unsigned int *)&r14.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r14.chunks[0U], 21U,
(unsigned int *)&r13.chunks[0U]);
sLong2MultiWord(i3, (unsigned int *)&r15.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r15.chunks[0U], 21U,
(unsigned int *)&r14.chunks[0U]);
MultiWordSub((unsigned int *)&r13.chunks[0U],
(unsigned int *)&r14.chunks[0U],
(unsigned int *)&r12.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r12.chunks[0U], 21U,
(unsigned int *)&r11.chunks[0U]);
sMultiWordShr((unsigned int *)&r11.chunks[0U], 15U,
(unsigned int *)&r10.chunks[0U]);
i = (N2 + ix) + yIdx;
y[i].re = (short)MultiWord2sLong((unsigned int *)&r10.chunks[0U]);
sLong2MultiWord(i4, (unsigned int *)&r15.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r15.chunks[0U], 21U,
(unsigned int *)&r14.chunks[0U]);
sLong2MultiWord(i5, (unsigned int *)&r16.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r16.chunks[0U], 21U,
(unsigned int *)&r15.chunks[0U]);
MultiWordSub((unsigned int *)&r14.chunks[0U],
(unsigned int *)&r15.chunks[0U],
(unsigned int *)&r13.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r13.chunks[0U], 21U,
(unsigned int *)&r12.chunks[0U]);
sMultiWordShr((unsigned int *)&r12.chunks[0U], 15U,
(unsigned int *)&r11.chunks[0U]);
y[i].im = (short)MultiWord2sLong((unsigned int *)&r11.chunks[0U]);
y[i2].re = y[i].re;
y[i2].im = (short)-y[i].im;
tempOut0Re_tmp += twiddleStep;
}
}
Is there a way to self-generate code that operates on 16-bit data and remove the Multiword functions? Hello everyone, I’m new to MATLAB and MATLAB coder. I’m trying to self-generate a code for a STM32F446RE microcontroller with MATLAB coder to use it on STM32CubeIDE. The code that I’m trying to generate is an FFT of fixed-point data. I’m using the dsp.FFT function with a 16-bit world length because it supports fixed-point.
This is my MATLAB script
s = readmatrix("FFT_data.txt");
c = fi(s,1,16);
q = fourier(c);
This is my MATLAB function
function [Y] = fourier(X)
ft = dsp.FFT("FFTLengthSource","Property","Normalize",true,"FFTLength",1024);
Y = abs(ft(X));
end
The program is working on my Micro, but it’s a little bit too slow. I think the problem is that the C code generated by MATLAB uses 64-bit Multiword functions despite the data being 16 bits long in MATLAB.
static void c_MWDSPCG_FFT_DblLen_Nrm_YCs16n(cint16_T y[], int nChans, int nRows,
const short twiddleTable[],
int twiddleStep)
{
int64m_T r;
int64m_T r1;
int64m_T r10;
int64m_T r11;
int64m_T r12;
int64m_T r13;
int64m_T r14;
int64m_T r15;
int64m_T r16;
int64m_T r2;
int64m_T r3;
int64m_T r4;
int64m_T r5;
int64m_T r6;
int64m_T r7;
int64m_T r8;
int64m_T r9;
int N2;
int N4;
int i;
int ix;
int tempOut0Re_tmp;
int yIdx;
short tempOut0Im;
short tempOut0Re;
/* In-place "double-length" data recovery
Table-based mem-optimized twiddle computation
Used to recover linear-ordered length-N point complex FFT result
from a linear-ordered complex length-N/2 point FFT, performed
on N interleaved real values.
*/
N2 = nRows >> 1;
N4 = N2 >> 1;
yIdx = nRows * (nChans – 1);
if (nRows > 2) {
tempOut0Re_tmp = N4 + yIdx;
tempOut0Re = (short)(y[tempOut0Re_tmp].re >> 1);
tempOut0Im = (short)(y[tempOut0Re_tmp].im >> 1);
i = (N2 + N4) + yIdx;
y[i].re = tempOut0Re;
y[i].im = tempOut0Im;
y[tempOut0Re_tmp].re = tempOut0Re;
y[tempOut0Re_tmp].im = (short)-tempOut0Im;
}
if (nRows > 1) {
sLong2MultiWord(y[yIdx].re << 15, (unsigned int *)&r.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r.chunks[0U], 21U,
(unsigned int *)&r2.chunks[0U]);
sLong2MultiWord(y[yIdx].im << 15, (unsigned int *)&r1.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r1.chunks[0U], 21U,
(unsigned int *)&r.chunks[0U]);
MultiWordSub((unsigned int *)&r2.chunks[0U], (unsigned int *)&r.chunks[0U],
(unsigned int *)&r4.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r4.chunks[0U], 21U,
(unsigned int *)&r5.chunks[0U]);
sMultiWordShr((unsigned int *)&r5.chunks[0U], 1U,
(unsigned int *)&r6.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r6.chunks[0U], 21U,
(unsigned int *)&r7.chunks[0U]);
sMultiWordShr((unsigned int *)&r7.chunks[0U], 15U,
(unsigned int *)&r8.chunks[0U]);
i = N2 + yIdx;
y[i].re = (short)MultiWord2sLong((unsigned int *)&r8.chunks[0U]);
y[i].im = 0;
}
sLong2MultiWord(y[yIdx].re << 15, (unsigned int *)&r1.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r1.chunks[0U], 21U,
(unsigned int *)&r.chunks[0U]);
sLong2MultiWord(y[yIdx].im << 15, (unsigned int *)&r3.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r3.chunks[0U], 21U,
(unsigned int *)&r1.chunks[0U]);
MultiWordAdd((unsigned int *)&r.chunks[0U], (unsigned int *)&r1.chunks[0U],
(unsigned int *)&r2.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r2.chunks[0U], 21U,
(unsigned int *)&r4.chunks[0U]);
sMultiWordShr((unsigned int *)&r4.chunks[0U], 1U,
(unsigned int *)&r5.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r5.chunks[0U], 21U,
(unsigned int *)&r6.chunks[0U]);
sMultiWordShr((unsigned int *)&r6.chunks[0U], 15U,
(unsigned int *)&r7.chunks[0U]);
y[yIdx].re = (short)MultiWord2sLong((unsigned int *)&r7.chunks[0U]);
y[yIdx].im = 0;
tempOut0Re_tmp = twiddleStep;
for (ix = 1; ix < N4; ix++) {
int i1;
int i2;
int i3;
int i4;
int i5;
int i6;
short cTemp_im;
short cTemp_re;
i = ix + yIdx;
i1 = y[i].re << 15;
sLong2MultiWord(i1, (unsigned int *)&r3.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r3.chunks[0U], 21U,
(unsigned int *)&r1.chunks[0U]);
i2 = (N2 – ix) + yIdx;
i3 = y[i2].re << 15;
sLong2MultiWord(i3, (unsigned int *)&r9.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r9.chunks[0U], 21U,
(unsigned int *)&r3.chunks[0U]);
MultiWordAdd((unsigned int *)&r1.chunks[0U], (unsigned int *)&r3.chunks[0U],
(unsigned int *)&r.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r.chunks[0U], 21U,
(unsigned int *)&r2.chunks[0U]);
sMultiWordShr((unsigned int *)&r2.chunks[0U], 2U,
(unsigned int *)&r4.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r4.chunks[0U], 21U,
(unsigned int *)&r5.chunks[0U]);
sMultiWordShr((unsigned int *)&r5.chunks[0U], 15U,
(unsigned int *)&r6.chunks[0U]);
tempOut0Re = (short)MultiWord2sLong((unsigned int *)&r6.chunks[0U]);
i4 = y[i].im << 15;
sLong2MultiWord(i4, (unsigned int *)&r9.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r9.chunks[0U], 21U,
(unsigned int *)&r3.chunks[0U]);
i5 = y[i2].im << 15;
sLong2MultiWord(i5, (unsigned int *)&r10.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r10.chunks[0U], 21U,
(unsigned int *)&r9.chunks[0U]);
MultiWordSub((unsigned int *)&r3.chunks[0U], (unsigned int *)&r9.chunks[0U],
(unsigned int *)&r1.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r1.chunks[0U], 21U,
(unsigned int *)&r.chunks[0U]);
sMultiWordShr((unsigned int *)&r.chunks[0U], 2U,
(unsigned int *)&r2.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r2.chunks[0U], 21U,
(unsigned int *)&r4.chunks[0U]);
sMultiWordShr((unsigned int *)&r4.chunks[0U], 15U,
(unsigned int *)&r5.chunks[0U]);
tempOut0Im = (short)MultiWord2sLong((unsigned int *)&r5.chunks[0U]);
sLong2MultiWord(i4, (unsigned int *)&r10.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r10.chunks[0U], 21U,
(unsigned int *)&r9.chunks[0U]);
sLong2MultiWord(i5, (unsigned int *)&r11.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r11.chunks[0U], 21U,
(unsigned int *)&r10.chunks[0U]);
MultiWordAdd((unsigned int *)&r9.chunks[0U],
(unsigned int *)&r10.chunks[0U],
(unsigned int *)&r3.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r3.chunks[0U], 21U,
(unsigned int *)&r1.chunks[0U]);
sMultiWordShr((unsigned int *)&r1.chunks[0U], 2U,
(unsigned int *)&r.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r.chunks[0U], 21U,
(unsigned int *)&r2.chunks[0U]);
sMultiWordShr((unsigned int *)&r2.chunks[0U], 15U,
(unsigned int *)&r4.chunks[0U]);
y[i].re = (short)MultiWord2sLong((unsigned int *)&r4.chunks[0U]);
sLong2MultiWord(i3, (unsigned int *)&r11.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r11.chunks[0U], 21U,
(unsigned int *)&r10.chunks[0U]);
sLong2MultiWord(i1, (unsigned int *)&r12.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r12.chunks[0U], 21U,
(unsigned int *)&r11.chunks[0U]);
MultiWordSub((unsigned int *)&r10.chunks[0U],
(unsigned int *)&r11.chunks[0U],
(unsigned int *)&r9.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r9.chunks[0U], 21U,
(unsigned int *)&r3.chunks[0U]);
sMultiWordShr((unsigned int *)&r3.chunks[0U], 2U,
(unsigned int *)&r1.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r1.chunks[0U], 21U,
(unsigned int *)&r.chunks[0U]);
sMultiWordShr((unsigned int *)&r.chunks[0U], 15U,
(unsigned int *)&r2.chunks[0U]);
y[i].im = (short)MultiWord2sLong((unsigned int *)&r2.chunks[0U]);
i1 = twiddleTable[tempOut0Re_tmp + N4 * twiddleStep];
i3 = y[i].re;
sLong2MultiWord(i1 * i3, (unsigned int *)&r11.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r11.chunks[0U], 22U,
(unsigned int *)&r10.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r10.chunks[0U], 21U,
(unsigned int *)&r9.chunks[0U]);
i4 = y[i].im;
i5 = twiddleTable[tempOut0Re_tmp + N2 * twiddleStep];
sLong2MultiWord(i5 * i4, (unsigned int *)&r12.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r12.chunks[0U], 22U,
(unsigned int *)&r11.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r11.chunks[0U], 21U,
(unsigned int *)&r10.chunks[0U]);
MultiWordSub((unsigned int *)&r9.chunks[0U],
(unsigned int *)&r10.chunks[0U],
(unsigned int *)&r3.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r3.chunks[0U], 21U,
(unsigned int *)&r1.chunks[0U]);
sMultiWordShr((unsigned int *)&r1.chunks[0U], 15U,
(unsigned int *)&r.chunks[0U]);
cTemp_re = (short)MultiWord2sLong((unsigned int *)&r.chunks[0U]);
sLong2MultiWord(i1 * i4, (unsigned int *)&r12.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r12.chunks[0U], 22U,
(unsigned int *)&r11.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r11.chunks[0U], 21U,
(unsigned int *)&r10.chunks[0U]);
sLong2MultiWord(i5 * i3, (unsigned int *)&r13.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r13.chunks[0U], 22U,
(unsigned int *)&r12.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r12.chunks[0U], 21U,
(unsigned int *)&r11.chunks[0U]);
MultiWordAdd((unsigned int *)&r10.chunks[0U],
(unsigned int *)&r11.chunks[0U],
(unsigned int *)&r9.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r9.chunks[0U], 21U,
(unsigned int *)&r3.chunks[0U]);
sMultiWordShr((unsigned int *)&r3.chunks[0U], 15U,
(unsigned int *)&r1.chunks[0U]);
cTemp_im = (short)MultiWord2sLong((unsigned int *)&r1.chunks[0U]);
i1 = tempOut0Re << 15;
sLong2MultiWord(i1, (unsigned int *)&r12.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r12.chunks[0U], 21U,
(unsigned int *)&r11.chunks[0U]);
i3 = cTemp_re << 15;
sLong2MultiWord(i3, (unsigned int *)&r13.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r13.chunks[0U], 21U,
(unsigned int *)&r12.chunks[0U]);
MultiWordAdd((unsigned int *)&r11.chunks[0U],
(unsigned int *)&r12.chunks[0U],
(unsigned int *)&r10.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r10.chunks[0U], 21U,
(unsigned int *)&r9.chunks[0U]);
sMultiWordShr((unsigned int *)&r9.chunks[0U], 15U,
(unsigned int *)&r3.chunks[0U]);
y[i].re = (short)MultiWord2sLong((unsigned int *)&r3.chunks[0U]);
i4 = tempOut0Im << 15;
sLong2MultiWord(i4, (unsigned int *)&r13.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r13.chunks[0U], 21U,
(unsigned int *)&r12.chunks[0U]);
i5 = cTemp_im << 15;
sLong2MultiWord(i5, (unsigned int *)&r14.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r14.chunks[0U], 21U,
(unsigned int *)&r13.chunks[0U]);
MultiWordAdd((unsigned int *)&r12.chunks[0U],
(unsigned int *)&r13.chunks[0U],
(unsigned int *)&r11.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r11.chunks[0U], 21U,
(unsigned int *)&r10.chunks[0U]);
sMultiWordShr((unsigned int *)&r10.chunks[0U], 15U,
(unsigned int *)&r9.chunks[0U]);
y[i].im = (short)MultiWord2sLong((unsigned int *)&r9.chunks[0U]);
i6 = (nRows – ix) + yIdx;
y[i6].re = y[i].re;
y[i6].im = (short)-y[i].im;
sLong2MultiWord(i1, (unsigned int *)&r14.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r14.chunks[0U], 21U,
(unsigned int *)&r13.chunks[0U]);
sLong2MultiWord(i3, (unsigned int *)&r15.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r15.chunks[0U], 21U,
(unsigned int *)&r14.chunks[0U]);
MultiWordSub((unsigned int *)&r13.chunks[0U],
(unsigned int *)&r14.chunks[0U],
(unsigned int *)&r12.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r12.chunks[0U], 21U,
(unsigned int *)&r11.chunks[0U]);
sMultiWordShr((unsigned int *)&r11.chunks[0U], 15U,
(unsigned int *)&r10.chunks[0U]);
i = (N2 + ix) + yIdx;
y[i].re = (short)MultiWord2sLong((unsigned int *)&r10.chunks[0U]);
sLong2MultiWord(i4, (unsigned int *)&r15.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r15.chunks[0U], 21U,
(unsigned int *)&r14.chunks[0U]);
sLong2MultiWord(i5, (unsigned int *)&r16.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r16.chunks[0U], 21U,
(unsigned int *)&r15.chunks[0U]);
MultiWordSub((unsigned int *)&r14.chunks[0U],
(unsigned int *)&r15.chunks[0U],
(unsigned int *)&r13.chunks[0U]);
MultiWordSignedWrap((unsigned int *)&r13.chunks[0U], 21U,
(unsigned int *)&r12.chunks[0U]);
sMultiWordShr((unsigned int *)&r12.chunks[0U], 15U,
(unsigned int *)&r11.chunks[0U]);
y[i].im = (short)MultiWord2sLong((unsigned int *)&r11.chunks[0U]);
y[i2].re = y[i].re;
y[i2].im = (short)-y[i].im;
tempOut0Re_tmp += twiddleStep;
}
}
Is there a way to self-generate code that operates on 16-bit data and remove the Multiword functions? fft, dsp.fft, matlab, matlab coder, embedded coder, stm32, fixed-point MATLAB Answers — New Questions
Gear teeth dimension identification
Hi, I am doing the project where i need to measure the Gear Teeth dimension like (Teeth width ,thickness, pitch error, PCD) acquired images from Machine vision system.I tried with Simulink script. But the results are not correct.Request you to share your views and coding on how to estimate the gear teeth dimension.Hi, I am doing the project where i need to measure the Gear Teeth dimension like (Teeth width ,thickness, pitch error, PCD) acquired images from Machine vision system.I tried with Simulink script. But the results are not correct.Request you to share your views and coding on how to estimate the gear teeth dimension. Hi, I am doing the project where i need to measure the Gear Teeth dimension like (Teeth width ,thickness, pitch error, PCD) acquired images from Machine vision system.I tried with Simulink script. But the results are not correct.Request you to share your views and coding on how to estimate the gear teeth dimension. gear teeth dimension measurement MATLAB Answers — New Questions
How to set register address with devmem on ZCU216?
Currently we are using ZCU216 with matlab/Simulink to develop logics. We created a register and its name is "Delay1Reg" and address is 0x0488 (see red box in attached photo) . When we use matlab terminal and write "writePort(hFPGA, "Delay1Reg",255);". This works very well.
Now we want to use an ssh connection and write the register from the embedded linux system e.g. "devmem 0x0488". But this looks like it’s reading something wrong. Do you have any ideas what is this register’saddress is using the devmem command?
I also have attached vivado’smemory map.Currently we are using ZCU216 with matlab/Simulink to develop logics. We created a register and its name is "Delay1Reg" and address is 0x0488 (see red box in attached photo) . When we use matlab terminal and write "writePort(hFPGA, "Delay1Reg",255);". This works very well.
Now we want to use an ssh connection and write the register from the embedded linux system e.g. "devmem 0x0488". But this looks like it’s reading something wrong. Do you have any ideas what is this register’saddress is using the devmem command?
I also have attached vivado’smemory map. Currently we are using ZCU216 with matlab/Simulink to develop logics. We created a register and its name is "Delay1Reg" and address is 0x0488 (see red box in attached photo) . When we use matlab terminal and write "writePort(hFPGA, "Delay1Reg",255);". This works very well.
Now we want to use an ssh connection and write the register from the embedded linux system e.g. "devmem 0x0488". But this looks like it’s reading something wrong. Do you have any ideas what is this register’saddress is using the devmem command?
I also have attached vivado’smemory map. devmem, zcu216, register address MATLAB Answers — New Questions
MATLAB:mex:ErrInvalidMEXFile : Invalid MEX-file: The specified procedure could not be found.
I have been facing a vicious Mex runtime error for weeks now that is significantly wasting my time without getting anywhere to resolve it. The error message is in the title of this question. Surprisingly, the compiled Mex files run smoothly under MATLAB 2023b and 2024a, but not under the older versions of MATLAB. Even stranger, I am compiling the Mex file with MATLAB 2022a, and removing all other matlab installations from the paths, and still getting this runtime error with the same MATLAB 2022a, but not with other newer installations.
What could be going wrong? How can I trace the root of this persistent error that sometimes changes to slightly different messages?I have been facing a vicious Mex runtime error for weeks now that is significantly wasting my time without getting anywhere to resolve it. The error message is in the title of this question. Surprisingly, the compiled Mex files run smoothly under MATLAB 2023b and 2024a, but not under the older versions of MATLAB. Even stranger, I am compiling the Mex file with MATLAB 2022a, and removing all other matlab installations from the paths, and still getting this runtime error with the same MATLAB 2022a, but not with other newer installations.
What could be going wrong? How can I trace the root of this persistent error that sometimes changes to slightly different messages? I have been facing a vicious Mex runtime error for weeks now that is significantly wasting my time without getting anywhere to resolve it. The error message is in the title of this question. Surprisingly, the compiled Mex files run smoothly under MATLAB 2023b and 2024a, but not under the older versions of MATLAB. Even stranger, I am compiling the Mex file with MATLAB 2022a, and removing all other matlab installations from the paths, and still getting this runtime error with the same MATLAB 2022a, but not with other newer installations.
What could be going wrong? How can I trace the root of this persistent error that sometimes changes to slightly different messages? matlab, mex, mex compiler MATLAB Answers — New Questions
anyone correct the below code for solving energy, vorticity eqn using the alternative direct implicit fdm method for which i should get the values of velocitiy at each grid i
% MATLAB code to solve the coupled temperature and vorticity equations using ADI FDM
% Parameters
Pr = 0.733; % Prandtl number
A = 1; % Length of the domain in X direction
Ha = 10; % Hartmann number (can change this to study its influence)
Gr = 2e4; % Grashof number
Nx = 41; % Number of grid points in X direction
Ny = 41; % Number of grid points in Y direction
dx = A/(Nx-1); % Grid spacing in X
dy = 1/(Ny-1); % Grid spacing in Y
dt = 0.001; % Time step size
max_iter = 1000; % Maximum iterations for time-stepping
% Initialize arrays
T = rand(Nx, Ny); % Temperature field
zeta = rand(Nx, Ny); % Vorticity field
Psi = rand(Nx, Ny); % Stream function
U = rand(Nx, Ny); % X-component of velocity
V = rand(Nx, Ny); % Y-component of velocity
% Boundary conditions for Psi and T
T(:,1) = -1; % At Y = 0, T = -1
T(:,end) = 1; % At Y = 1, T = 1
Psi(:,1) = 0; Psi(:,end) = 0; % Stream function at Y boundaries
Psi(1,:) = 0; Psi(end,:) = 0; % Stream function at X boundaries
% Time-stepping loop
for iter = 1:max_iter
% Compute velocities U and V from stream function Psi
for i = 2:Nx-1
for j = 2:Ny-1
U(i,j) = (Psi(i,j+1) – Psi(i,j-1))/(2*dy); % U = dPsi/dY
V(i,j) = -(Psi(i+1,j) – Psi(i-1,j))/(2*dx); % V = -dPsi/dX
end
end
% Solve for temperature T using ADI method (X-direction first)
for j = 2:Ny-1
for i = 2:Nx-1
T(i,j) = T(i,j) + dt * ( …
-(U(i,j) * (T(i+1,j) – T(i-1,j))/(2*dx)) – … % Advection term (X)
(V(i,j) * (T(i,j+1) – T(i,j-1))/(2*dy)) + … % Advection term (Y)
(1/Pr) * ((T(i+1,j) – 2*T(i,j) + T(i-1,j))/(dx^2) + … % Diffusion (X)
(T(i,j+1) – 2*T(i,j) + T(i,j-1))/(dy^2)) ); % Diffusion (Y)
end
end
% Solve for vorticity zeta using ADI method (Y-direction next)
for j = 2:Ny-1
for i = 2:Nx-1
zeta(i,j) = zeta(i,j) + dt * ( …
-(U(i,j) * (zeta(i+1,j) – zeta(i-1,j))/(2*dx)) – … % Advection (X)
(V(i,j) * (zeta(i,j+1) – zeta(i,j-1))/(2*dy)) + … % Advection (Y)
(Gr/2) * (T(i,j+1) – T(i,j))/(dy) + … % Buoyancy effect
((zeta(i+1,j) – 2*zeta(i,j) + zeta(i-1,j))/(dx^2) + … % Diffusion (X)
(zeta(i,j+1) – 2*zeta(i,j) + zeta(i,j-1))/(dy^2)) – … % Diffusion (Y)
Ha^2 * (V(i+1,j) – V(i-1,j))/(2*dx) ); % Hartmann term
end
end
% Enforce boundary conditions on Psi
Psi(:,1) = 0; Psi(:,end) = 0; % At Y boundaries
Psi(1,:) = 0; Psi(end,:) = 0; % At X boundaries
% Enforce boundary conditions on T
T(:,1) = -1; % At Y = 0
T(:,end) = 1; % At Y = 1
T(1,:) = T(2,:); % Neumann BC at X = 0
T(end,:) = T(end-1,:); % Neumann BC at X = A
% Optional: Display or store the solution at certain intervals
if mod(iter, 100) == 0
fprintf(‘Iteration %dn’, iter);
end
end
% Final solution display – Single graph with subplots for T and Psi
figure;
subplot(1,2,1);
contourf(linspace(0,A,Nx), linspace(0,1,Ny), T’, 20); colorbar;
title(‘Temperature Distribution T’);
xlabel(‘X’); ylabel(‘Y’);
subplot(1,2,2);
streamslice(linspace(0,A,Nx), linspace(0,1,Ny), U’, V’); % Plot streamlines for velocity
title(‘Streamlines – Fluid Flow’);
xlabel(‘X’); ylabel(‘Y’);% MATLAB code to solve the coupled temperature and vorticity equations using ADI FDM
% Parameters
Pr = 0.733; % Prandtl number
A = 1; % Length of the domain in X direction
Ha = 10; % Hartmann number (can change this to study its influence)
Gr = 2e4; % Grashof number
Nx = 41; % Number of grid points in X direction
Ny = 41; % Number of grid points in Y direction
dx = A/(Nx-1); % Grid spacing in X
dy = 1/(Ny-1); % Grid spacing in Y
dt = 0.001; % Time step size
max_iter = 1000; % Maximum iterations for time-stepping
% Initialize arrays
T = rand(Nx, Ny); % Temperature field
zeta = rand(Nx, Ny); % Vorticity field
Psi = rand(Nx, Ny); % Stream function
U = rand(Nx, Ny); % X-component of velocity
V = rand(Nx, Ny); % Y-component of velocity
% Boundary conditions for Psi and T
T(:,1) = -1; % At Y = 0, T = -1
T(:,end) = 1; % At Y = 1, T = 1
Psi(:,1) = 0; Psi(:,end) = 0; % Stream function at Y boundaries
Psi(1,:) = 0; Psi(end,:) = 0; % Stream function at X boundaries
% Time-stepping loop
for iter = 1:max_iter
% Compute velocities U and V from stream function Psi
for i = 2:Nx-1
for j = 2:Ny-1
U(i,j) = (Psi(i,j+1) – Psi(i,j-1))/(2*dy); % U = dPsi/dY
V(i,j) = -(Psi(i+1,j) – Psi(i-1,j))/(2*dx); % V = -dPsi/dX
end
end
% Solve for temperature T using ADI method (X-direction first)
for j = 2:Ny-1
for i = 2:Nx-1
T(i,j) = T(i,j) + dt * ( …
-(U(i,j) * (T(i+1,j) – T(i-1,j))/(2*dx)) – … % Advection term (X)
(V(i,j) * (T(i,j+1) – T(i,j-1))/(2*dy)) + … % Advection term (Y)
(1/Pr) * ((T(i+1,j) – 2*T(i,j) + T(i-1,j))/(dx^2) + … % Diffusion (X)
(T(i,j+1) – 2*T(i,j) + T(i,j-1))/(dy^2)) ); % Diffusion (Y)
end
end
% Solve for vorticity zeta using ADI method (Y-direction next)
for j = 2:Ny-1
for i = 2:Nx-1
zeta(i,j) = zeta(i,j) + dt * ( …
-(U(i,j) * (zeta(i+1,j) – zeta(i-1,j))/(2*dx)) – … % Advection (X)
(V(i,j) * (zeta(i,j+1) – zeta(i,j-1))/(2*dy)) + … % Advection (Y)
(Gr/2) * (T(i,j+1) – T(i,j))/(dy) + … % Buoyancy effect
((zeta(i+1,j) – 2*zeta(i,j) + zeta(i-1,j))/(dx^2) + … % Diffusion (X)
(zeta(i,j+1) – 2*zeta(i,j) + zeta(i,j-1))/(dy^2)) – … % Diffusion (Y)
Ha^2 * (V(i+1,j) – V(i-1,j))/(2*dx) ); % Hartmann term
end
end
% Enforce boundary conditions on Psi
Psi(:,1) = 0; Psi(:,end) = 0; % At Y boundaries
Psi(1,:) = 0; Psi(end,:) = 0; % At X boundaries
% Enforce boundary conditions on T
T(:,1) = -1; % At Y = 0
T(:,end) = 1; % At Y = 1
T(1,:) = T(2,:); % Neumann BC at X = 0
T(end,:) = T(end-1,:); % Neumann BC at X = A
% Optional: Display or store the solution at certain intervals
if mod(iter, 100) == 0
fprintf(‘Iteration %dn’, iter);
end
end
% Final solution display – Single graph with subplots for T and Psi
figure;
subplot(1,2,1);
contourf(linspace(0,A,Nx), linspace(0,1,Ny), T’, 20); colorbar;
title(‘Temperature Distribution T’);
xlabel(‘X’); ylabel(‘Y’);
subplot(1,2,2);
streamslice(linspace(0,A,Nx), linspace(0,1,Ny), U’, V’); % Plot streamlines for velocity
title(‘Streamlines – Fluid Flow’);
xlabel(‘X’); ylabel(‘Y’); % MATLAB code to solve the coupled temperature and vorticity equations using ADI FDM
% Parameters
Pr = 0.733; % Prandtl number
A = 1; % Length of the domain in X direction
Ha = 10; % Hartmann number (can change this to study its influence)
Gr = 2e4; % Grashof number
Nx = 41; % Number of grid points in X direction
Ny = 41; % Number of grid points in Y direction
dx = A/(Nx-1); % Grid spacing in X
dy = 1/(Ny-1); % Grid spacing in Y
dt = 0.001; % Time step size
max_iter = 1000; % Maximum iterations for time-stepping
% Initialize arrays
T = rand(Nx, Ny); % Temperature field
zeta = rand(Nx, Ny); % Vorticity field
Psi = rand(Nx, Ny); % Stream function
U = rand(Nx, Ny); % X-component of velocity
V = rand(Nx, Ny); % Y-component of velocity
% Boundary conditions for Psi and T
T(:,1) = -1; % At Y = 0, T = -1
T(:,end) = 1; % At Y = 1, T = 1
Psi(:,1) = 0; Psi(:,end) = 0; % Stream function at Y boundaries
Psi(1,:) = 0; Psi(end,:) = 0; % Stream function at X boundaries
% Time-stepping loop
for iter = 1:max_iter
% Compute velocities U and V from stream function Psi
for i = 2:Nx-1
for j = 2:Ny-1
U(i,j) = (Psi(i,j+1) – Psi(i,j-1))/(2*dy); % U = dPsi/dY
V(i,j) = -(Psi(i+1,j) – Psi(i-1,j))/(2*dx); % V = -dPsi/dX
end
end
% Solve for temperature T using ADI method (X-direction first)
for j = 2:Ny-1
for i = 2:Nx-1
T(i,j) = T(i,j) + dt * ( …
-(U(i,j) * (T(i+1,j) – T(i-1,j))/(2*dx)) – … % Advection term (X)
(V(i,j) * (T(i,j+1) – T(i,j-1))/(2*dy)) + … % Advection term (Y)
(1/Pr) * ((T(i+1,j) – 2*T(i,j) + T(i-1,j))/(dx^2) + … % Diffusion (X)
(T(i,j+1) – 2*T(i,j) + T(i,j-1))/(dy^2)) ); % Diffusion (Y)
end
end
% Solve for vorticity zeta using ADI method (Y-direction next)
for j = 2:Ny-1
for i = 2:Nx-1
zeta(i,j) = zeta(i,j) + dt * ( …
-(U(i,j) * (zeta(i+1,j) – zeta(i-1,j))/(2*dx)) – … % Advection (X)
(V(i,j) * (zeta(i,j+1) – zeta(i,j-1))/(2*dy)) + … % Advection (Y)
(Gr/2) * (T(i,j+1) – T(i,j))/(dy) + … % Buoyancy effect
((zeta(i+1,j) – 2*zeta(i,j) + zeta(i-1,j))/(dx^2) + … % Diffusion (X)
(zeta(i,j+1) – 2*zeta(i,j) + zeta(i,j-1))/(dy^2)) – … % Diffusion (Y)
Ha^2 * (V(i+1,j) – V(i-1,j))/(2*dx) ); % Hartmann term
end
end
% Enforce boundary conditions on Psi
Psi(:,1) = 0; Psi(:,end) = 0; % At Y boundaries
Psi(1,:) = 0; Psi(end,:) = 0; % At X boundaries
% Enforce boundary conditions on T
T(:,1) = -1; % At Y = 0
T(:,end) = 1; % At Y = 1
T(1,:) = T(2,:); % Neumann BC at X = 0
T(end,:) = T(end-1,:); % Neumann BC at X = A
% Optional: Display or store the solution at certain intervals
if mod(iter, 100) == 0
fprintf(‘Iteration %dn’, iter);
end
end
% Final solution display – Single graph with subplots for T and Psi
figure;
subplot(1,2,1);
contourf(linspace(0,A,Nx), linspace(0,1,Ny), T’, 20); colorbar;
title(‘Temperature Distribution T’);
xlabel(‘X’); ylabel(‘Y’);
subplot(1,2,2);
streamslice(linspace(0,A,Nx), linspace(0,1,Ny), U’, V’); % Plot streamlines for velocity
title(‘Streamlines – Fluid Flow’);
xlabel(‘X’); ylabel(‘Y’); #cfd #fdm #adi MATLAB Answers — New Questions
get values from rapid accelerator build summary
I have a Simulink model I run routinely in rapid accelerator mode. The performance is great, but the build process sometimes encounters strange errors. To troubleshoot that, I would like my script to be able to extract information from the text build summary printed to the console. In particular, I want the three numbers from the line that says "1 of 16 models built (9 models already up to date)", so I can branch on them, like this:
try
rtp = Simulink.BlockDiagram.buildRapidAcceleratorTarget("ModelName")
catch ME
[built, total, done] = something(?);
if done < total
if built > 0
call_self_again()
else
do_something_different()
end
else
report_success_anyway()
end
end
What something can I use to tell me the values of built, total, and done?I have a Simulink model I run routinely in rapid accelerator mode. The performance is great, but the build process sometimes encounters strange errors. To troubleshoot that, I would like my script to be able to extract information from the text build summary printed to the console. In particular, I want the three numbers from the line that says "1 of 16 models built (9 models already up to date)", so I can branch on them, like this:
try
rtp = Simulink.BlockDiagram.buildRapidAcceleratorTarget("ModelName")
catch ME
[built, total, done] = something(?);
if done < total
if built > 0
call_self_again()
else
do_something_different()
end
else
report_success_anyway()
end
end
What something can I use to tell me the values of built, total, and done? I have a Simulink model I run routinely in rapid accelerator mode. The performance is great, but the build process sometimes encounters strange errors. To troubleshoot that, I would like my script to be able to extract information from the text build summary printed to the console. In particular, I want the three numbers from the line that says "1 of 16 models built (9 models already up to date)", so I can branch on them, like this:
try
rtp = Simulink.BlockDiagram.buildRapidAcceleratorTarget("ModelName")
catch ME
[built, total, done] = something(?);
if done < total
if built > 0
call_self_again()
else
do_something_different()
end
else
report_success_anyway()
end
end
What something can I use to tell me the values of built, total, and done? rapid accelerator, build summary MATLAB Answers — New Questions
troubleshoot rapid accelerator build process
I have a Simulink model I run routinely in rapid accelerator mode. My problem is the build process is strangely inconsistent. Sometimes, when I call Simulink.BlockDiagram.buildRapidAcceleratorTarget, everything goes smoothly, and builds all in one call. Other times, the same call only builds one sub-model at a time, and raises an error, but eventually succeeds when I up-arrow and hit return on the same build command, 16 times in a row. I am trying to figure out what Simulink and I are doing differently in the two cases, but I am just not seeing it.
My most recent clue is the file build times. When all is well, the overall build process from a blank slate takes about 5 minutes. When I look at the times from a build that went poorly, mexa64 files and slxc files are interleaved, one of each for each sub-model. When I look at the times from a build that went smoothly, all 16 mexa64 files are older than all of the slxc files.
Can anyone tell me what might be causing rapid accelerator to only sometimes build all the mexas first, or how I can convince it to do that every time?I have a Simulink model I run routinely in rapid accelerator mode. My problem is the build process is strangely inconsistent. Sometimes, when I call Simulink.BlockDiagram.buildRapidAcceleratorTarget, everything goes smoothly, and builds all in one call. Other times, the same call only builds one sub-model at a time, and raises an error, but eventually succeeds when I up-arrow and hit return on the same build command, 16 times in a row. I am trying to figure out what Simulink and I are doing differently in the two cases, but I am just not seeing it.
My most recent clue is the file build times. When all is well, the overall build process from a blank slate takes about 5 minutes. When I look at the times from a build that went poorly, mexa64 files and slxc files are interleaved, one of each for each sub-model. When I look at the times from a build that went smoothly, all 16 mexa64 files are older than all of the slxc files.
Can anyone tell me what might be causing rapid accelerator to only sometimes build all the mexas first, or how I can convince it to do that every time? I have a Simulink model I run routinely in rapid accelerator mode. My problem is the build process is strangely inconsistent. Sometimes, when I call Simulink.BlockDiagram.buildRapidAcceleratorTarget, everything goes smoothly, and builds all in one call. Other times, the same call only builds one sub-model at a time, and raises an error, but eventually succeeds when I up-arrow and hit return on the same build command, 16 times in a row. I am trying to figure out what Simulink and I are doing differently in the two cases, but I am just not seeing it.
My most recent clue is the file build times. When all is well, the overall build process from a blank slate takes about 5 minutes. When I look at the times from a build that went poorly, mexa64 files and slxc files are interleaved, one of each for each sub-model. When I look at the times from a build that went smoothly, all 16 mexa64 files are older than all of the slxc files.
Can anyone tell me what might be causing rapid accelerator to only sometimes build all the mexas first, or how I can convince it to do that every time? rapid accelerator MATLAB Answers — New Questions
Sampling time of matlab figure data
I have some real time experiment data saved in matlab figures. While saving the data in simulink scope, the sampling time was set as the inherited sampling time of ‘-1’. Now, I have those scope data saved as only matlab figures. Is it possible to change the sampling time of the saved data to 0.1? Without changing the scope settings and running the real time experiment again?I have some real time experiment data saved in matlab figures. While saving the data in simulink scope, the sampling time was set as the inherited sampling time of ‘-1’. Now, I have those scope data saved as only matlab figures. Is it possible to change the sampling time of the saved data to 0.1? Without changing the scope settings and running the real time experiment again? I have some real time experiment data saved in matlab figures. While saving the data in simulink scope, the sampling time was set as the inherited sampling time of ‘-1’. Now, I have those scope data saved as only matlab figures. Is it possible to change the sampling time of the saved data to 0.1? Without changing the scope settings and running the real time experiment again? simulink, sampling time matlab figure data MATLAB Answers — New Questions
Only the first if-statement block executes,
Hi there!
I wrote an if-statement, followed by an if-else statement, followed by another if-else statement, followed by the end keyword.
The goal is to use different formulas for different values of alpha, something like the below.
However, it seems that only the first if-statement block executes — for all possible values of alpha, even when alpha already exceeds that interval.
Where is my mistake? Thanks in advance.
alpha = linspace(0,3*pi/2,1000)
if 0 <= alpha <= pi/2
vx = …;
vy = …;
elseif pi/2 < alpha <= pi
vx = …;
vy = …;
elseif pi < alpha <= 3*pi/2
vx = …;
vy = …;
endHi there!
I wrote an if-statement, followed by an if-else statement, followed by another if-else statement, followed by the end keyword.
The goal is to use different formulas for different values of alpha, something like the below.
However, it seems that only the first if-statement block executes — for all possible values of alpha, even when alpha already exceeds that interval.
Where is my mistake? Thanks in advance.
alpha = linspace(0,3*pi/2,1000)
if 0 <= alpha <= pi/2
vx = …;
vy = …;
elseif pi/2 < alpha <= pi
vx = …;
vy = …;
elseif pi < alpha <= 3*pi/2
vx = …;
vy = …;
end Hi there!
I wrote an if-statement, followed by an if-else statement, followed by another if-else statement, followed by the end keyword.
The goal is to use different formulas for different values of alpha, something like the below.
However, it seems that only the first if-statement block executes — for all possible values of alpha, even when alpha already exceeds that interval.
Where is my mistake? Thanks in advance.
alpha = linspace(0,3*pi/2,1000)
if 0 <= alpha <= pi/2
vx = …;
vy = …;
elseif pi/2 < alpha <= pi
vx = …;
vy = …;
elseif pi < alpha <= 3*pi/2
vx = …;
vy = …;
end if statement MATLAB Answers — New Questions
How to dewarp an image?
Hello,
I made a thermogram of one face of a cube, from 45 degrees angle. Therefore, the face does not results on the picture as a square, but it looks like a trapeze.
I would like to dewarp such image in order to get the square-shape back. How can I do it?
Thanks everybody!Hello,
I made a thermogram of one face of a cube, from 45 degrees angle. Therefore, the face does not results on the picture as a square, but it looks like a trapeze.
I would like to dewarp such image in order to get the square-shape back. How can I do it?
Thanks everybody! Hello,
I made a thermogram of one face of a cube, from 45 degrees angle. Therefore, the face does not results on the picture as a square, but it looks like a trapeze.
I would like to dewarp such image in order to get the square-shape back. How can I do it?
Thanks everybody! image processing, image analysis, image MATLAB Answers — New Questions
Plotting data dependent on three independent variables.
I have a mathematical expression which is dependent on three independent variables. Each of the three variables are one dimensional arrays or row matrices. How to plot the data as a functions of the three variables?I have a mathematical expression which is dependent on three independent variables. Each of the three variables are one dimensional arrays or row matrices. How to plot the data as a functions of the three variables? I have a mathematical expression which is dependent on three independent variables. Each of the three variables are one dimensional arrays or row matrices. How to plot the data as a functions of the three variables? matlab plot MATLAB Answers — New Questions
How can I build an input file by using one file including all necessary earthquake events’ info in Coulomb 3.4?
In Coulomb 3.4 software, when I tried to build an input file from lon&lat map section, I saw that I should write fault elements one by one. However, I want to import one file ( e.g. a txt file) including whole fault paramerters such as longitude, latitude, depth, magnitude(Mw), strike, dip, and rake. Thus, how I import that mentioned file? Is there any way to fix this problem?In Coulomb 3.4 software, when I tried to build an input file from lon&lat map section, I saw that I should write fault elements one by one. However, I want to import one file ( e.g. a txt file) including whole fault paramerters such as longitude, latitude, depth, magnitude(Mw), strike, dip, and rake. Thus, how I import that mentioned file? Is there any way to fix this problem? In Coulomb 3.4 software, when I tried to build an input file from lon&lat map section, I saw that I should write fault elements one by one. However, I want to import one file ( e.g. a txt file) including whole fault paramerters such as longitude, latitude, depth, magnitude(Mw), strike, dip, and rake. Thus, how I import that mentioned file? Is there any way to fix this problem? coulomb3.4, input file, coulomb 3.3, earthquake, fault elements, input, function, code, file, matlab MATLAB Answers — New Questions
Using erfcinv and array indices are being rejected as not positive or valid, despite all being positive in the vector
Hello, I am currently working on some research data analysis and have been trying to fit autocorrelated data to a plot with a 95% confidence interval to check the distribution of the residuals. To do this, I’m working from the mathworks autocorrelation page (https://www.mathworks.com/help/signal/ug/residual-analysis-with-autocorrelation.html). When trying to set the confidence interval, I get the error that all array indices must be positive or logical values. Below is the code that I’m using, as well as residual data set.
erfbatp = erfcinv(a.Residuals.Raw);
erfbatp = erfbatp’;
erfbatp = erfbatp(~isnan(erfbatp))’;
conf99batp = sqrt(2)*erfbatp(2*.05/2);
lconfbatp = -conf99batp/sqrt(length(a.Residuals.Raw));
upconfbatp = conf99batp/sqrt(length(a.Residuals.Raw));
1.52973772211594
1.58151023710729
1.53641175549635
1.68852456597857
1.36494692501592
2.07737261195390
1.50753906916397
2.02256675790035
1.05155195289689
1.40278856349961
2.16079514557325
1.43199717380622
1.48556496430640
1.42827621898581
1.62133741215299
1.34671652045243
1.62813386205574
1.41104099011439
1.50196551118339
1.34604336518968
1.47733944833881
1.19896422993831
1.72322100428785
Above is the residual values in the erfbatp variable after removing NaNs, any advice or information would be appreciated.Hello, I am currently working on some research data analysis and have been trying to fit autocorrelated data to a plot with a 95% confidence interval to check the distribution of the residuals. To do this, I’m working from the mathworks autocorrelation page (https://www.mathworks.com/help/signal/ug/residual-analysis-with-autocorrelation.html). When trying to set the confidence interval, I get the error that all array indices must be positive or logical values. Below is the code that I’m using, as well as residual data set.
erfbatp = erfcinv(a.Residuals.Raw);
erfbatp = erfbatp’;
erfbatp = erfbatp(~isnan(erfbatp))’;
conf99batp = sqrt(2)*erfbatp(2*.05/2);
lconfbatp = -conf99batp/sqrt(length(a.Residuals.Raw));
upconfbatp = conf99batp/sqrt(length(a.Residuals.Raw));
1.52973772211594
1.58151023710729
1.53641175549635
1.68852456597857
1.36494692501592
2.07737261195390
1.50753906916397
2.02256675790035
1.05155195289689
1.40278856349961
2.16079514557325
1.43199717380622
1.48556496430640
1.42827621898581
1.62133741215299
1.34671652045243
1.62813386205574
1.41104099011439
1.50196551118339
1.34604336518968
1.47733944833881
1.19896422993831
1.72322100428785
Above is the residual values in the erfbatp variable after removing NaNs, any advice or information would be appreciated. Hello, I am currently working on some research data analysis and have been trying to fit autocorrelated data to a plot with a 95% confidence interval to check the distribution of the residuals. To do this, I’m working from the mathworks autocorrelation page (https://www.mathworks.com/help/signal/ug/residual-analysis-with-autocorrelation.html). When trying to set the confidence interval, I get the error that all array indices must be positive or logical values. Below is the code that I’m using, as well as residual data set.
erfbatp = erfcinv(a.Residuals.Raw);
erfbatp = erfbatp’;
erfbatp = erfbatp(~isnan(erfbatp))’;
conf99batp = sqrt(2)*erfbatp(2*.05/2);
lconfbatp = -conf99batp/sqrt(length(a.Residuals.Raw));
upconfbatp = conf99batp/sqrt(length(a.Residuals.Raw));
1.52973772211594
1.58151023710729
1.53641175549635
1.68852456597857
1.36494692501592
2.07737261195390
1.50753906916397
2.02256675790035
1.05155195289689
1.40278856349961
2.16079514557325
1.43199717380622
1.48556496430640
1.42827621898581
1.62133741215299
1.34671652045243
1.62813386205574
1.41104099011439
1.50196551118339
1.34604336518968
1.47733944833881
1.19896422993831
1.72322100428785
Above is the residual values in the erfbatp variable after removing NaNs, any advice or information would be appreciated. data analysis, residuals, autocorrelation, erfcinv MATLAB Answers — New Questions
after inter activation key write for me Invalid Activation Key (510). For help resolving this issue, see this MATLAB Answer.
Invalid Activation Key (510). For help resolving this issue, see this MATLAB Answer.Invalid Activation Key (510). For help resolving this issue, see this MATLAB Answer. Invalid Activation Key (510). For help resolving this issue, see this MATLAB Answer. invalid activation key (510). for help resolving t MATLAB Answers — New Questions
Why I get a 3 dimensional array using to workspace block?
Hello everybody. I have a for loop which its output is 2 by N. N can be 1000 for example. And I define this for loop inside matlab function block in simulink. When I want to get output from it, it is defined as 2 by 1000 by 51. I mean it becomes 3 Dimensional! Why this happens and how to solve it? I also saw this problem when I wanted to integrate a matlab function. I need the output be M by N, but not M by N by D.Hello everybody. I have a for loop which its output is 2 by N. N can be 1000 for example. And I define this for loop inside matlab function block in simulink. When I want to get output from it, it is defined as 2 by 1000 by 51. I mean it becomes 3 Dimensional! Why this happens and how to solve it? I also saw this problem when I wanted to integrate a matlab function. I need the output be M by N, but not M by N by D. Hello everybody. I have a for loop which its output is 2 by N. N can be 1000 for example. And I define this for loop inside matlab function block in simulink. When I want to get output from it, it is defined as 2 by 1000 by 51. I mean it becomes 3 Dimensional! Why this happens and how to solve it? I also saw this problem when I wanted to integrate a matlab function. I need the output be M by N, but not M by N by D. matlab function simulink arrays MATLAB Answers — New Questions
Delete a timer object in it’s callback
I have short timer callbacks to change a color from a button press or other action back to it’s original color. This gives a nice flash of color when that action happens. Since the names given to a timer when it is declared don’t really exist e.g."timer-1" and you don’t get to know which one it is anyway, I tagged them like this:
Example: Set color then start timer
app.LickRight.Color=’green’;
tR = timer;
tR.Tag = "tR"; % Tag it with it’s own name
tR.StartDelay = 0.1;
tR.TimerFcn = @(~,~)app.RightBlack;
start(tR);
The callback looks like this:
function RightBlack(app,~)
app.LickRight.Color=’black’;
stop(timerfind(‘Tag’,’tR’));
delete(timerfind(‘Tag’,’tR’));
end
break at the start of the callback after color change:
1042 stop(timerfind(‘Tag’,’tR’));
K>> timerfind(‘Tag’,’tR’)
ans =
Timer Object: timer-2
Timer Settings
ExecutionMode: singleShot
Period: 1
BusyMode: drop
Running: on
Callbacks
TimerFcn: @(~,~)app.RightBlack
ErrorFcn: ”
StartFcn: ”
StopFcn: ”
First question: Since it’s a singleShot and I timed out into the callback why is it still Running On
OK, I can handle it with
stop(timerfind(‘Tag’,’tR’));
Step into that and check
K>> timerfind(‘Tag’,’tR’)
ans =
Timer Object: timer-2
Timer Settings
ExecutionMode: singleShot
Period: 1
BusyMode: drop
Running: off
Callbacks
TimerFcn: @(~,~)app.RightBlack
ErrorFcn: ”
StartFcn: ”
StopFcn: ”
OK, that worked, now it’s off at least. Next line executed:
delete(timerfind(‘Tag’,’tR’));
Check again for this one:
K>> timerfind(‘Tag’,’tR’)
ans =
Empty timer object: 0-by-0
That’s interesting, not sure what it means. Let’s check all timer objects
K>> timerfind
ans =
Timer Object: timer-2
Timer Settings
ExecutionMode: singleShot
Period: 1
BusyMode: drop
Running: off
Callbacks
TimerFcn: @(~,~)app.RightBlack
ErrorFcn: ”
StartFcn: ”
StopFcn: ”
Wait what!? Is it there or not:
Let’s delete everything for now (Can’t do this in the real program, multiple timers could be running at once)
K>> delete(timerfind)
K>> timerfind
ans =
[]
Yeah, that’s what I’m looking for. Why didn’t I get it the first time?
Here’s what’s weird: When I try it again it does delete and I don’t get what’s above. How did that happen the first time? (retorical question)
Is this clear simple way to find a timer so you can delete it in the callback? Is it OK to put lessons learned here inMatlabCentral for others to find?
I’m asking any way because the HELP for this kind of thing is archaic (GUIDE) and a very long solution from IDK how long ago whith all kinds of handle manipulation. Let’s show how timer cleanup can be done easily, because if you don’t delete them after they run they pile up eating memory, even outlasting stopping and rerunning the program, because they are separate process threads.
Oh, one more thing. Good practice when using timers in an App Designed program to do this when app closes in the built in function?
% Close request function: MouseOdor
function MouseOdorCloseRequest(app, event)
fclose(‘all’); % closes all open files. This probably happens anyway?
% Clear all timers
stop(timerfindall);
delete(timerfindall);
delete(app);
end
Anyone else notice that you can’t add text after a code section unless you leave some blank lines below where you are typing?I have short timer callbacks to change a color from a button press or other action back to it’s original color. This gives a nice flash of color when that action happens. Since the names given to a timer when it is declared don’t really exist e.g."timer-1" and you don’t get to know which one it is anyway, I tagged them like this:
Example: Set color then start timer
app.LickRight.Color=’green’;
tR = timer;
tR.Tag = "tR"; % Tag it with it’s own name
tR.StartDelay = 0.1;
tR.TimerFcn = @(~,~)app.RightBlack;
start(tR);
The callback looks like this:
function RightBlack(app,~)
app.LickRight.Color=’black’;
stop(timerfind(‘Tag’,’tR’));
delete(timerfind(‘Tag’,’tR’));
end
break at the start of the callback after color change:
1042 stop(timerfind(‘Tag’,’tR’));
K>> timerfind(‘Tag’,’tR’)
ans =
Timer Object: timer-2
Timer Settings
ExecutionMode: singleShot
Period: 1
BusyMode: drop
Running: on
Callbacks
TimerFcn: @(~,~)app.RightBlack
ErrorFcn: ”
StartFcn: ”
StopFcn: ”
First question: Since it’s a singleShot and I timed out into the callback why is it still Running On
OK, I can handle it with
stop(timerfind(‘Tag’,’tR’));
Step into that and check
K>> timerfind(‘Tag’,’tR’)
ans =
Timer Object: timer-2
Timer Settings
ExecutionMode: singleShot
Period: 1
BusyMode: drop
Running: off
Callbacks
TimerFcn: @(~,~)app.RightBlack
ErrorFcn: ”
StartFcn: ”
StopFcn: ”
OK, that worked, now it’s off at least. Next line executed:
delete(timerfind(‘Tag’,’tR’));
Check again for this one:
K>> timerfind(‘Tag’,’tR’)
ans =
Empty timer object: 0-by-0
That’s interesting, not sure what it means. Let’s check all timer objects
K>> timerfind
ans =
Timer Object: timer-2
Timer Settings
ExecutionMode: singleShot
Period: 1
BusyMode: drop
Running: off
Callbacks
TimerFcn: @(~,~)app.RightBlack
ErrorFcn: ”
StartFcn: ”
StopFcn: ”
Wait what!? Is it there or not:
Let’s delete everything for now (Can’t do this in the real program, multiple timers could be running at once)
K>> delete(timerfind)
K>> timerfind
ans =
[]
Yeah, that’s what I’m looking for. Why didn’t I get it the first time?
Here’s what’s weird: When I try it again it does delete and I don’t get what’s above. How did that happen the first time? (retorical question)
Is this clear simple way to find a timer so you can delete it in the callback? Is it OK to put lessons learned here inMatlabCentral for others to find?
I’m asking any way because the HELP for this kind of thing is archaic (GUIDE) and a very long solution from IDK how long ago whith all kinds of handle manipulation. Let’s show how timer cleanup can be done easily, because if you don’t delete them after they run they pile up eating memory, even outlasting stopping and rerunning the program, because they are separate process threads.
Oh, one more thing. Good practice when using timers in an App Designed program to do this when app closes in the built in function?
% Close request function: MouseOdor
function MouseOdorCloseRequest(app, event)
fclose(‘all’); % closes all open files. This probably happens anyway?
% Clear all timers
stop(timerfindall);
delete(timerfindall);
delete(app);
end
Anyone else notice that you can’t add text after a code section unless you leave some blank lines below where you are typing? I have short timer callbacks to change a color from a button press or other action back to it’s original color. This gives a nice flash of color when that action happens. Since the names given to a timer when it is declared don’t really exist e.g."timer-1" and you don’t get to know which one it is anyway, I tagged them like this:
Example: Set color then start timer
app.LickRight.Color=’green’;
tR = timer;
tR.Tag = "tR"; % Tag it with it’s own name
tR.StartDelay = 0.1;
tR.TimerFcn = @(~,~)app.RightBlack;
start(tR);
The callback looks like this:
function RightBlack(app,~)
app.LickRight.Color=’black’;
stop(timerfind(‘Tag’,’tR’));
delete(timerfind(‘Tag’,’tR’));
end
break at the start of the callback after color change:
1042 stop(timerfind(‘Tag’,’tR’));
K>> timerfind(‘Tag’,’tR’)
ans =
Timer Object: timer-2
Timer Settings
ExecutionMode: singleShot
Period: 1
BusyMode: drop
Running: on
Callbacks
TimerFcn: @(~,~)app.RightBlack
ErrorFcn: ”
StartFcn: ”
StopFcn: ”
First question: Since it’s a singleShot and I timed out into the callback why is it still Running On
OK, I can handle it with
stop(timerfind(‘Tag’,’tR’));
Step into that and check
K>> timerfind(‘Tag’,’tR’)
ans =
Timer Object: timer-2
Timer Settings
ExecutionMode: singleShot
Period: 1
BusyMode: drop
Running: off
Callbacks
TimerFcn: @(~,~)app.RightBlack
ErrorFcn: ”
StartFcn: ”
StopFcn: ”
OK, that worked, now it’s off at least. Next line executed:
delete(timerfind(‘Tag’,’tR’));
Check again for this one:
K>> timerfind(‘Tag’,’tR’)
ans =
Empty timer object: 0-by-0
That’s interesting, not sure what it means. Let’s check all timer objects
K>> timerfind
ans =
Timer Object: timer-2
Timer Settings
ExecutionMode: singleShot
Period: 1
BusyMode: drop
Running: off
Callbacks
TimerFcn: @(~,~)app.RightBlack
ErrorFcn: ”
StartFcn: ”
StopFcn: ”
Wait what!? Is it there or not:
Let’s delete everything for now (Can’t do this in the real program, multiple timers could be running at once)
K>> delete(timerfind)
K>> timerfind
ans =
[]
Yeah, that’s what I’m looking for. Why didn’t I get it the first time?
Here’s what’s weird: When I try it again it does delete and I don’t get what’s above. How did that happen the first time? (retorical question)
Is this clear simple way to find a timer so you can delete it in the callback? Is it OK to put lessons learned here inMatlabCentral for others to find?
I’m asking any way because the HELP for this kind of thing is archaic (GUIDE) and a very long solution from IDK how long ago whith all kinds of handle manipulation. Let’s show how timer cleanup can be done easily, because if you don’t delete them after they run they pile up eating memory, even outlasting stopping and rerunning the program, because they are separate process threads.
Oh, one more thing. Good practice when using timers in an App Designed program to do this when app closes in the built in function?
% Close request function: MouseOdor
function MouseOdorCloseRequest(app, event)
fclose(‘all’); % closes all open files. This probably happens anyway?
% Clear all timers
stop(timerfindall);
delete(timerfindall);
delete(app);
end
Anyone else notice that you can’t add text after a code section unless you leave some blank lines below where you are typing? delete timer MATLAB Answers — New Questions