Error using fscanf Invalid file identifier. Use fopen to generate a valid file identifier.

6 views (last 30 days)
file=fopen('CIData(1).txt','r');
densities=fscanf(file,'%g');
Average=mean(Densities);
fclose(f);
z=1.282;
standard_dev=2.65;
num_samples=length(densities);
estimate_average=z*standard_dev/sqrt(n);
upper_limit=Average+estimate_average;
lower_limit=Average-estimate_average;
fprintf("Num_of_samples: %d\n",num_samples);
fprintf("Mean Value: %.2f\n",Average);
fprintf("Standard_deviation: %.2f\n",standard_dev);
fprintf("Z score for 80 percent confidence: %.2f\n",z);
fprintf("Upper_limit: %.1f\n",upper_limit);
fprintf("Lower_limit: %.1f\n",lower_limit);
  3 Comments
Sadia
Sadia on 26 Nov 2020
Edited: Sadia on 26 Nov 2020
Mr. Walter Roberson I have the same issue while running a code for truss tcl file in matlab. Why matlab is not reading tcl files?
Following is code:
clear
% Note: all files (OpenSees.exe, Truss.tcl, and truss.m) need to be in a same folder
A = 10;
%% Edit tcl file
str = deblank(fileread('Truss.tcl')); % Open tcl file, and remove trailing whitespace (empty last line, if there is)
tcl = regexp(str, '\r?\n', 'split')'; % Save coding lines into cell array
tcl{50} = sprintf('set A %g',A); % Edit the desired input line, for example
fid = fopen('Truss.tcl','wt'); % Open the tcl file
fprintf(fid,'%s\n',tcl{:}); % Overwrite all lines with cell array from Matlab
fclose(fid); % Close tcl file
%% Run OpenSees
!OpenSees.exe Truss.tcl
clc % to clear command window
%% Read Result
fid2 = fopen('example.out','r'); % Open output file
formatSpec = '%d %f'; % Format of output
sizeA = [1 Inf]; % Data size of output: 1 row and infinity columns for this example
data = fscanf(fid2,formatSpec,sizeA); % Read output file in the specified output format and data size
fclose(fid2); % Close output file
def = ['Displacement X: ' num2str(data(2)) '\n' 'Displacement Y: ' num2str(data(4)) '\n'];
fprintf(def)
Walter Roberson
Walter Roberson on 26 Nov 2020
trussfile = 'Truss.tcl';
outfile = 'example.out';
% Note: all files (OpenSees.exe, Truss.tcl, and truss.m) need to be in a same folder
A = 10;
%% Edit tcl file
assert(exist(trussfile, 'file'), "Truss file must be in current directory: '%s'", trussfile)
str = deblank(fileread(trussfile)); % Open tcl file, and remove trailing whitespace (empty last line, if there is)
tcl = regexp(str, '\r?\n', 'split')'; % Save coding lines into cell array
tcl{50} = sprintf('set A %g',A); % Edit the desired input line, for example
[fid, msg] = fopen(trussfile,'wt'); % Open the tcl file
if fid < 0
error("Failed to open truss file '%s' for writing because: '%s'", trussfile, msg);
end
fprintf(fid,'%s\n',tcl{:}); % Overwrite all lines with cell array from Matlab
fclose(fid); % Close tcl file
%% Run OpenSees
cmd = sprintf('OpenSees.exe "%s"', trussfile);
[status, output] = system(cmd);
if status ~= 0
error("Failed executing OpenSees, output was: %s", output)
end
%% Read Result
[fid2, msg] = fopen(outfile,'r'); % Open output file
if fid2 < 0
error("Failed to open output file '%s' for reading because: '%s'", msg);
end
formatSpec = '%d %f'; % Format of output
sizeA = [1 Inf]; % Data size of output: 1 row and infinity columns for this example
data = fscanf(fid2,formatSpec,sizeA); % Read output file in the specified output format and data size
fclose(fid2); % Close output file
def = ['Displacement X: ' num2str(data(2)) '\n' 'Displacement Y: ' num2str(data(4)) '\n'];
fprintf(def)

Sign in to comment.

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!