Creating multiple plots in one graph from multiple .txt files

5 views (last 30 days)
Hi,
i need help to create code that allows me to read data from multiple .txt files, then do some math on it and from calculated values create plots on one graph from all of those .txt. I'am struggling with it because of my lack of knowledge in matlab.
I have to take WF/dLogM colum, find max of it and then divide every value of that column by found max value. Then just plot Molecular Weight as x and calculated values as y.
I need any sugestions what functions/commands I should use.
  1 Comment
Rik
Rik on 17 Nov 2023
If you have trouble with Matlab basics you may consider doing the Onramp tutorial (which is provided for free by Mathworks).
What have you tried for each step?

Sign in to comment.

Answers (1)

Mathieu NOE
Mathieu NOE on 17 Nov 2023
hello
try this :
d = dir('NW*.txt'); % d is a structure array that contains the list of all txt files starting with NW
% create figure and use "hold on" to overlay multiple results
figure(1)
hold on
% main loop
for k = 1:numel(d)
FileName=d(k).name;
fprintf(1,'Now reading %s\n',FileName);
data = readmatrix(FileName,"NumHeaderLines",40,"DecimalSeparator",","); % read the data and skip the first 40 lines
% data has 20 columns :
% Ret. Vol. RI UV RALS LALS IV - DP IV - IP Adjusted RI Adjusted UV Adjusted RALS Adjusted LALS Adjusted IV - DP Adjusted IV - IP
%Molecular Weight Cumulative Weight Fraction Normalized Wt Fr Normalized Mole Fraction Conc. WF/dLogM
% I have to take WF/dLogM colum, find max of it and then divide every value of that column
% by found max value.
[m,n] = size(data);
WF_dLogM = data(:,n); % could be done also with = data(:,end) but we need anyway "n" again below
Molecular_Weight = data(:,n-5);
% remove NaN values due to missing data in file
ind_nan = isnan(WF_dLogM);
WF_dLogM(ind_nan) = []; % NaN values are removed
Molecular_Weight(ind_nan) = []; % values are removed
% remove very large negative values (bug ?) like -1,00000e+005
ind_neg = (WF_dLogM<0);
WF_dLogM(ind_neg) = []; % very large negative values are removed
Molecular_Weight(ind_neg) = []; % values are removed
% normalize data
WF_dLogM_normalized = WF_dLogM./max(WF_dLogM); %
%plot Molecular Weight as x and calculated values as y
leg_str{k} = ['File : ' FileName ];
plot(Molecular_Weight,WF_dLogM_normalized);
end
% plot legend
legend(leg_str);
  6 Comments
Mathieu NOE
Mathieu NOE on 20 Nov 2023
small improvement to combine both data files in one plot
% d = dir('2023*.txt'); % d is a structure array that contains the list of all txt files starting with 2023
% d = dir(['NW*.txt'); % d is a structure array that contains the list of all txt files starting with NW
% You can save the output of DIR as a structured array, or in your case two structured arrays that can then be combined:
d1 = dir('NW*.txt');
d2 = dir('2023*.txt');
d = [d1;d2];
% create figure and use "hold on" to overlay multiple results
figure(1)
hold on
% main loop
for k = 1:numel(d)
FileName=d(k).name;
fprintf(1,'Now reading %s\n',FileName);
data = readmatrix(FileName,"NumHeaderLines",40,"DecimalSeparator",","); % read the data and skip the first 40 lines
% data has 20 columns :
% Ret. Vol. RI UV RALS LALS IV - DP IV - IP Adjusted RI Adjusted UV Adjusted RALS Adjusted LALS Adjusted IV - DP Adjusted IV - IP
%Molecular Weight Cumulative Weight Fraction Normalized Wt Fr Normalized Mole Fraction Conc. WF/dLogM
% I have to take WF/dLogM colum, find max of it and then divide every value of that column by found max value.
[m,n] = size(data);
WF_dLogM = data(:,n); % could be done also with = data(:,end) but we need anyway "n" again below
Molecular_Weight = data(:,n-5);
% find NaN index
ind_nan = isnan(WF_dLogM);
[begin,ends] = find_start_end_group(~ind_nan);
% we want only the first group of data
ind = (begin(1):ends(1));
WF_dLogM = WF_dLogM(ind);
Molecular_Weight = Molecular_Weight(ind);
% WF_dLogM(ind_nan) = []; % NaN values are removed
% Molecular_Weight(ind_nan) = []; % values are removed
% remove very large negative values (bug ?) like -1,00000e+005
ind_neg = (WF_dLogM<0);
WF_dLogM(ind_neg) = []; % very large negative values are removed
Molecular_Weight(ind_neg) = []; % values are removed
% normalize data
WF_dLogM_normalized = WF_dLogM./max(WF_dLogM); %
%plot Molecular Weight as x and calculated values as y
FileName = strrep(FileName,'_',' ');
leg_str{k} = ['File : ' FileName ];
plot(Molecular_Weight,WF_dLogM_normalized);
end
% plot legend
legend(leg_str);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [begin,ends] = find_start_end_group(ind)
% This locates the beginning /ending points of data groups
% Important : ind must be a LOGICAL array
D = diff([0;ind(:);0]);
begin = find(D == 1);
ends = find(D == -1) - 1;
end
Mathieu NOE
Mathieu NOE on 11 Dec 2023
hello again @Gabriel
do you mind accepting my answer (if it has fullfiled your expectations ) ? tx

Sign in to comment.

Categories

Find more on Environment and Settings 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!