'The variable appears to change size on every loop iteration'
Show older comments
I have been working on a basic procedural data processing program and my code is as follows: (for the error section - Lines 27-62):
function []= Munguia_BIOEFinal()
user_direct = uigetdir();
user_direct_files = dir(user_direct);
addpath(user_direct);
logFilename = strcat('Computed_values.csv');
if(exist(logFilename,'file'))
delete(logFilename);
end
s_no=0;
rv_max_val='';
rv_max_val_std='';
rv_max_val_mean='';
Sec_data_val='';
Sec_data_val_std='';
Sec_data_val_mean='';
fid_csv = fopen(logFilename,'w');
fprintf(fid_csv, ' Date : %s\n', datestr(now) ) ;
fprintf(fid_csv,'\n\nS_No,File_Name,Mean P_Max,Std P_Max,mean max dP/dt, std max dP/dt, mean min dP/dt, std min dP/dt,15percentage of max dvd\n');
for idx=1:length(user_direct_files)
if ~(strcmp(user_direct_files(idx).name,'.') || strcmp(user_direct_files(idx).name,'..'))
disp(['reading this file ....' user_direct_files(idx).name]);
fname = user_direct_files(idx).name;
fid=fopen(fname);
tline = fgetl(fid);
tlines = cell(0,1);
while ischar(tline)
tlines{end+1,1} = tline;
tline = fgetl(fid);
end
fclose(fid);
first_data ='';
sec_data = '';
fa_data = '';
fa_data_dash = '';
rv_data = '';
rv_data_dash = '';
disp('Reading data from the file......');
for file_idx=10:length(tlines)
data_set = split(tlines{file_idx});
first_data{end+1} = data_set{2};
sec_data{end+1} = data_set{3};
fa_data{end+1} = data_set{4};
fa_data_dash{end+1} =data_set{5};
rv_data{end+1} = data_set{6};
if ~isempty(rv_data_dash)
rv_data_dash{end+1} = data_set{7};
end
end
disp('Writing data to an array......')
rv_data = data_edit(rv_data);
sec_data = data_edit(sec_data);
Sec_data_val{end+1} = max(sec_data);
Sec_data_val_std{end+1} = std(sec_data);
Sec_data_val_mean{end+1} = mean(sec_data);
if isnan(rv_data)
value=1;
else
[value,value_idx] = findpeaks(rv_data);
end
rv_max_val{end+1} = max(value);
rv_max_val_std{end+1} = std(value);
rv_max_val_mean{end+1} = mean(value);
s_no=s_no+1;
fprintf(fid_csv,'%d',s_no);
fprintf(fid_csv,',"%s"',char(user_direct_files(idx).name));
fprintf(fid_csv,',"%s"',mean(rv_data));
fprintf(fid_csv,',"%s"',std(rv_data));
fprintf(fid_csv,',"%s"',mean(min(rv_data)));
fprintf(fid_csv,',"%s"',std(min(rv_data)));
fprintf(fid_csv,',"%s"',mean(value));
fprintf(fid_csv,',"%s"',std(value));
disp('Writing data to a csv file......');
fprintf(fid_csv,',"%s"\n',max(value)*0.15);
%saveas(gcf,strrep(fname,'.txt','.png'));%save plot
%close all;
end
end
rmpath(user_direct);
fclose(fid_csv);
disp('csv file created successfully......');
end

When I look on my scripts for solutions to the error, it states to change size on every loop iteration. Consider Preallocating for speed. How would I go about fixing this error in particular
Answers (1)
Some hints:
- addpath(user_direct); - Append only folders to Matlab's path, which contain M-files. Avoid to include folders containing data files. Use absolute path names instead.
- strcat('Computed_values.csv') - strcat with a single input does nothing expect wasting time.
logFilename = fullfile(user_direct, 'Computed_values.csv');
if isfile(logFilename)
delete(logFilename);
end
You can simplify:
fid=fopen(fname);
tline = fgetl(fid);
tlines = cell(0,1);
while ischar(tline)
tlines{end+1,1} = tline;
tline = fgetl(fid);
end
fclose(fid);
to:
tlines = strsplit(fileread(fullfile(user_direct, fname)), newline);
This should fail:
first_data =''; % first_data is a CHAR vector
for file_idx=10:length(tlines)
data_set = split(tlines{file_idx});
first_data{end+1} = data_set{2}; % first_data is a cell string
Pre-allocate the array with its final size instead:
nCell = length(tlines) - 9;
first_data = cell(1, nCell);
for iCell = 1:nCell
file_idx = iCell + 9;
data_set = split(tlines{file_idx});
first_data{iCell} = data_set{2};
The error has another problem:
rv_data{end+1} = data_set{6};
This fails, if data_set has less than 6 elements. Check this using the debugger:
dbstop if error
Enable the debugger. Run the function again. If Matlab stops, check the contents of the array:
disp(data_set)
This will fail also:
fprintf(fid_csv,',"%s"',mean(rv_data))
mean() replies numerical values, but %s expects a char vector or string. This appears multiple times.
In
fprintf(fid_csv,',"%s"',char(user_direct_files(idx).name));
the char() conversion is not needed: user_direct_files(idx).name is a CHAR vector already.
Categories
Find more on Managing Data 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!