MATLAB Answers

ned to turn iterated columns into a single column

2 views (last 30 days)
Phoebe Langius
Phoebe Langius on 17 Jun 2021
Commented: Stephen on 17 Jun 2021
I havea code that takes data from multiple matlab files and then created about output data of it. The problem is that the data is in separate columns per matlab file input. I want them to all be on one column
For example the first input is 50x1, the next is 100x1, then 22 x1. (the data amount will be different every time). I want these to be 177x1
This is the current codethat I have:
%% Pre-allocate memory for group data
GroupData.SubjectList = subjlist;
GroupData.Interval = NaN(length(subjlist));
%% Collect individual data and put into structure
for iteration = 1:length(subjlist)
if subjlist (iteration, 1)<100
SubjectData = sprintf('load NCL0%d_Session00%d.mat',subjlist(iteration,1), subjlist(iteration,2));
eval(SubjectData);
else
SubjectData = sprintf('load NCL0%d_Session0%d.mat', subjlist(iteration,1), subjlist(iteration,2));
eval(SubjectData);
end
RawHoldTime= (Poke_HoldData(:,10))
%[ data_output ] = HoldTime_RawHoldTime( Poke_HoldData )
GroupData.RawHoldTime(:,iteration) = RawHoldTime;
%% combining into 1 column
end
end

Answers (2)

Stephen
Stephen on 17 Jun 2021
Edited: Stephen on 17 Jun 2021
Do NOT use EVAL for trivial code like this. Rather than forcing yourself into writing complex, obfuscated code just because you want to use command syntax, you should always prefer to use function sytnax:
For example, replace this obfuscated code:
SubjectData = sprintf('load NCL0%d_Session%03d.mat',subjlist(iteration,1), subjlist(iteration,2));
eval(SubjectData);
with this simpler, cleaner, more efficient code:
F = sprintf('NCL%02d_Session%03d.mat',subjlist(iteration,1:2));
S = load(F); % <- function syntax!
Note how I also loaded into an output variable S (which is a scalar structure) to make the code much more robust.
Rather than duplicating code inside that IF-ELSE and adding those leading zeros by hand, you should use the SPRINTF fieldwidth and leading zeros options to specify the number of leading zeros, as my code above shows.
A robust and efficient way to create one vector as you request from the imported data of multiple files is to concatenate the imported data after the loop. This is very easy to achieve using a comma-separated list:
N = size(subjlist,1); % SIZE is reliable, LENGTH is not.
C = cell(1,N); % preallocate
for k = 1:N
F = sprintf('NCL%02d_Session%03d.mat',subjlist(k,1:2));
S = load(F);
C{k} = S.Poke_HoldData(:,10);
end
V = vertcat(C{:}) % <- comma-separated list
Note that you can easily process files that are lcoated in another directory (separate your code and data!):
P = 'absolute or relative path to the folder where the files are saved';
..
for k = 1:N
..
S = load(fullfile(P,F));
..
end
  10 Comments
Stephen
Stephen on 17 Jun 2021
Judging by those filenames, you should set the first fieldwidth to 4:
... 'NCL%04d_Session%03d.mat'
... ^

Sign in to comment.


Phoebe Langius
Phoebe Langius on 17 Jun 2021
I have figured it out! Thank you so much!
  1 Comment
Stephen
Stephen on 17 Jun 2021
I am glad that you got it working.
Please remember to accept and/or vote for my answer if it helped you.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!