Reference to non-existent field

I can't work out why I keep getting reference to non-existent field. I've defined t_accData and I can't see that it is used in any other function so I'm confused:
%% Load data, sample at multiple rates, save resulting as separate CSV files:
%Check to see if this step has been completed and saved previously
%If it has, load saved mat file to speed up processing
Fs = [400 200 100 50 25]; %Arranging from High to low could be better for some plots
%resample in loop
for j = 1:length(Fs)
%Get current sample rate within loop
c_rate = Fs(j);
%Check whether file exists (within the for loop)
if ~exist([filename(1:end-4) '_resample_rawData_Fs_' num2str(c_rate) 'hz.mat'],'file');
%If the file doe not exist, create it:
%load acc data into a structure array with fields
% time, timestamp, x y z, sampleRate;
t_accData = resampleCWA(filename, c_rate); %#ok<*AGROW>
%Save t_accData to a Mat file
save ([filename(1:end-4) '_resample_rawData_Fs_' num2str(c_rate) 'hz.mat', 't_accData'], 't_accData');
%Takes all files of t_accData and combines them into one structure
%with all sample frequencies within it. The delets t_accData.
c_accData(j) = t_accData;
else
%If the file does exist, load the existing file containing variable
%'c_accData'
tempStruct = load([filename(1:end-4) '_resample_rawData_Fs_' num2str(c_rate) 'hz.mat']);
c_accData(j) = tempStruct.t_accData;
clear tempStruct;
end %end of 'If/else' section
end %end of loading 'for loop

4 comentarios

Matt J
Matt J el 11 de Dic. de 2018
Editada: Matt J el 11 de Dic. de 2018
Well, what does dbstop say? It should, for example, let you easily verify whether tempStruct does indeed posssess a field called t_accData.
You appear to be hitting the branch where the file already exists. You should re-check whether the file contains the variable c_accData like the comment says, or if it contains t_accData like you try to access.
Note: we recommend that you assign
filename = [filename(1:end-4) '_resample_rawData_Fs_' num2str(c_rate) 'hz.mat'];
and then use the variable, instead of rebuilding the name every time you use it.
Stephen23
Stephen23 el 11 de Dic. de 2018
Editada: Stephen23 el 11 de Dic. de 2018
I would recommend that you replace
filename(1:end-4)
with the more robust
[~,F] = fileparts(filename)
Also replace all of those string concatenations with sprintf, which would make it easier to spot bugs, such as this strange filename input to save:
[filename(1:end-4) '_resample_rawData_Fs_' num2str(c_rate) 'hz.mat', 't_accData']
Most probably you did not want that trailing t_accData in the file extension. Better:
sprintf('%s_resample_rawData_Fs_%dhz.mat',F,c_rate)
Justine Pearce
Justine Pearce el 11 de Dic. de 2018
Thank you everyone! It now seems to be working with out errors! I'm new to coding so really appreciate your help and advice on writing style too thank you!

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre App Building en Centro de ayuda y File Exchange.

Productos

Versión

R2017a

Preguntada:

el 11 de Dic. de 2018

Comentada:

el 11 de Dic. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by