필터 지우기
필터 지우기

How to extract data by looping through struct

조회 수: 11 (최근 30일)
Emu
Emu 2022년 9월 13일
댓글: Emu 2022년 9월 13일
Hi - I have some data saved as .mat files and i am trying to load it in (think I have done this) and then loop through the data in the struct to plot it all on the same graph (to then plot the mean average also.) Here is my script that doesn't work:
S = dir(fullfile(outputDir,'*.mat'));
fields = fieldnames(S)
for k = 1:length(fields)
F = fullfile(outputDir,S(k).name);
S(k).data = load(F);
S.(fields{k});
%a = table2array(readtable(strcat(S(outputFolders).folder,'/', S(outputFolders).name)));
for j= 1:length(S.data);
%for i = 1:length(preSumTypesoutput)
plot([-6 -5 -4 -3 -2 -1], data(:,i)); hold on;
xticks([-6 -5 -4 -3 -2 -1])
ylim([0 10])
ylim([0 5])
title('Pre-arousal vocalisations')
end
end
and I attach some example data (I am trying to plot 6 of these).

채택된 답변

Walter Roberson
Walter Roberson 2022년 9월 13일
S = dir(fullfile(outputDir,'*.mat'));
S is the output of dir()
fields = fieldnames(S)
The field names returned by dir() include such items as 'name', 'folder', 'bytes', 'isdir' and so on.
for k = 1:length(fields)
You want to loop over the directory structure... unusual, but maybe there is a reason
F = fullfile(outputDir,S(k).name);
You are using the field name index for the directory structure, and using it to index the struct array returned by dir() . If you wanted to loop over the fields of S you would need to use something like
thisfield = {S.(fields{K})};
  댓글 수: 3
Walter Roberson
Walter Roberson 2022년 9월 13일
S = dir(fullfile(outputDir,'*.mat'));
filenames = fullfile({S.folder}, {S.name});
num_files = length(filenames);
tiledlayout('flow');
for K = 1 : num_files
thisfile = filenames{K};
[~, basename, ~] = fileparts(thisfile);
datastruct = load(thisfile);
data = datastruct.preSumTypesoutput; %fixed field name!!
nexttile();
plot([-6 -5 -4 -3 -2 -1], data);
xticks([-6 -5 -4 -3 -2 -1])
ylim([0 10])
ylim([0 5])
title("Pre-arousal vocalisations for " + basename)
end
Emu
Emu 2022년 9월 13일
I see :) thank you so much that is incredibly helpful ! how would I plot a mean average?

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

추가 답변 (1개)

Emu
Emu 2022년 9월 13일
ok thank you, but I still don't understand how to loop through my data to plot it..?

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by