Extract variable from .mat files

조회 수: 15(최근 30일)
Haresh Kumar
Haresh Kumar 2020년 11월 9일
댓글: Stephen23 2020년 11월 13일
Hello,
I have mutiple .mat files which I am running in a for loop. I want to extract the data from the specific variable of .mat file. I tried .variable commond, but it didnot work. Could you please suggest me which commond will I use in this case?.
Here is the attached code:
S = dir('*.mat');
LT= [];
for i=1:length(S)
disp(S(i).name)
T = readtable((S(i).name.(A));
Where, A is the vaiable from which I want to extract the data and all .mat files are in tabular format.
Br,
Haresh Kumar
  댓글 수: 4
Stephen23
Stephen23 2020년 11월 9일
"...all .mat files are in tabular format."
Please upload a sample file by clicking the paperclip button.

댓글을 달려면 로그인하십시오.

답변(2개)

Rik
Rik 2020년 11월 9일
편집: Rik 2020년 11월 9일
I think I understand what you mean. You will have to use load to load a variable from a mat file:
S = dir('*.mat');
LT= [];
for n=1:length(S)
A=load(fullfile(S(n).folder,S(n).name));
T=A.A;
%remainder of your code here
end
  댓글 수: 4
Rik
Rik 2020년 11월 9일
Thanks for the comment. I guess I shouldn't edit code on autopilot (I always load to S). I'll edit my answer.

댓글을 달려면 로그인하십시오.


Stephen23
Stephen23 2020년 11월 9일
D = 'absolute/relative path to the folder where the files are saved';
S = dir(fullfile(D,'*.mat'));
for k = 1:numel(S)
F = fullfile(D,S(k).name);
T = load(F);
T.A
end
  댓글 수: 4
Stephen23
Stephen23 2020년 11월 13일
The MATLAB way is to use logical indexing:
K = T.A;
K(K<0) = []

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by