How do I plot data transformed and calculated by functions in app designer?
Show older comments
I am attempting to plot fourior transform of my data along with the variable w_c which is calculated via algorithm in attached code. I have the plot working in my ordinary matlab notebook but cannot plot the transformed variables within app designer as the variables I need to plot are within the fucntions. When I assign the transformed data to my table outside of the functions I get error function; must be initialised within scope. If I assign the data to the table inside the function it returns nothing. How do I share these variables across outter and inner functions to then plot the FFT of my time domain data alongside the xline of calculated w_c value? The variables needed for the plot are 'out_f' and 'Ef'.
function ButtonPushed(app, event)
[filename,Directory] = uigetfile('*.txt');
g = readtable(filename);
global a
function [output] = readdatav4 (input);
t = g{:,1};
Et = g{:,2};
n_s = 0.063;%constants
p = 4.9;%constants
[w_c,Amp_noisef,w,Ef]= get_noise(t,Et,n_s,p);
w_c;
end
function [w_c,Amp_noisef,w,Ef]= get_noise(t,Et,n_s,p)
w= [1:length(t)]./(t(end)-t(1)); % Natural Frequency
Ef=abs(fft(Et)); % Spectrum
window=round(length(w).* n_s); % Window
[out_f,out_Ef]=SmoothingSpec(w,Ef,window); % Smoothing
w0=linspace(1,5,length(out_f));
Ef_cube=out_Ef.*(w0.^p); % smooth spectra times w^p
[M3,iM3] = max(out_Ef);
[m3,im3] = min(Ef_cube(iM3+1:end)); % Find min
Amp_noisef = out_Ef(im3+iM3); % noise floor amplitude
w_c = out_f(im3+iM3); % Cuttoff frequency
a = Table(out_f, Ef(length(out_f)));
app.UITable.Data = a;
a.Properties.VariableNames{1} = 'out_f';
a.Properties.VariableNames{2} = 'Ef';
app.UITable.ColumnName = a.Properties.VariableNames;
end
function [out_w,out_Ef]=SmoothingSpec(w,Ef,window)
if rem(length(Ef),2)==0 % case even data size
sz=size(Ef);
else
sz=size(Ef)+1; % case odd data size
end
z=sz(1)/2; % keep positive frequencies
for i=1:z-window
out_Ef(i)=mean(Ef(i:i+window)); % smooth
end
out_w=w(1:z-window); % length cut for w
end
2 Comments
chrisw23
on 6 Dec 2022
You should not use the global statement in AppDesigner. Think and code in terms of object oriented programming. Use properties to store data to be used in other method scopes.
Create a property and give it a describing name (not a) or use the given objects properties like
app.UITable.Data = Table(out_f, Ef(length(out_f)));
And use app.UITable.Data where ever you need it in your app.
Create properties app.out_f and app.out_Ef (initialize them in your startup method if you have to) and write directly to them
[app.out_f,app.out_Ef]=SmoothingSpec(w,Ef,window); % Smoothing
If you have a look at your AppDesigner app methods you'll find app as the first argument of every method. That grants you access to everything.
Gregor
on 7 Dec 2022
Answers (0)
Categories
Find more on Develop Apps Using App Designer in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!