load and do operations on multiple file in a loop

4 visualizaciones (últimos 30 días)
Dipen Bista
Dipen Bista el 11 de En. de 2019
Comentada: Luna el 11 de En. de 2019
Hi,
I have my files named 'Us2asp_001.txt', 'Us2asp_002.txt, .......'Us2asp_00n.txt'.
I want to read the files and do similar operations in each of them.
I read about it and got to know that sprintf is a solution. However, the files are saved into a cell array and I am not sure how to do the further calculations.
I am rather new to matlab. Can you please help me with this?
I have attached the code which I want to write in a loop.
Thank you in advance.
Regards
Dipen
data1=readtable('Upasp2_001.txt');
data2=readtable('Upasp2_002.txt');
data3=readtable('Upasp2_003.txt');
%% Changing units
data1{:,2:end}=data1{:,2:end}*-1000;
data2{:,2:end}=data2{:,2:end}*-1000;
data3{:,2:end}=data3{:,2:end}*-1000;
%% Calculation
data1{:,7}=data1{:,3}+data1{:,5};
data1{:,8}=data1{:,4}+data1{:,6};
data1{:,9}=sqrt(data1{:,7}.^2+data1{:,8}.^2);
data1.Properties.VariableNames([7 8 9])={'Px','Py','Ptot'};
data2{:,7}=data2{:,3}+data2{:,5};
data2{:,8}=data2{:,4}+data2{:,6};
data2{:,9}=sqrt(data2{:,7}.^2+data2{:,8}.^2);
data2.Properties.VariableNames([7 8 9])={'Px','Py','Ptot'};
data3{:,7}=data3{:,3}+data3{:,5};
data3{:,8}=data3{:,4}+data3{:,6};
data3{:,9}=sqrt(data3{:,7}.^2+data3{:,8}.^2);
data3.Properties.VariableNames([7 8 9])={'Px','Py','Ptot'};
%% Plotting
figure
plot(data1.uxtop,data1.Ptot,data2.uxtop,data2.Ptot,data3.uxtop,data3.Ptot);
legend('Mesh 1','Mesh2','Mesh3');
title('Load displacement');
xlabel('ux (mm)');
ylabel('Total Load applied (P) kN');
xlim([0,10]);

Respuesta aceptada

Luna
Luna el 11 de En. de 2019
Editada: Luna el 11 de En. de 2019
Hi Dipen,
As far as I understand you want to read all the files in a specific folder with a for loop and do the calculations.
I wrote some code for you, you can check if it works correctly.
(Please don't forget to read comments.)
%% this part gives you the txt file name list in your folder
dirName = 'C:\Users\....\Desktop\NewFolder'; % folder path (please edit this as your own)
files = dir( fullfile(dirName,'*.txt') ); % list of all *.txt files
files = {files.name}'; % file names
%pre allocation for legend for the plot
legendString = cell(1,numel(files));
%% for loop
for i = 1:numel(files) % for loop for each file
% readtable
data = readtable(files{i});
% changing units
data{:,2:end}=data{:,2:end}*-1000;
% Calculation
data{:,7}=data{:,3}+data{:,5};
data{:,8}=data{:,4}+data{:,6};
data{:,9}=sqrt(data{:,7}.^2+data{:,8}.^2);
data.Properties.VariableNames([7 8 9])={'Px','Py','Ptot'};
% plot
figure;
plot(data.uxtop,data.Ptot); % plots the ith file data
hold on; % holds the plot for the next loop
legendString{i} = ['Mesh',sprintf('%d',i)]; % creates legend string as MeshX where X is a number
end % end for loop
%% legend,title,axis properties for plotted figure
legend(legendString);
title('Load displacement');
xlabel('ux (mm)');
ylabel('Total Load applied (P) kN');
xlim([0,10]);
ps. If you want to shorten your calculations section, please share an example data(txt) and desired output.
  2 comentarios
Dipen Bista
Dipen Bista el 11 de En. de 2019
Hi Luna,
It works perfectly.
Thank you very much :).
Attached files
where calculation is to be performed.
  • Upasp2_001.txt
  • Upasp2_002.txt
The plot of 'uxtotal' vs 'Ptot' from these files should be compared to
plot of 'uxtotal' vs 'Ptot' from the 'labtotal.txt'
Best Regards,
Dipen
Luna
Luna el 11 de En. de 2019
your welcome :)

Iniciar sesión para comentar.

Más respuestas (1)

madhan ravi
madhan ravi el 11 de En. de 2019
Lookup cell2mat() , horzcat() , vertcat() and cellfun() to work with cells.
  1 comentario
Dipen Bista
Dipen Bista el 11 de En. de 2019
Hi Madhan Ravi
Thank you for the answer.
My matlab skills are not that good.
Could you please write how would you wirte the code in matrix for the above problem I worte if you would have done it?
Best Regards,
Dipen

Iniciar sesión para comentar.

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by