フィルターのクリア

Verify the File Path Check File Existence Verify File Permissions Check File Identifier Error Handling for File Openingwith thermo dta to matlab

4 ビュー (過去 30 日間)
clear all
close all
clc
% Define the file path
file_path = 'thermo.date';
% Attempt to open the file for reading
try
f1 = fopen(file_path, 'r');
% Check if file opening was successful
if f1 == -1
error('Could not open the file.');
end
% Reading the comment lines
for i = 1:5
fgetl(f1);
end
% Extracting all species data
for i = 1:53
% Extracting name of gas species
A = fgetl(f1);
z = findstr(A, ' ');
species_name = A(1:z(1)-1);
% Extracting local temperatures of gas species
t1 = str2double(A(z(1):z(2)));
t2 = str2double(A(z(2):z(3)));
t3 = str2double(A(z(3):end));
% Extracting co-efficient of gas species
B = fgetl(f1);
a_values = sscanf(B, '%f');
a1 = a_values(1);
a2 = a_values(2);
a3 = a_values(3);
a4 = a_values(4);
a5 = a_values(5);
C = fgetl(f1);
a_values = sscanf(C, '%f');
a6 = a_values(1);
a7 = a_values(2);
a8 = a_values(3);
a9 = a_values(4);
a10 = a_values(5);
D = fgetl(f1);
a_values = sscanf(D, '%f');
a11 = a_values(1);
a12 = a_values(2);
a13 = a_values(3);
a14 = a_values(4);
% Creating the range of low and high values of local temperatures
T = linspace(t1, t3, 1000);
R = 8.314; % Universal gas constant in J/mol.K
% Calculation for Specific heat, Enthalpy and Entropy
for j = 1:length(T)
if T(j) > t2
Cp(j) = (a1 + a2*T(j) + a3*T(j)^2 + a4*T(j)^3 + a5*T(j)^4)*R;
H(j) = (a1 + (a2*T(j))/2 + (a3*T(j)^2)/3 + (a4*T(j)^3)/4 + (a5*T(j)^4)/5 + (a6/T(j)))*R*T(j);
S(j) = a1*log(T(j)) + a2*T(j) + (a3*T(j)^2)/2 + (a4*T(j)^3)/3 + (a5*T(j)^4)/4 + a7;
else
Cp(j) = (a8 + a9*T(j) + a10*T(j)^2 + a11*T(j)^3 + a12*T(j)^4)*R;
H(j) = (a8 + (a9*T(j))/2 + (a10*T(j)^2)/3 + (a11*T(j)^3)/4 + (a12*T(j)^4)/5 + (a13/T(j)))*R*T(j);
S(j) = a8*log(T(j)) + a9*T(j) + (a10*T(j)^2)/2 + (a11*T(j)^3)/3 + (a12*T(j)^4)/4 + a14;
end
end
% Molecular weight calculation
elements = ['H', 'C', 'O', 'N', 'A', 'S'];
atomic_weight = [1.0079 12.0107 15.999 14.0067 39.948 32.065];
mol_mass = 0;
for l = 1:length(species_name)
for m = 1:length(atomic_weight)
if strcmp(species_name(l), elements(m))
mol_mass = mol_mass + atomic_weight(m);
position = m;
end
end
n = str2double(species_name(l));
if n > 1
mol_mass = mol_mass + atomic_weight(position)*(n-1);
end
end
% Printing the molecular weight of the gas species in the command window
fprintf('Molecular Weight of %s is %f u\n', species_name, mol_mass);
% Creating a folder to save the plots
mkdir(species_name);
cd(species_name);
% Plotting specific heat vs temperature
figure(1)
plot(T, Cp, 'color', 'b', 'linewidth', 2)
xlabel('Temperature (K)')
ylabel('Specific Heat (J/K Kg)')
title({'Specific Heat vs Temperature', ['Species name = ', species_name, ' & Molecular weight = ', num2str(mol_mass)]});
saveas(gcf, 'Cp_vs_Temp.png')
% Plotting enthalpy vs temperature
figure(2)
plot(T, H, 'color', 'r', 'linewidth', 2)
xlabel('Temperature (K)')
ylabel('Enthalpy (J/K)')
title({'Enthalpy vs Temperature', ['Species name = ', species_name, ' & Molecular weight = ', num2str(mol_mass)]});
saveas(gcf, 'Enthalpy_vs_Temp.png')
% Plotting entropy vs temperature
figure(3)
plot(T, S, 'color', 'g', 'linewidth', 2)
xlabel('Temperature (K)')
ylabel('Entropy (J/K)')
title({'Entropy vs Temperature', ['Species name = ', species_name, ' & Molecular weight = ', num2str(mol_mass)]});
saveas(gcf, 'Entropy_vs_Temp.png')
% To change the current directory to the main folder
cd('..');
end
% Close the file
fclose(f1);
catch ME
% Error handling for various reasons file cannot be opened
switch ME.identifier
case 'MATLAB:fopen:FileNotFound'
disp('File not found. Please check the file path.');
case 'MATLAB:fopen:PermissionDenied'
disp('Permission denied. MATLAB does not have the necessary permissions to read the file.');
case 'MATLAB:fopen:InvalidPermission'
disp('Invalid file permissions. Please check file permissions.');
case 'MATLAB:fopen:InvalidFid'
disp('Invalid file identifier. Use fopen to generate a valid file identifier.');
otherwise
disp('An unknown error occurred while opening the file.');
disp(ME.message);
end
end
An unknown error occurred while opening the file.
Could not open the file.
  2 件のコメント
Walter Roberson
Walter Roberson 2024 年 2 月 28 日
Your try/catch spans all of the code. In theory, you could be catching another error.
Stephen23
Stephen23 2024 年 2 月 29 日
編集済み: Stephen23 2024 年 2 月 29 日
Avoid calling CD in code: to import any file data and to save those images use absolute/relative filenames.
It is possible that your CD has changed the directory, then the code errors, and then you run again from a current directory that is not the one you expect to be in. It is best to avoid CD in code.
Avoid TRY around the entirety of your code: judging by the displayed messages you are intending it to catch file errors, but that is not really a good reason to hide all errors in your code. Much better to use a few ASSERT statements to check that the filedata is as expected.
Note that
elements = ['H', 'C', 'O', 'N', 'A', 'S'];
is just a more complex way of writing
elements = 'HCONAS';

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2024 年 2 月 28 日
[f1, msg] = fopen(file_path, 'r');
% Check if file opening was successful
if f1 == -1
error('Could not open the file because: %s', msg);
end

カテゴリ

Help Center および File ExchangeParticle & Nuclear Physics についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by