MATLAB:我的 for 循环没有在每次迭代的散点图中保留数据

问题描述 投票:0回答:1

我的所有代码都有效,但由于迭代时的某种原因,它不会存储每次试验中找到的数据。我已经多次浏览它,我可以看到它慢慢地逐个添加每个试验的数据,但最终的散点图只有最后一个试验。我知道这与存储数据或我的散点图所在的位置有关,但我不太擅长 malab。

    for trial = 1:50
        trial_start = (trial-1) * trial_duration;
        window_start = trial_start - time_before_trial_start;
        window_end = trial_start + trial_duration + time_after_trial_end;

        % times for current trial
        trial_event_times = event_times(event_times >= window_start & event_times <= window_end);

        trial_numbers = trial * ones(size(trial_event_times));
        

        % raster plot for current trial
        scatter(trial_event_times, trial);
        
        % x and y axis
        xlim([-0.5, 1.5]);
        ylim([0, 50]);
        xlabel('Time (s)');
        ylabel('Trial');
        title(['Raster Plot for (' signal_title ')']);
        

    end
matlab for-loop scatter-plot
1个回答
0
投票

在每个散点图之前保持当前图形内的位置,然后在 for 循环后保持轴关闭。

    for trial = 1:50
      trial_start = (trial-1) * trial_duration;
      window_start = trial_start - time_before_trial_start;
      window_end = trial_start + trial_duration + time_after_trial_end;

      % times for current trial
      trial_event_times = event_times(event_times >= ...
        window_start &   event_times <= window_end);

      trial_numbers = trial * ones(size(trial_event_times));
    
      % raster plot for current trial
      hold on
      scatter(trial_event_times, trial);
    
      % x and y axis
      xlim([-0.5, 1.5]);
      ylim([0, 50]);
      xlabel('Time (s)');
      ylabel('Trial');
      title(['Raster Plot for (' signal_title ')']);
    
    end

    hold off % don't forget
© www.soinside.com 2019 - 2024. All rights reserved.