How to load multiple input txt file in matlab?
조회 수: 2 (최근 30일)
이전 댓글 표시
Hello to everyone.
I want to load several txt file (I dont know how many it depends on my experimental data that I get) to matlab which has 5 coloumns and Do some calculation.
my problem is that I dont know how can I load them in different name like data1, data2, data3 and ... .
is here any one who can help me?
thanks.
here is part of my code that I could load just one txt file:
clear all
close all
clc
exfilt={'*.txt'};
[filename,filepath]= uigetfile(exfilt,'pick an txt file!');
data = importdata([filepath filename]) ;
frequency = data(:,1);
eps_prime = data(:,2);
eps_zegond = data(:,3);
mu_prime = data(:,4);
mu_zegond = data(:,5);
plot(frequency,eps_prime,'b*')
figure
plot(frequency,eps_zegond,'b*')
figure
plot(frequency,mu_prime,'b*')
figure
plot(frequency,mu_zegond,'b*')
댓글 수: 1
Stephen23
2023년 2월 5일
"my problem is that I dont know how can I load them in different name like data1, data2, data3 and ... ."
Do NOT do that, unless you want to force yourself into writing slow, complex, inefficient code.
The simple and efficient approach is to use indexing. The MATLAB documentation shows how to use indexing:
You should use indexing too.
채택된 답변
Sulaymon Eshkabilov
2023년 2월 5일
For your exercise, if your data files contain the same data type in terms of columns, then you can use this code to collect all data and plot them all in 4 separate plot figures:
close all
clearvars
TXTfile = dir('*.txt');
Nfiles = length(TXTfile);
mydata = cell(1, Nfiles);
for k = 1:Nfiles
mydata{k} = readtable(TXTfile(k).name);
frequency = mydata{k}.Var1;
eps_prime = mydata{k}.Var2;
eps_zegond = mydata{k}.Var3;
mu_prime = mydata{k}.Var4;
mu_zegond = mydata{k}.Var5;
figure(1)
plot(frequency,eps_prime), hold all
figure(2)
plot(frequency,eps_zegond), hold all
figure(3)
plot(frequency,mu_prime), hold all
figure(4)
plot(frequency,mu_zegond), hold all
end
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Import and Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!