how to extract table data in structure using multi variables in its path

hello,
I'm using a data-base which is built out of folders and structure in them.
I'm able to get into the right folder but having trouble getting to the table in the structure as they have to be called using few variables.
I want to do the same thing as I do with "V{j}" but it doesn't work.
in the 2 last rows: 1-that work , 2nd- what I want it to be.
thanks!!
function [COP_trail] = COP_extract(P) %Participant # & Subject#_raw have
% to be changed manualy!!
load(['C:\Users\ETAMAR\Desktop\bio-project\files\MATfiles\MAT files\Participant2\Raw_Data.mat'])
COP_trail = cell(8,7);
subjects = {'Subject1_raw','Subject2_raw','Subject3_raw','Subject4_raw','Subject5_raw','Subject6_raw','Subject7_raw','Subject8_raw','Subject9_raw','Subject10_raw','Subject11_raw','Subject12_raw','Subject13_raw','Subject14_raw','Subject15_raw','Subject16_raw'};
V = {'V1','V15','V2','V25','V3','V35','V4'};
for j = 1:7
COP_trail{1,j} = V{j};
for i = 1:7
%comand = sprintf('%s.%s.',(subjects{P}),V{j});
COP_trail{i+1,j} = Subject2_raw.(V{j}).COP{i,2} ;
%COP_trail{i+1,j} = (subjects{P}).(V{j}).COP{i,2} ;
end
end

 採用された回答

Stephen23
Stephen23 2023 年 2 月 19 日
編集済み: Stephen23 2023 年 2 月 20 日
The simple solution is to always LOAD into an output variable, which itself is a scalar structure. For example:
F = 'C:\Users\ETAMAR\Desktop\bio-project\files\MATfiles\MAT files\Participant2\Raw_Data.mat';
S = load(F);
COP_trail = cell(8,7);
subjects = {'Subject1_raw','Subject2_raw','Subject3_raw','Subject4_raw','Subject5_raw','Subject6_raw','Subject7_raw','Subject8_raw','Subject9_raw','Subject10_raw','Subject11_raw','Subject12_raw','Subject13_raw','Subject14_raw','Subject15_raw','Subject16_raw'};
V = {'V1','V15','V2','V25','V3','V35','V4'};
for j = 1:numel(V)
COP_trail{1,j} = V{j};
for i = 1:numel(subjects)
COP_trail{i+1,j} = S.(subjects{i}).(V{j}).COP{i,2};
end
end
To aid debugging you might like to swap the loops around, something like this:
subjects = {'Subject1_raw','Subject2_raw','Subject3_raw','Subject4_raw','Subject5_raw','Subject6_raw','Subject7_raw','Subject8_raw','Subject9_raw','Subject10_raw','Subject11_raw','Subject12_raw','Subject13_raw','Subject14_raw','Subject15_raw','Subject16_raw'};
V = {'V1','V15','V2','V25','V3','V35','V4'};
F = 'C:\Users\ETAMAR\Desktop\bio-project\files\MATfiles\MAT files\Participant2\Raw_Data.mat';
S = load(F);
COP_trail = repmat(V,8,1);
for i = 1:numel(subjects)
tmp = S.(subjects{i});
for j = 1:numel(V)
COP_trail{i+1,j} = tmp.(V{j}).COP{i,2};
end
end

その他の回答 (0 件)

カテゴリ

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

製品

リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by