combing two arrays into a table (app designer)-Array indices must be positive integers or logical values.

9 views (last 30 days)
I am trying to show to arrays in a table on app designer however, it keeps returning the error 'Array indices must be positive integers or logical values.' I have initialised the arrays as properties in order to call on them in the app, I susspect this is where I have gone wrong as the code is executing as if app.out_ff and app.Ef are empty. How can I resolve this? Thanks in advance!
%initialising:
properties (Access = public)
Property % Description
out_ff
Eff
end
methods (Access = public)
function results = func(app)
app.out_ff = out_f;
app.Eff = Ef;
end
end
%getting file and creating table:
[filename,Directory] = uigetfile('*.txt');
rawData = readtable(filename);
app.UITable.Data = table(app.out_ff, app.Eff(length(app.out_ff))); % error source
app.UITable.Data.Properties.VariableNames{1} = 't';
app.UITable.Data.Properties.VariableNames{2} = 'Ef';
app.UITable.ColumnName = app.UITable.Data.Properties.Variable
%the functions that produce the arrays:
function [output] = readdatav4 (input)
t = rawData{:,1};
Et = rawData{:,2};
n_s = 0.063;
p = 4.9;
[w_c,Amp_noisef,w,app.Eff]= 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
[out_f,out_Ef]=SmoothingSpec(w,Ef,window); % Smoothing
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
  1 Comment
Jonas
Jonas on 7 Dec 2022
i guess
length(out_ff)
gives 0, since it is not set at the start of the app (size 0x0 probably)
there effectively, there is written
app.Eff(0)
which gives the indexing error

Sign in to comment.

Accepted Answer

Kevin Holly
Kevin Holly on 7 Dec 2022
Here are a few options to consider below.
1. Define variables
You can define variables when they are created as properties
properties (Access = public)
out_ff = zeros(10,1)
Eff = zeros(10,1)
end
2. Use an if condition that only ruins lines with both variables are not empty
if ~isempty(app.out_ff) && ~isempty(app.Eff)
app.UITable.Data = table(app.out_ff, app.Eff(length(app.out_ff))); % error source
app.UITable.Data.Properties.VariableNames{1} = 't';
app.UITable.Data.Properties.VariableNames{2} = 'Ef';
app.UITable.ColumnName = app.UITable.Data.Properties.Variable
else
% Alternative
end
3. Use a try catch
try
app.UITable.Data = table(app.out_ff, app.Eff(length(app.out_ff))); % error source
app.UITable.Data.Properties.VariableNames{1} = 't';
app.UITable.Data.Properties.VariableNames{2} = 'Ef';
app.UITable.ColumnName = app.UITable.Data.Properties.Variable
catch
% Alternative
end
  5 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!