How to load multiple input txt file in matlab?

2 views (last 30 days)
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 Comment
Stephen23
Stephen23 on 5 Feb 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.

Sign in to comment.

Accepted Answer

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 5 Feb 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

More Answers (0)

Categories

Find more on Just for fun in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!