Clear Filters
Clear Filters

Loop through every subfolder

2 views (last 30 days)
Alexander Guillen
Alexander Guillen on 24 Mar 2023
Answered: Jack on 25 Mar 2023
I have two subfolders in a folder named main: Antoine1(subfolder1) and Antoine2(subfolder2). In each subfolder I have a mat file named Antoinecomp which contains the Antoine constant parameters of two chemical elements such that row1 contains the parameters of element 1 and row 2 the parameters of the second chemical element.
I wrote a function called Antoine which takes these constants and the temperature to solve for A1 and A2. These mat files should contain two columns corresponding to each row. Following some examples available in MATLAB Answers, I came up with the following:
It works well, but this is computing the first row of both mat files and placing them in A1, and so on. How can I modify this code so it doesn't do this? Instead, I want the code to compute row 1 and row 2 of each mat file and placing them in their corresponding mat file (A1 and A2)
clear all; close all; clc
T = linspace(353.2,383.655,10); % Temperature in Kelvin
FileList = dir('**\*.mat');
for k = 1:numel(FileList)
File = fullfile(FileList(k).folder, FileList(k).name);
a = load(File);
A1(:,k) = Antoine(a.Antoinecomp(1,1),a.Antoinecomp(1,2),a.Antoinecomp(1,3),T)';
A2(:,k) = Antoine(a.Antoinecomp(2,1),a.Antoinecomp(2,2),a.Antoinecomp(2,3),T)';
end
Thank you in advance for any guidance.

Accepted Answer

Jack
Jack on 25 Mar 2023
Hi,
To modify your code to compute row 1 and row 2 of each mat file and place them in their corresponding mat file (A1 and A2), you can modify the loop that loads the mat files to extract the parameters for each row and store them separately in the output variables.
Here is an example of how you can modify your code to achieve this:
function [A1, A2] = Antoine(T)
% Load Antoine constants for element 1
load('main\Antoine1\Antoinecomp.mat', 'Antoinecomp');
% Extract parameters for row 1 and row 2
A1_1 = Antoinecomp(1,1:2);
A1_2 = Antoinecomp(2,1:2);
% Load Antoine constants for element 2
load('main\Antoine2\Antoinecomp.mat', 'Antoinecomp');
% Extract parameters for row 1 and row 2
A2_1 = Antoinecomp(1,1:2);
A2_2 = Antoinecomp(2,1:2);
% Compute A1 and A2 for each element and each row
A1 = [AntoineEqn(A1_1, T); AntoineEqn(A1_2, T)];
A2 = [AntoineEqn(A2_1, T); AntoineEqn(A2_2, T)];
end
function P = AntoineEqn(A, T)
% Compute vapor pressure using Antoine equation
P = 10.^(A(1) - A(2)./(T + A(3)));
end
In this modified code, we load the first mat file containing the Antoine constants for element 1, and then extract the parameters for row 1 and row 2 separately using indexing. We then repeat this process for the second mat file containing the Antoine constants for element 2.
Finally, we compute A1 and A2 separately for each element and each row, and store the results in the corresponding output variables.
Note that we also defined a separate helper function AntoineEqn to compute the vapor pressure using the Antoine equation, as this is a repetitive operation that can be easily encapsulated in a function.

More Answers (0)

Categories

Find more on Chemistry 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!