Problem creating loop using data from imported table/array - how to analyse & save data from each cycle?
1 view (last 30 days)
Show older comments
Hi! I need urgent help - I am really new to Matlab, but am trying to create a script to analyse data from an exported waveform (*.txt). I have successfully imported the file & done the peak analysis for one example trial, incl. saving min and max peaks to (unfortunately, so far different) tables, etc. Now I need to execute the whole thing for each of the 82 trials (and later average some of them) and can't seem to find a solution neither in books, nor online. I have 82 rows = trials of 500 ms & 500 columns = signal amplitude @every millisec
The imported table is called TestSkript. I extracted from it only the data I need. Then tried and failed miserably to create a loop for the rest of the script. This is an example of my latest effort, mimicking sth. I found in a book (mind you, I still have to apply it to more commands - but I am first trying to make it work at all):
var=TestSkript(1:82,17:517);
openvar('var')
data=table2array(var)
T = zeros(1,501);
T(1) = data(1,1:501);
for i=0:82;
i=i+1;
T(i) = data(i,:)
end
figure
plot(T)
Please, help! Any advice on how I can create a loop and execute it for one data row at a time is deeply appreciated.
2 Comments
Guillaume
on 12 Jun 2018
If your data is truly in a table then it's possible that rowfun may be all that is needed. Although from your description it doesn't sound that the data should be in a table.
In any case, for us to help you more we need a much better description of what you're wanting to do with each row of your data. The code you've given us is complete gibberish that would result in many errors. and does not appear to do anything with the rows.
Answers (2)
Bob Thompson
on 12 Jun 2018
Couple of things here. First for T(1) = data(1,1:501) you can't assign a matrix to a single double array element. If you are trying to assign all of the values of the first row of data to T then use T = data(1,:).
Second, don't start a loop index at 0, it's just not a good coding practice IMO. Also, you don't need i = i+1, as your index, i, is advance automatically each time the loop runs.
Your current loop does advance through each data row. If you want to perform some kind of calculation to that row then include the calculation into the loop and have the results saved into the loop. The setup you have now looks good, as you are essentially trying to save each row of data into an element of T. The only problem I see with this is that T is already defined as an array of doubles (based on the zeros function you used earlier), and therefore cannot contain an array in and individual element. I would suggest removing the line T = zeros(1,501);.
2 Comments
Bob Thompson
on 12 Jun 2018
You're getting the error because you're trying to define a single numeric value using an entire row of an array.
size(T) == 1x501 % As determined by T = data(1,:);
size(T(i)) == 1x1 % Putting a single index like this yields a single element
size(data(i,:) == 1x501 % Same as data(1,:)
The quickest solution to this is to simply define T = data(i,:) within the for loop. T will be overwritten each time, but it will retain the proper size. Alternatively, you can turn T into a cell matrix, which allows for other matrices to be stored within each cell.
For the file naming I would suggest creating a dynamically created string within the loop, and using this string as your file name.
title = ['maxpeaktable',num2str(i),'.xls'];
writetable(Tmax,title);
Guillaume
on 13 Jun 2018
Step 1: Wrap all your code for processing one single row into a function that takes your table and the row number as input. E.g.:
function processsignal(signals, signumber)
time = 0:500;
amp = signals{signumber, 17:517};
%your code from plot(time, amp) onwards ...
plot(time, amp);
...
end
Step 2: Modify the save functions to generate the filenames using the signal number. e.g.
writetable(Tmax,'maxpeaktable1.xls')
becomes
writetable(Tmax, compose('maxpeaktable%02d.xls', signumber));
Step 3: write the script that calls your function for row of your table:
for row = 1:82
processsignal(var, row);
end
Miscellaneous notes:
a)
str = {'Baseline (mV)','=' baseline};
is not the way to create a string. Again, use compose:
str = compose('Baseline (mv) = %f', baseline);
b)
figure
fig=figure
Creates two figures in succession. The first one will never be used.
0 Comments
See Also
Categories
Find more on Logical 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!