Extracting table from data structure

Hello,
I have multile .mat files and from these files I want to extract the table with variable name B (500000×1 double). At first, I have loaded data using S = dir('*.mat'); and then I am using commond K = S.B; to extract the data from table variable name B. This commond doesnot work and gave me this message: Reference to non-existent field 'B'.. Can you guide me which commond will I use to extract the table data from data structure.
Br,
Haresh Kumar

댓글 수: 4

"Reference to non-existent field 'B'."
Where S is your structure, show us the complete output of this command:
fieldnames(S)
Haresh Kumar
Haresh Kumar 2021년 4월 6일
6×1 cell array
{'name' }
{'folder' }
{'date' }
{'bytes' }
{'isdir' }
{'datenum'}
Stephen23
Stephen23 2021년 4월 6일
편집: Stephen23 2021년 4월 6일
@Haresh Kumar: that certainly looks like the structure returned by DIR, which by default does not contain a "B" field.
Most likely you forgot to actually import some file data (possibly into a table).
Haresh Kumar
Haresh Kumar 2021년 4월 6일
Hello,
Let me make it simple. I am giving directory path inorder to run the multiple .mat files from the folder. Each .mat file (number (1)......number(n)) contains multiple tables from which I only want the table of variable B. Now, you can guide which commond will I use to get the data from variable B (Table)
Br,
Haresh Kumar

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

답변 (2개)

Hiro Yoshino
Hiro Yoshino 2021년 4월 6일

0 개 추천

First thing you should do is check if S is a table variable.
If it turns out to be a table variable, then the next thing would be running the following command to check if B exists
S.Properties.VariableNames
Good luck.

댓글 수: 3

Haresh Kumar
Haresh Kumar 2021년 4월 6일
Hello,
S is 181×1 struc.
Br,
Haresh Kumar
Hiro Yoshino
Hiro Yoshino 2021년 4월 6일
type S in the command window to see what fields S has.
Haresh Kumar
Haresh Kumar 2021년 4월 6일
name
folder
date
bytes
isdir
datenum

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

Steven Lord
Steven Lord 2021년 4월 6일

0 개 추천

At first, I have loaded data using S = dir('*.mat');
The dir function does not load data. All it does is generate a list of files in the directory with the .mat extension. To load the data you'll need to use the load function. Then if you want to check if the loaded data contains a variable named B (that has been loaded into a field of the output of load you can use isfield.
data = load('census.mat')
data = struct with fields:
cdate: [21×1 double] pop: [21×1 double]
doesItHaveAVariableB = isfield(data, 'B')
doesItHaveAVariableB = logical
0
doesItHaveAVariableCdate = isfield(data, 'cdate')
doesItHaveAVariableCdate = logical
1

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

질문:

2021년 4월 6일

댓글:

2021년 4월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by