MATLAB Answers

how to read lots of file and plot them

13 views (last 30 days)
Samaneh Arzpeima
Samaneh Arzpeima on 9 Jul 2019
Commented: Raj on 25 Apr 2020
Hi
Dearly need your help!
I have a folder,its name is "waves".
Files name inside it are like
S(numbers from 1 to 54).L0.FX(X Y Z) for example ⇒ S46.L0.FXZ.sema
so there are 54*3=162 files in my "waves" folder.
each file has 2 columns of data, time and acceleration.
I have to read all the files for a same number and 3 X,Y,Z component and plot them in one figure
S(number).L0.FX(X).sema
S(number).L0.FX(Y).sema
S(number).L0.FX(Z).sema
I suppose to have 54 figurs.
it is just too complicated
how can i write a code that is going to read data and plot, I have no idea.
the only thing that I came up with
idir='./waves/'
files=dir([idir.'*sema'])
for i=1:numel(files)/3;
for d=[X Y Z]
file_name = ............
end
please make it simple I am still studying
Thank You

Accepted Answer

Raj
Raj on 9 Jul 2019
You can use this:
for ii=1:54 % Number of S set of files
for jj=1:3 % Number of sub files for each S index
if jj==1
temp='X';
elseif jj==2
temp='Y';
else
temp='Z';
end
%% Initialize variables.
myfilename= sprintf('S%i.L0.FX%s.sema',ii,temp);
filename = fullfile('C:\','Users','user','Documents','MATLAB','waves_sample',myfilename);% Put your file path here
delimiter = ' ';
%% Format string for each line of text:
% column1: double (%f)
% column2: double (%f)
formatSpec = '%f%f%[^\n\r]';
%% Open the text file.
fileID = fopen(filename,'r');
%% Read columns of data according to format string.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true, 'EmptyValue' ,NaN, 'ReturnOnError', false);
%% Close the text file.
fclose(fileID);
%% Allocate imported array to column variable names (NOTE: Time and Acceleration data gets overwritten with each new file read)
Time = dataArray{:, 1};
Acceleration = dataArray{:, 2};
%% Clear temporary variables
clearvars filename delimiter formatSpec fileID dataArray ans;
figure(ii)% Figure1 will be for S1 three files and so on
plot(Time,Acceleration)% customize plots here as you desire
hold on
end
end

  2 Comments

ddd ppp
ddd ppp on 25 Apr 2020
did you just make that? or did you find that code somewhere? thats a lot of code for one answer. thanks for your time.
Raj
Raj on 25 Apr 2020
Well I found some part of the code online and modified it to suit this answer.

Sign in to comment.

More Answers (1)

KSSV
KSSV on 9 Jul 2019
files = dir('*.txt') % Give your extension
N = length(files) ; % total number of files
% loop for each file
for i = 1:N
thisfile = files(i).name ;
% Read the file
% plot the file
end

  1 Comment

Samaneh Arzpeima
Samaneh Arzpeima on 9 Jul 2019
Did you read my question
my file name is like
S(numbers from 1 to 54).L0.FX(X Y Z) for example ⇒ S46.L0.FXZ.sema
Your tip wont work for me
thanx for your time anyway

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!