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

5 vues (au cours des 30 derniers jours)
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 commentaire
Jonas
Jonas le 7 Déc 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

Connectez-vous pour commenter.

Réponse acceptée

Kevin Holly
Kevin Holly le 7 Déc 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 commentaires

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Identification dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by