Why i'm getting this error

3 次查看(过去 30 天)
Mohd Babar
Mohd Babar 2024-6-13
评论: Mohd Babar 2024-6-14
for i =1:N
fx =[ 'C:\Users\SC23M048-BABARMALIKM\Downloads\Abaqus\Hierarchical composite\aax1\jjx' num2str(i) '.txt'];
fy =[ 'C:\Users\SC23M048-BABARMALIKM\Downloads\Abaqus\Hierarchical composite\aay1\jjy' num2str(i) '.txt'];
fu =[ 'C:\Users\SC23M048-BABARMALIKM\Downloads\Abaqus\Hierarchical composite\aau11\jju1' num2str(i) '.txt'];
fv =[ 'C:\Users\SC23M048-BABARMALIKM\Downloads\Abaqus\Hierarchical composite\aau21\jju2' num2str(i) '.txt'];
x = textfile(fx);
y = textfile(fy);
z1 = textfile(fu);
z2 = textfile(fv);
U1 = griddata(x,y,z1,K1,K2);
U2 = griddata(x,y,z2,K1,K2);
zz1 = U1+zz1;
zz2 = U2+zz2;
i ;
end
Error using fscanf
Invalid file identifier. Use fopen to generate a valid file identifier.
value= fscanf(fileID,'%f');
this is the error i'm getting
  6 个评论
Ganesh
Ganesh 2024-6-13
I wonder how your files are structured.
In your question your directory is named "aax1" but you have uploaded ".txt" files with the same name. The "code final.txt" does not have any file of the format "aa.." at all.
You can send the folder structure so we could get a better idea of what you need to access.
Mohd Babar
Mohd Babar 2024-6-14
i have atteched the final code live script

请先登录,再进行评论。

回答(1 个)

Shivani
Shivani 2024-6-13
编辑:Shivani 2024-6-13
The error you're encountering is due to an attempt to read from a file without properly opening it first. In MATLAB, before you can read from a file, you need to open it using fopen, which returns a file identifier. This file identifier is then used with functions like fscanf, fgets, or fread to read from the file. After you're done with the file, it's important to close it using fclose to free up system resources.
You can read about these functions by accessing the following MATLAB documentation link: https://www.mathworks.com/help/matlab/ref/fscanf.html
In the code snippet shared above, you are using 'textfile' as a user-defined function to read the file contents. It is likely that the file identifier in the function is not being opened or closed correctly, leading to this error. The below code snippet shows an example of how the 'textfile()' function can be implemented:
function value = textfile(filename)
fileID = fopen(filename, 'r');
if fileID == -1
error('Failed to open file: %s', filename);
end
value = fscanf(fileID, '%f');
fclose(fileID);
end

标签

Community Treasure Hunt

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

Start Hunting!

Translated by