After creating a folder structure and .m file, I want to write to the file, but the fid keeps coming back as -1. What should i be doing that I'm not?
25 次查看(过去 30 天)
显示 更早的评论
% Here are the variable name string values:
% outputFileName = "Aaron.m"
% outputFilePath = "./folder/yayy/"
% currentSection is a string with a bunch of data
% ===================================
file = fullfile(outputFilePath, outputFileName);
mkdir(file);
[fid, msg] = fopen(file, 'w');
if fid == -1
error('Could not open file "%s" for writing.', file, msg);
end
% Write the content of the current section to the new .m file
fprintf(fid, '%s\n', currentSection);
% Close the file
fclose(fid);
1 个评论
采纳的回答
dpb
about 5 hours 前
编辑:dpb
2025-12-9,19:05
file = fullfile(outputFilePath, outputFileName);
mkdir(file);
You created the directory with the fully qualified file name, not the folder...
% Here are the variable name string values:
% outputFileName = "Aaron.m"
% outputFilePath = "./folder/yayy/"
% currentSection is a string with a bunch of data
% ===================================
file = fullfile(outputFilePath, outputFileName);
if exist(outputFilePath)~=7 % see if already exists, if not create it but not multiple times
mkdir(outputFilePath);
end
[fid, msg] = fopen(file, 'w');
if fid == -1
error('Could not open file "%s" for writing.', file, msg);
end
% Write the content of the current section to the new .m file
fprintf(fid, '%s\n', currentSection);
% Close the file
fclose(fid);
2 个评论
dpb
about 2 hours 前
If that resovled your issue, go ahead and Accept an Answer to let folks know if nothing else...
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!