Error with fopen: I saw a lot of answers saying it has to do with folder permissions, but I should have write permissions because it is a folder I created.
5 views (last 30 days)
Show older comments
% ---------------------------------------------------
% Inputs
% ---------------------------------------------------
inputFile = input("Enter the input file name: ", 's');
outputFile = input('Enter the output file name: ', 's');
S = input("Enter the window size: ");
% ---------------------------------------------------
% Computations
% ---------------------------------------------------
import = importdata(inputFile);
importData = import.data;
M = (S - 1) / 2;
numValues = size(importData, 1);
time = [];
voltage = [];
j = 1;
for j = 1:(numValues - S + 1)
sum = 0;
k = 0;
for k = 0:(S - 1)
sum = sum + importData((j+k), 2);
end
average = sum / S;
time(j) = importData((j + M), 1);
voltage(j) = sum / S;
end
newData = [time; voltage];
% ---------------------------------------------------
% Outputs
% ---------------------------------------------------
fOut = fopen(outputFile,'wt');
fprintf(fOut, '%14s %16s\n', 'Time (seconds)', 'Voltage (Volts)');
fprintf(fOut, '%14.1f %16.2f\n', newData);
fclose(fOut);
1 Comment
Srija Kethiri
on 13 Dec 2022
Hi Reece,
Can you paste the full error message you are getting when trying to open a file using 'fopen' command.
Answers (1)
Jan
on 13 Dec 2022
fopen fails to open a file for writing, if:
- you do not have write-permissing in this folder. You "should" have such permissions is not sufficient. Check this explicitly in the operating system. You can try to create a new file there also as a reliable test. Check, if the folder is really what you expect it to be.
- the file is existing and protected. Check the file permissions.
- the file is existing and opened by another application allready. This locks the file also.
- the file name contain invalid characters.
- Matlab or the OS runs out of file handles. Usually operating systems can open about 200 files only at the same time. If another code opens a lot of files without a proper closing, this list can be exhausted.
You can find hundreds of corresponding questions in this forum, in which the programmers were convinced that they do have write-privileges and the file was not opened somewhere else. But after another check or a restart of Matlab (or the computer), it worked immediately.
Never call fopen without checking the success and showing a proper error message:
[fOut, msg] = fopen(outputFile, 'wt');
assert(fOut > 0, 'Failed to open "%s": %s', outputFile, msg);
Your code allows to provide outputFile without a path. Then it is taken relatively to the current folder. Check twice, if this current folder is really, what you expect it to be. Prefer to use absolute pathes in general:
folder = cd; % Or insert the wanted folder here
outputFile = fullfile(folder, outputFile);
Using uiputfile() is safer then input() also, because it shows the current folder and let the user see, if it is not the expected one:
% Replace:
outputFile = input('Enter the output file name: ', 's');
% by:
[file, folder] = uiputfile('*.*', 'Enter output file:');
if ~ischar(file)
error('User aborted choosing an output file.');
end
outputFile = fullfile(folder, file);
Then the error message created by assert contains the folder also.
0 Comments
See Also
Categories
Find more on Call C from MATLAB 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!