Why doesn't my function for writing a file work?
2 views (last 30 days)
Show older comments
I have a section of code that I'm trying to convert to a function. This code works without being used as a function, so the problem is seemingly with how I'm defining the function. MHere is my function:
function write_fill_empty_data_model_temp(input_general,...
data_proc_fill_empty)
output_file_fill_empty_data_model = fopen(strcat(pwd,'\',...
input_general{1}.raw_data_daily_directory,'\',...
input_general{1}.raw_data_test_directory,'\',...
input_general{1}.fill_empty_file_name,...
'_fill_empty_data_model.txt'),'w');
fprintf(output_file_fill_empty_data_model,'%s\t%s\n',...
'Time - Model (s)',...
'Valve 1 Position (%)');
fprintf(output_file_fill_empty_data_model,'%.3f\t%.3f\n',...
[data_proc_fill_empty{1}.time_model_no_shift,...
data_proc_fill_empty{1}.valve_1_opening_percent]');
fclose(output_file_fill_empty_data_model);
end
My inputs are two structures. As best as I can tell, the only inputs to this function are the two structures. When I call this function, I get this error:
Not enough input arguments.
Error in write_fill_empty_data_model (line 3)
output_file_fill_empty_data_model =
fopen(strcat(pwd,'\',input_general{1}.raw_data_daily_directory,'\',input_general{1}.raw_data_test_directory,'\',input_general{1}.fill_empty_file_name,'_fill_empty_data_model.txt'),'w');
I have struggled to figure out the problem. Can someone please help?
0 Comments
Accepted Answer
Jan
on 15 Oct 2021
Edited: Jan
on 15 Oct 2021
"Not enough input arguments" might mean, that you call this function without inputs. Therefore the most important part of the code is missing: how do you call this function?
Maybe you press the green triangle in the editor? This would call trhe function without inputs.
Remarks:
Try to find simpler names for functions and variables. "write_fill_empty_data_model_temp", "input_general" and "data_proc_fill_empty" look really confusing. Calling a file ID "output_file_fill_empty_data_model" is very strange. The standard "fid" is a nice convention.
fullfile() is smarter than strcat() to concatenate path names.
More Answers (0)
See Also
Categories
Find more on Structures 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!