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!