フィルターのクリア

How to load multiple input txt file in matlab?

2 ビュー (過去 30 日間)
majid
majid 2023 年 2 月 5 日
回答済み: Sulaymon Eshkabilov 2023 年 2 月 5 日
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 件のコメント
Stephen23
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
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 件)

カテゴリ

Help Center および File ExchangeData Import and Analysis についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by