Effacer les filtres
Effacer les filtres

How to load multiple input txt file in matlab?

2 vues (au cours des 30 derniers jours)
majid
majid le 5 Fév 2023
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*')
Error using matlab.internal.lang.capability.Capability.require
Support for Java user interfaces is required, which is not available on this platform.

Error in uigetfile (line 117)
Capability.require(Capability.Swing);
  1 commentaire
Stephen23
Stephen23 le 5 Fév 2023
"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.

Connectez-vous pour commenter.

Réponse acceptée

Sulaymon Eshkabilov
Sulaymon Eshkabilov le 5 Fév 2023
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

Plus de réponses (0)

Catégories

En savoir plus sur Data Import and Analysis dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by