- length, not lentgth. Is this your actual code?
- what is SubjectFolder? size and type (cell array, string array, something else?)
- converting to char before checking number of elements makes me nervous
How to avoid overwriting/saving the same plots in a Loop through multiple subjects across different conditions?
3 views (last 30 days)
Show older comments
Hi everyone,
I have a dataset where I need to plot results of 20 subjects. My following code (converted from Brainstorm to Fieldtrip toolbox) first was saving time frequency (TF) plots for the 1st subject only in the results folder but not for other 19 subjects. I made some changes in the loop through subjects and it's saving the required plots for all subjects. But it is overwriting/ saving the same plot for all subjects.
Any suggestions where could be the mistake in the code? And how can I loop through so that it saves the plots for two conditions 1.Space 2. Time and 3. their difference Space-Time for each single subject?
New to matlab, your answer would be really appreciated.
Thanks in advance
Best,
Abrar
%%Time Frequency Representation
clear all
close all
clc
ft_folder = 'D:\fieldtrip-20200406\fieldtrip-20200406'
run([ft_folder 'ft_defaults.m'])
results_folder = 'D:\BS\wo\PICS';
mkdir(results_folder)
%% define folders
data_main = 'D:\BS\wo\data'; %data imported from Brainstorm software (BS)
subjects_id = 01:20
SubjectFolder = ls([data_main 'Subject*']) %SubjectFolder contains list of all subjects as folders Subject01, Subject02 and so on with .mat files
SubjectFolder = extractfield(dir([data_main 'Subject*', ]),'name');
folder_spatial_trials = 'Spatial_Correct';
folder_temporal_trials = 'Temporal_Correct';
% preparation struct
load('D:\BS\wo\code_extra\data_megin.mat')
cfg.layout = 'neuromag306all.lay';
fignum = 1
......................%some code
%Following loop is saving the required plot but overwriting same plot for all subjects
for s = 1:lentgth(char(SubjectFolder))% loop through subjects for TF plots across space and time condition
%The plot 1 which i need to save for each subject
figure('Name',[char(SubjectFolder(s)) ' Space']), ft_multiplotTFR(cfg, TFRwave_space)
set(gcf, 'units','normalized','outerposition',[0 0 1 1])
saveas(gca,[results_folder '' char(SubjectFolder(s)) ' Space.jpeg']) % visualize and save Time-Freq plots for Spatial trials for each subject
end
% The plot 2 which i need to save for each subject
for s = 1:lentgth(char(SubjectFolder))
figure('Name',[char(SubjectFolder(s)) ' Time']), ft_multiplotTFR(cfg, TFRwave_time)
set(gcf, 'units','normalized','outerposition',[0 0 1 1])
saveas(gca,[results_folder '' char(SubjectFolder(s)) ' Time.jpeg']) % visualize and save Time-Freq plots for Temporal trials for each subject
end
TFRwave_diff = TFRwave_space; % visualize and save TF power difference for each subject
TFRwave_diff.powspctrm = (TFRwave_space.powspctrm - TFRwave_time.powspctrm)./(TFRwave_space.powspctrm + TFRwave_time.powspctrm);
% The plot 3 which i need to save for each subject
figure,
cfg = [ ] ;
% cfg.zlim = [- 1 1];
cfg.colorbar = 'yes';
cfg.layout = 'neuromag306all.lay';
ft_multiplotTFR(cfg, TFRwave_diff)
set(gcf, 'units','normalized','outerposition',[0 0 1 1])
saveas(gca,[results_folder '' char(SubjectFolder(s)) ' Space-Time.jpeg'])
end
6 Comments
Sindar
on 23 Oct 2020
Edited: Sindar
on 23 Oct 2020
Assuming it isn't some weirdness with one of the above suggestions (I'm still eyeing that char suspiciously), what is the file it keeps overwriting? Is it what you'd expect for the first SubjectFolder? The last? Something completely different?
Also, can you copy the results of these:
SubjectFolder{1}
fprintf('%s\n',SubjectFolder{1:5})
Also, double check that you're actually looping. If SubjectFolder is really 1x20, then you only have one iteration here:
for s=1:size(SubjectFolder,1)
this is safer:
for s=1:numel(SubjectFolder)
Answers (0)
See Also
Categories
Find more on Digital Filter Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!