Extract variable from .mat files

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 件のコメント

Rik
Rik 2020 年 11 月 9 日
What exactly do you want to do?
Haresh Kumar
Haresh Kumar 2020 年 11 月 9 日
Well, I have data in one variable of table that I want to classify into different groups.
Haresh Kumar
Haresh Kumar 2020 年 11 月 9 日
I want to read the frrom one specific variable of table (which is in .mat file) and then want to apply the rest of algorithm.
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 日

0 投票

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 件のコメント

Haresh Kumar
Haresh Kumar 2020 年 11 月 9 日
Hello,
Thank you for your answer. I am getting this error ''Reference to non-existent field 'folder''' while running your code. I wrote the folder name but it does not work.
Br,
Haresh Kumar
Rik
Rik 2020 年 11 月 9 日
Then you're running an older release of Matlab (or Octave), which you should have mentioned. You can replace that part with the full or relative path to the mat files.
Stephen23
Stephen23 2020 年 11 月 9 日
@Rik: probably one of those S variables should be renamed.
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 日

0 投票

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 件のコメント

Haresh Kumar
Haresh Kumar 2020 年 11 月 10 日
Hi,
I couldnot understand T.A commond, so let's make it simple. There are a number of .matfiles and in each .mat file there are variables. I want to load the data from a specific variable of .mat file and after loading data I want to delete the negative entries.
May be we can arrange a zoom call and I can show you my code which is more simple way of desribing I guess. Do you have any available time?.
Br,
Haresh Kumar
Stephen23
Stephen23 2020 年 11 月 10 日
"I couldnot understand T.A commond..."
T is a scalar structure containing the data imported from the mat file, where each variable in the mat file is stored in one field of the structure. You wrote in your original question that "...A is the vaiable from which I want to extract the data...", so accordingly I used the field A to access that data. If the variable actually has a different name than what you described in your question, then of course you would have to use that name instead.
Haresh Kumar
Haresh Kumar 2020 年 11 月 13 日
Hello,
Thank you for providing the breif information about this commond. I also want to delete the -ve entries from variable (A) before applying the rest of code and for that I am using this commond:
K=T.A;
If K <0
K=[]
else
K= K
But it doesnot work. Could you please guide me how can I solve this problem?.
Br,
Haresh Kumar
Stephen23
Stephen23 2020 年 11 月 13 日
The MATLAB way is to use logical indexing:
K = T.A;
K(K<0) = []

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

質問済み:

2020 年 11 月 9 日

コメント済み:

2020 年 11 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by